Luminova Framework

PHP Luminova: Localization Definitions and Text Translations

Last updated: 2026-11-28 20:57:12

Translate application labels and messages with locale-specific PHP language files. Set a default locale, look up translations by key, and insert dynamic values with named or positional placeholders.

Give each message a stable key and write its translation in each language you support. Luminova uses the locale, such as en for English or fr for French, to choose which file to read. Your controllers and views can use the same key regardless of the language shown to the user.


Use Translations

Define Translations in Language Files

Create one PHP file per message group and locale in app/Languages/. Name it Context.locale.php, where the context groups related messages. For example, Users.en.php and Users.fr.php contain English and French messages for the Users context.

Keep the keys and placeholder names consistent across locales. For example, Users.password.reset selects the Users file for the active locale and looks for reset inside the password array.

English · app/Languages/Users.en.php

return [
    'notify' => 'Hello, {name}! New messages: {count}.',
    'password' => [
        'reset' => 'Enter your email address to reset your password.',
    ],
];

French · app/Languages/Users.fr.php

return [
    'notify' => 'Bonjour, {name} ! Nouveaux messages : {count}.',
    'password' => [
        'reset' => 'Saisissez votre adresse e-mail pour réinitialiser votre mot de passe.',
    ],
];

NoteEach language file must return an array of translations.


Look Up a Translation

With app.locale = en, lang() reads Users.en.php for the key Users.notify. The keys in the placeholders array match the names inside braces in the message. Pass locale: 'fr' to select the French file for a single lookup. See the lang() helper reference.

use function Luminova\Funcs\lang;

echo lang(
    'Users.notify',
    placeholders: ['name' => 'Peter', 'count' => '20']
);
// Hello, Peter! New messages: 20.

Format Messages with Placeholders

Placeholders allow you to insert values such as a user’s name or message count into a translation. Match the format in the language file to the array you pass to lang() or Translator::get().

FormatMessage in the language fileValues to pass
NamedHello, {name}! New messages: {count}.['name' => 'Peter', 'count' => '20']
PositionalHello, %s! New messages: %d.['Peter', '20']

Named placeholders match the array keys, so their order can vary across languages. Positional placeholders use vsprintf(). Use %s for text and %d for integers. If a translation needs to reorder the values, number the specifiers. For example, %2$d nouveaux messages pour %1$s. produces 20 nouveaux messages pour Peter. with the same list.


Set a Default Locale and Honor User Preferences

Set app.locale in .env to choose the language your application uses by default.

app.locale = en

Store the user’s preferred locale in a session or cookie if you offer a language switcher. On each request, match that value against your supported locales, then call locale($preferredLocale) before looking up translations. This sets the locale for the current request. Setting persist: true writes the application-wide default to .env. See the locale() helper reference.


Class Definition

Create a Translator with an explicit locale when preparing text for a different audience. For example, use new Translator('fr') to prepare an email in French while the current request uses English.


Methods

__construct

Create a translator with an optional locale. With null, the instance reads the application locale on lookup.

public function __construct(private ?string $locale = null)

Parameters:

ParameterTypeDescription
$locale?stringInitial locale override. Defaults to null.

setLocale

Set the locale for subsequent lookups on this instance.

public function setLocale(string $locale): self

Parameters:

ParameterTypeDescription
$localestringLocale code matching the language file suffix.

Return:

self - The current translator instance.


getLocale

Read the locale selected explicitly for this instance. To read the application’s current locale, call locale() without arguments.

public function getLocale(): ?string

Return:

?string - The stored locale, or an empty string when no override is set.


get

Look up a message using the instance’s locale. Use a key such as Users.notify and pass named replacements or positional values as shown in the usage examples.

public function get(string $lang, ?string $default = null, array $placeholders = []): string

Parameters:

ParameterTypeDescription
$langstringLanguage file context and dot-separated translation key.
$default?stringFallback text. Defaults to null.
$placeholdersarray<string,string>&#124;array<int,string>Named replacements or a list of printf-style formatting arguments. Defaults to [].

Return:

string - The translation or the fallback text.

Throws:

Example:

use Luminova\Components\Languages\Translator;

$translator = new Translator('fr');
echo $translator->get(
    'Users.notify',
    placeholders: ['name' => 'Peter', 'count' => '20']
);
// Bonjour, Peter ! Nouveaux messages : 20.