PHP langsys/langsys-php
Temel PHP SDK: düz PHP, bir framework, bir worker ya da bir CLI olsun, her PHP uygulaması için tek bir istemci. Kodunuzdaki ifade hem arama anahtarı hem de temel dildeki varsayılan metindir; anahtar dosyası da ayıklama adımı da yoktur. Çeviri sunucuda yapılır, bu yüzden sayfa zaten yerelleştirilmiş olarak gelir.
Bu bir arka uç SDK'sı olduğundan bu sayfada tarayıcı içi deneme alanı yok; tarayıcı PHP çalıştıramaz. Laravel mi kullanıyorsunuz? Laravel paketi bu istemciyi bir Blade yönergesi, ara katman yazılımı ve bir facade ile sarmalar. Tam başvuru: README.
Bir yapay zeka kodlama ajanı mı kullanıyorsunuz? Ona şu istemi verin
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. Kurulum
composer require langsys/langsys-php PHP 7.4 veya üzeri; curl, json ve intl eklentileriyle birlikte — intl, çoğulları işleyen ve sayıları ile tarihleri her yerel ayar için biçimlendiren eklentidir.
# 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 Henüz projeniz veya anahtarlarınız yok mu? Hızlı başlangıç ikisini de iki dakikada sağlar.
İstemci
İstek başına bir istemci. Kimlik bilgilerini ortamdan okur ve her yerel ayarın kataloğunu önbelleğe alır; böylece bir sayfayı çevirmek en fazla bir API çağrısına mal olur.
<?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'); Çevir
translate() ikinci parametre olarak yerel ayarı, üçüncü parametre olarak kategoriyi alır. İstemcinin geçerli yerel ayarını kullanmak için yerel ayar yerine null verin.
// 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. Cümledeki değerler
Yer tutucular, her Langsys SDK'sının kullandığı aynı biçim olan {name} ile yazılır; böylece tek bir katalog hem PHP arka ucunda hem de JS ön yüzünde aynı şekilde görüntülenir.
// 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. Çoğullar
Cümleyi düz yazın: Langsys, çeviri sırasında her dilin çoğul biçimlerini üretir ve SDK sayınıza uyan biçimi gösterir.
// 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'in oluşturduğu çoğul biçimleri PHP'ye langsys/langsys-php 1.3.2 sürümünden itibaren ulaşır; önceki sürümler, 1 dahil her sayı için çoğul biçimi kullanır.
Cinsiyet uyumu
İspanyolca, Fransızca veya Rusça {username} has been invited ifadesini, söz konusu kişiye göre çekimler. Langsys bu dalları çeviri sırasında, çoğulları oluşturduğu gibi oluşturur; argümanın adı değişkenin adına _gender eklenerek verilir: male, female veya other. Bildiğiniz her durumda iletin. Nasıl çalışır ve buna ihtiyaç duyan ifadeler nasıl bulunur →
// 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'
]); Cinsiyeti bilmiyor musunuz? 'other' değerini açıkça iletin — bu, nötr daldır. Yine de eksikse SDK sizin yerinize other dalını kullanır, böylece cümle yine doğru okunur.
Kategoriler
Kategori bir ifadenin kapsamını belirler; böylece aynı sözcükler farklı bağlamlarda farklı çevrilebilir.
// 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" Ziyaretçinin dili
Bunu kendi yönlendirmenizden veya kullanıcının ayarlarından belirleyin ya da istemcinin tarayıcının Accept-Language başlığını okumasına izin verin.
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')); Şablonlarınızda
Küçük bir yardımcı, görünümlerinize kısa bir ad kazandırır ve çıktıyı kaçışlar.
<?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> Sunucuda token keşfi
Yazma anahtarıyla, projenin görmediği her ifade siz oluştururken kuyruğa alınır ve yanıttan sonra kaydedilir; ziyaretçi temel dildeki metninizi hemen görür, ifade ise bir sonraki ziyaretçi için çevrilir. Salt okunur anahtarla hiçbir şey kaydedilmez ve hiçbir şey bozulmaz.
// 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],
]); Sayfaların tamamı
Sayfa elinizde zaten bir HTML dizesi olarak mı var? Head dahil tek seferde çevirin.
// 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'); Önbellekleme
// 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');