PHP Luminova: Localization Definitions and Text Translations
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().
| Format | Message in the language file | Values to pass |
|---|---|---|
| Named | Hello, {name}! New messages: {count}. | ['name' => 'Peter', 'count' => '20'] |
| Positional | Hello, %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 = enStore 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
- Full namespace:
Luminova\Components\Languages\Translator - This class implements Luminova\Interface\LazyObjectInterface.
- This class is final.
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:
| Parameter | Type | Description |
|---|---|---|
$locale | ?string | Initial locale override. Defaults to null. |
setLocale
Set the locale for subsequent lookups on this instance.
public function setLocale(string $locale): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$locale | string | Locale 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(): ?stringReturn:
?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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$lang | string | Language file context and dot-separated translation key. |
$default | ?string | Fallback text. Defaults to null. |
$placeholders | array<string,string>|array<int,string> | Named replacements or a list of printf-style formatting arguments. Defaults to []. |
Return:
string - The translation or the fallback text.
Throws:
- Luminova\Exceptions\NotFoundExceptionThe language file is unavailable or empty and the fallback is empty.
- Luminova\Exceptions\RuntimeExceptionNo usable locale is set.
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.