Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.
- PHP >= 8.3
- Nette Framework
- Composer
composer require drago-ex/translator
Register the DI extension in your NEON configuration. You must provide a base directory for translation files.
extensions:
translator: Drago\Localization\DI\TranslatorExtension(translateDir: %appDir%/locale)translator:
moduleLocaleDir: %appDir%/modules/Blog/locale- The base directory is always loaded first
- The optional module directory is loaded second
- If translation keys collide, later directories override earlier ones
Translation files must be named by language code:
cs.neon
en.neon
"Hello, world!": "Hello, world!"Add the TranslatorAdapter trait to your presenter:
use Drago\Localization\TranslatorAdapter;The trait provides:
- persistent language parameter ($lang)
- automatic translator initialization
- template integration
You can access the currently set language using the following property:
$this->lang;To get the initialized translator for the current language:
$this->getTranslator()The translator is automatically registered in templates. Example usage in Latte:
{_"Hello, world!"}
{$label|translate}To enable translations in forms, set the translator explicitly:
$form->setTranslator($this->getTranslator());To support language prefixes, configure your routes accordingly:
$router->addRoute('[<lang=en cs|en>/]<presenter>/<action>', 'Presenter:action');You can switch languages by passing the lang parameter:
<a n:href="this, lang => cs">Czech</a>
<a n:href="this, lang => en">English</a>- Translator loads translations lazily on first use
- Translations are loaded once per request
- Missing keys return the original message