In directory mode each language is a directory named <language>/, and
every *.php file inside it becomes a namespace. Keys are addressed as
filename.key.
languages/
en/
main.php
admin.php
tr/
main.php
admin.php
Directory mode is not the default, so you must call useDirectory() —
before the first language is loaded:
use InitPHP\Translator\Translator;
$lang = new Translator();
$lang->useDirectory()
->setDir(__DIR__ . '/languages/')
->setDefault('en');Calling
useDirectory()after a language has already been loaded throws aTranslatorException. Set the mode first.
The namespace is the file name without .php, lower-cased. So
admin.php is addressed as admin.*:
<?php
// languages/en/admin.php
return [
'dashboard' => 'Dashboard',
'menu' => [
'settings' => [
'title' => 'Settings',
],
],
];echo $lang->translate('admin.dashboard'); // "Dashboard"
echo $lang->translate('admin.menu.settings.title'); // "Settings" (deeply nested)The first segment selects the file; the remaining segments walk the nested array inside it.
$lang->change('tr');
echo $lang->translate('main.hello_world'); // value from tr/main.phpWhen a key is missing in the active language it falls back to the default language, including for nested keys — see Key resolution & fallback.
- A missing
<language>/directory throws aTranslatorException. - A directory containing no
*.phpfiles loads as an empty language: lookups simply return the key, and no exception is raised. - Any
*.phpfile that does notreturnan array throws aTranslatorException.
See File mode for the simpler one-file-per-language layout.