PHPackages                             genai/i18n - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. genai/i18n

ActiveLibrary[Localization &amp; i18n](/categories/localization)

genai/i18n
==========

Localization for the GenAI stack — Spring-style message keys compiled from per-locale .ini catalogs into a reflection-free Cache\\Messages, a Translator bean, a global \_\_() view helper, and a LocaleInterceptor that resolves the request locale (?lang / session / Accept-Language). PHP 5.3-safe at runtime.

v1.0.1(1mo ago)079↓45.8%Apache-2.0PHPPHP &gt;=5.3.0CI passing

Since Jul 3Pushed 1mo agoCompare

[ Source](https://github.com/GenAIIO/php-i18n)[ Packagist](https://packagist.org/packages/genai/i18n)[ RSS](/packages/genai-i18n/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

genai/i18n
==========

[](#genaii18n)

Localization for the GenAI stack — **Spring-style message keys**, compiled the same way everything else here is: per-locale `.ini` catalogs are baked at build time into a reflection-free `Cache\Messages`, read at runtime by a `Translator`. PHP 5.3.29-safe at runtime.

How it works
------------

[](#how-it-works)

```
config/messages/en.ini          config/messages/vi.ini
  login.cta  = "Log in"            login.cta  = "Đăng nhập"
  hello.user = "Hi, :name!"        hello.user = "Chào :name!"

```

Build (`composer compile`) → **`Cache\Messages::all()`** = `array(locale => array(key => message))`. Runtime → the `Translator` bean (auto-wired) looks up the current locale, falls back to the fallback locale, then to the key itself, and interpolates `:name` / `{name}` placeholders.

Use it
------

[](#use-it)

**1. Configure** (`app.ini`, optional):

```
[i18n]
default   = en
fallback  = en
available = en,vi
param     = lang        ; ?lang=vi switches
```

**2. Translate** — anywhere, via the global helper (degrades to the key if unwired):

```
echo __('login.cta');                       // "Log in"
echo __('hello.user', array('name' => $n)); // "Hi, Linh!"
echo __n('games.count', $n);                // "1 game|:count games"
```

In templates: ``. For non-view code you can also inject the `Translator` bean and call `->trans()` / `->choice()`.

**3. Switch locales per request** — enable the interceptor with a thin subclass (early, so the locale is set before rendering):

```
#[Intercept(order: -20)]
class LocaleGuard extends \GenAI\I18n\Interceptor\LocaleInterceptor {}
```

It resolves the locale in this order: `?lang=` (then remembers it in the session) → session → `Accept-Language` → default. `
