l10n translator for Nette with simple panel for Tracy
l10nNetteTranslator is available on Packagist.org, just add the dependency to your composer.json.
{
"require" : {
"zemistr/l10n-nette-translator": "1.*"
}
}
or run Composer command:
php composer.phar require zemistr/l10n-nette-translator
<?php
require('src/l10nNetteTranslator.php');
Just add following code into the config.neon:
extensions:
translator: l10nNetteTranslator\ApplicationDI\Extension
translator:
# languages are required
languages:
-
lang: l10n\Language\CzechLanguage # must implements l10n\Language\ILanguage
plural: l10n\Plural\PluralRule8 # must implements l10n\Plural\IPlural
-
lang: l10n\Language\SlovakLanguage # if language implements l10n\Plural\IPlural, you can ignore plural section
-
lang: l10n\Language\EnglishLanguage
default: true # if is not set, the first language will be set as default
# storage is optional
storage: @translator_simple_nette_storage # must implements l10nNetteTranslator\Storage\IStorage
services:
# You can use any storage implements Nette\Caching\IStorage
translator_nette_storage:
class: Nette\Caching\Storages\FileStorage(%appDir%/Texts) # Texts will be saved in %appDir%/Texts as file named by ISO 639-1
autowired: false
translator_simple_nette_storage: l10nNetteTranslator\Storage\SimpleNetteStorage(@translator_nette_storage)
and add into presenter this code:
/** @var \l10nNetteTranslator\Translator */
protected $translator;
public function injectTranslator(\l10nNetteTranslator\Translator $translator) {
$this->translator = $translator;
}
protected function beforeRender() {
$this->template->setTranslator($this->translator);
}
class XxxPresenter extends \Nette\Application\UI\Presenter {
public function createComponentForm() {
$form = new Form();
$form->setTranslator($this->translator);
...
return $form;
}
}
Example usage in Latte
// users -> singular => %n% person
// users -> plural 1 => %n% people
{_'users'} // 1 person
{_'users', 0} // 0 people
{_'users', 50} // 50 people
----
// user -> singular => I am %firstname% %lastname%
{_'user', ['%firstname%' => 'John','%lastname%' => 'Doe']} // I am John Doe
{_'user', 1, ['%firstname%' => 'John','%lastname%' => 'Doe']} // I am John Doe
class XxxPresenter extends \Nette\Application\UI\Presenter {
public function actionDefault() {
// argument must be ISO 639-1 code
$this->translator->setActiveLanguageCode('cs');
}
}
(c) Martin Zeman (Zemistr), 2015 (http://zemistr.eu)