PHP langsys/langsys-php

बेस PHP SDK: किसी भी PHP ऐप के लिए एक क्लाइंट — चाहे सादा PHP हो, कोई फ़्रेमवर्क, वर्कर या CLI। आपके कोड में लिखा वाक्यांश ही लुकअप की और बेस भाषा का डिफ़ॉल्ट पाठ भी है — न कोई कुंजी फ़ाइल, न कोई एक्सट्रैक्शन चरण। अनुवाद सर्वर पर होता है, इसलिए पेज पहले से स्थानीयकृत होकर पहुँचता है।

यह बैकएंड SDK है, इसलिए इस पेज पर ब्राउज़र में चलने वाला प्लेग्राउंड नहीं है — ब्राउज़र PHP नहीं चला सकता। Laravel पर हैं? Laravel पैकेज इस क्लाइंट को Blade डायरेक्टिव, मिडलवेयर और फ़ैसाड के साथ रैप करता है। पूरा संदर्भ: README।

AI कोडिंग एजेंट इस्तेमाल कर रहे हैं? उसे यह प्रॉम्प्ट दें
Add Langsys translation to this PHP app.

1. Read https://docs.langsys.dev/llms.txt, then follow
   https://docs.langsys.dev/learn/sdk/php/ for the install and setup.
2. The English text in the code is the translation key. Wrap user-facing
   strings as they are: no keys file, no extraction step, and no
   hand-written plural or gender branches (Langsys generates those).
3. Read the Langsys project id and API key from environment variables.
   The write key is for development and servers only; anything shipped
   to the browser gets the read-only key.
4. Keep brand names and user-generated content out of translation.

इंस्टॉल करें

composer require langsys/langsys-php

PHP 7.4 या उसके बाद का संस्करण, जिसमें curl, json और intl एक्सटेंशन हों — intl ही हर लोकेल के लिए बहुवचन रेंडर करता है और संख्याओं व तारीखों को फ़ॉर्मैट करता है।

.env
# A server can hold a WRITE key — it never reaches visitors. A write key is
# what lets phrases your pages use for the first time register themselves.
LANGSYS_API_KEY=your-api-key
LANGSYS_PROJECT_ID=your-project-id

अभी तक प्रोजेक्ट या कीज़ नहीं हैं? क्विकस्टार्ट दो मिनट में आपको दोनों दिला देता है।

क्लाइंट

हर अनुरोध के लिए एक क्लाइंट। यह अपने क्रेडेंशियल एनवायरनमेंट से पढ़ता है और हर लोकेल का कैटलॉग कैश करता है, इसलिए किसी पेज का अनुवाद करने में अधिकतम एक API कॉल लगती है।

स्रोत देखें ↗
<?php
require 'vendor/autoload.php';

use LangsysSDKClient;

// Reads LANGSYS_API_KEY and LANGSYS_PROJECT_ID from the environment.
$langsys = new Client();
$langsys->setLocale('es-es');

अनुवाद करें

translate() में लोकेल दूसरे और कैटेगरी तीसरे स्थान पर आती है। क्लाइंट का मौजूदा लोकेल इस्तेमाल करने के लिए लोकेल की जगह null पास करें।

स्रोत देखें ↗
// translate($phrase, $locale, $category, $contentBlockId, $params)
echo $langsys->translate('Order confirmed', null, 'Checkout');   // current locale
echo $langsys->translate('Order confirmed', 'fr-fr', 'Checkout'); // an explicit one

// A phrase the project doesn't have yet renders as written — never blank.

वाक्य में मान

प्लेसहोल्डर {name} रूप का उपयोग करते हैं, जो हर Langsys SDK में इसी तरह संग्रहीत होता है, इसलिए एक ही कैटलॉग PHP बैकएंड और JS फ़्रंटएंड दोनों से एक जैसा रेंडर होता है।

स्रोत देखें ↗
// Pass values as params, never build the string yourself.
echo $langsys->translate('Hello, {name}!', null, 'Greetings', null, ['name' => $user->name]);

// sprintf('Hello, %s!', $name) would register "Hello, Sarah!", "Hello, Ahmed!", …
// — one catalog entry per value, billed as words, and impossible to undo.

बहुवचन

देखें · 1:14 बहुवचन रूप

वाक्य को सीधा-सादा लिखें: Langsys अनुवाद के दौरान हर लोकेल के बहुवचन रूप बना देता है, और SDK आपकी गिनती से मेल खाने वाला रूप दिखाता है।

स्रोत देखें ↗
// Written flat. Langsys generates each locale's plural forms during
// translation and the SDK picks the branch that matches the count.
echo $langsys->translate('You have {count} new messages.', null, 'Inbox', null, ['count' => $count]);

Langsys द्वारा बनाए गए बहुवचन रूप PHP में langsys/langsys-php 1.3.2 से उपलब्ध हैं; इससे पहले के संस्करण हर संख्या के लिए बहुवचन रूप ही दिखाते हैं, 1 के लिए भी।

लिंग-अनुरूपता

स्पैनिश, फ़्रेंच या रूसी में {username} has been invited का रूप उस व्यक्ति के अनुसार बदलता है जिसके बारे में बात हो रही है। Langsys अनुवाद के दौरान ये शाखाएँ अपने आप बनाता है — ठीक वैसे ही जैसे बहुवचन रूप बनाता है — और आर्ग्युमेंट का नाम वेरिएबल के नाम में _gender जोड़कर रखा जाता है: male, female या other। जब भी यह जानकारी हो, इसे अवश्य दें। यह कैसे काम करता है, और जिन वाक्यांशों को इसकी ज़रूरत है उन्हें कैसे खोजें →

जेंडर आर्ग्युमेंट पास करना स्रोत देखें ↗
// Pass the agreement argument alongside the value it inflects.
echo $langsys->translate('{username} has been invited', null, 'Team', null, [
    'username'        => $user->name,
    'username_gender' => $user->gender, // 'male' | 'female' | 'other'
]);

लिंग पता नहीं है? 'other' को स्पष्ट रूप से पास करें — यह तटस्थ ब्रांच है। अगर यह फिर भी न दिया गया हो, तो SDK आपके लिए other ब्रांच चुन लेता है, इसलिए वाक्य फिर भी सही पढ़ा जाता है।

श्रेणियाँ

श्रेणी वाक्यांश का दायरा तय करती है, इसलिए वही शब्द अलग-अलग संदर्भों में अलग तरह से अनूदित हो सकते हैं।

// Same word, two meanings — the category disambiguates.
echo $langsys->translate('Home', null, 'Main Menu');     // es: "Inicio"
echo $langsys->translate('Home', null, 'Home repairs');  // es: "Casa"

विज़िटर का लोकेल

इसे अपनी रूटिंग या उपयोगकर्ता की सेटिंग से सेट करें, या क्लाइंट को ब्राउज़र का Accept-Language हेडर पढ़ने दें।

स्रोत देखें ↗
use LangsysSDKLocaleLocaleDetector;

// Explicit, from your own routing or the user's settings:
$langsys->setLocale('pt-br');

// Or from the browser: without setLocale(), getLocale() reads Accept-Language
// and falls back to the project's base locale.
$locale = $langsys->getLocale();

// The same detection, from a header your framework hands you:
$locale = LocaleDetector::fromAcceptLanguage($request->getHeaderLine('Accept-Language'));

आपके टेम्पलेट में

एक छोटा हेल्पर आपके व्यू को एक छोटा नाम देता है और आउटपुट को एस्केप करता है।

helpers.php
<?php
// helpers.php — one client per request, one short name in your templates.
use LangsysSDKClient;

function t(string $phrase, string $category, array $params = []): string
{
    static $langsys = null;
    $langsys ??= (new Client())->setLocale($_SESSION['locale'] ?? 'en-us');

    return htmlspecialchars($langsys->translate($phrase, null, $category, null, $params));
}

// In a template:
// <h1><?= t('Welcome to our store', 'Home') ?></h1>

सर्वर पर टोकन डिस्कवरी

राइट कुंजी के साथ, प्रोजेक्ट ने जो भी वाक्यांश पहले नहीं देखा है, वह रेंडर होते समय कतार में जोड़ दिया जाता है और रिस्पॉन्स के बाद रजिस्टर हो जाता है — विज़िटर को तुरंत आपकी बेस भाषा का टेक्स्ट दिखता है, और वाक्यांश अगले विज़िटर के लिए अनुवादित हो जाता है। रीड-ओनली कुंजी के साथ कुछ भी रजिस्टर नहीं होता और कुछ भी नहीं टूटता।

स्रोत देखें ↗
// With a write key, translate() queues every phrase the project doesn't have
// and sends them after the response, from a shutdown handler. Nothing to call.
// With a read key the queue is simply dropped — no errors.

// Or register up front, e.g. from a deploy script:
$langsys->registerPhrases([
    ['phrase' => 'Order confirmed', 'category' => 'Checkout'],
    ['phrase' => 'Copyright 2026', 'category' => 'Footer', 'translatable' => false],
]);

पूरे पेज

क्या आपके पास पेज पहले से HTML स्ट्रिंग के रूप में है? head सहित उसे एक ही बार में अनुवाद करें।

स्रोत देखें ↗
// An HTML page you already have as a string: <title>, meta and OpenGraph
// tags, and the body — phrases and content blocks — in one pass.
ob_start();
// … your app renders the page …
$html = ob_get_clean();

echo $langsys->setLocale('es-es')->translatePage($html, 'Home');

कैशिंग

स्रोत देखें ↗
// Catalogs are cached for an hour (a file cache by default; Redis or none
// via LANGSYS_CACHE_DRIVER). After editing translations in the dashboard:
$langsys->clearCache('es-es');
डॉक्स और लर्निंग सेंटर में खोजें