Manage Nette app translations on per namespace basis.
Translations manager for Nette framework using contributte/translation. It allows you to have your translation files located where they are really used (e.g. next to a component or a model).
No more global translations mess! 💪
composer require wavevision/namespace-translator
Register required extensions in your project config:
extensions: translation: Contributte\Translation\DI\TranslationExtension namespaceTranslator: Wavevision\NamespaceTranslator\DI\Extension
You can configure
namespaceTranslator as follows (default values):
namespaceTranslator: dirNames: # names of dirs in which namespace translations will reside - translations - Translations loaders: # namespace translations loaders neon: Wavevision\NamespaceTranslator\Loaders\Neon php: Wavevision\NamespaceTranslator\Loaders\TranslationClass
Note: Refer to Contributte docs for further info about configuring
With this setup, you can start managing your translations like a boss 🤵.
The best thing is the translator keeps full backwards compatibility with
so you can still use your translations as you are used to and migrate to namespaces step-by-step.
Any translation not found by namespace translator will fallback to
Your components (or presenters) can use
Make sure your component has
The trait will provide your component class / template with
$translator property / variable.
The translator will look for resources in configured dir names inside component's namespace.
translatemacro in component templates will, of course, work too.
Even your services can use the translator. Simply use
Make sure your service is registered with
inject: true in your config.
After that, it works the same as with your components.
There are two resource loaders included by default:
Using PHP classes is useful when you want to refer to your translations using constants so changes in your resources get propagated throughout the whole project.
Classes containing translations must implement
You can also create and register your own loader, just make sure it implements
See tests for example app implementation.