, (*1)
, (*2)
DEMO, (*3)
LiveTranslator
LiveTranslator is tool for Nette Framework., (*4)
LiveTranslator enables simple and user friendly localization of your web, by letting you to translate your texts
via panel in debug bar. Works with the Nette 2., (*5)
LiveTranslator is forked from NetteTranslator, uses its robust
parts (especially TranslationPanel)
and changes other, mainly the storage where translations are saved., (*6)
Installation
{
"require": {
"vladahejda/livetranslator": "~1.0"
},
"minimum-stability": "RC"
}
Then load classes via autoloader (composer autoloading
or Nette RobotLoader)., (*7)
Usage
To launch the translator follow these steps:, (*8)
1. prepare storage
- I am using Nette Database
Execute SQL script in LiveTranslator/Storage/NetteDatabase.createTable.sql
(or its namespaced version,
see using namespaces) at your database., (*9)
Open your configuration file and add service:, (*10)
services:
translatorStorage: LiveTranslator\Storage\NetteDatabase(localization_text, localization)
You can rename tables in SQL script (use the same names in config file)., (*11)
Dibi storage is in progress. You can write your own storage. See below., (*12)
You can store translations into plaintext file. Just add following service into your config
and choose persistent and write-accessible (existing) directory:, (*13)
services:
translatorStorage: LiveTranslator\Storage\File(%appDir%/../data/localization)
- I want to save translations elsewhere
Look at the interface LiveTranslator\ITranslatorStorage
and implement it to write your own storage., (*14)
Then add storage into your configuration file as a service., (*15)
2. add LiveTranslator and Panel service
Into your config file add two more services LiveTranslator\Translator
and LiveTranslator\Panel
and define
the default language (it is language which in your web is written basically):, (*16)
nette:
debugger:
bar:
- LiveTranslator\Panel
services:
translator: LiveTranslator\Translator(en)
translatorPanel: LiveTranslator\Panel
3. set up your BasePresenter
Inject LiveTranslator, set current language and give translator to template and forms:, (*17)
class BasePresenter extends \Nette\Application\UI\Presenter
{
/** @var string @persistent */
public $lang = 'en';
/** @var \LiveTranslator\Translator @inject */
public $translator;
// since Nette 2.1 you can omit this method
public function injectTranslator(\LiveTranslator\Translator $translator)
{
$this->translator = $translator;
}
public function startup()
{
parent::startup();
$this->translator->setCurrentLang($this->lang);
}
protected function createTemplate($class = NULL)
{
$template = parent::createTemplate($class);
$template->setTranslator($this->translator);
return $template;
}
// to have translated even forms add this method too
protected function createComponent($name)
{
$component = parent::createComponent($name);
if ($component instanceof \Nette\Forms\Form) {
$component->setTranslator($this->translator);
}
return $component;
}
}
4. mark texts for translation
In presenters call $this->translator->translate('text to translate')
, in latte use underscore macro
{_ 'text to translate'}
, (*18)
Done. In development mode look at your debugbar. You can see panel "translations". Browse your web and texts will
occurs in panel. Switch languages and translate!, (*19)
Advanced
If you want to use your translator fully, there is some more stuff you would do., (*20)
Say translator which languages you use
Call function $translator->setAvailableLanguages()
and give the array of languages that your web is available in.
Then set the name of presenter language persistent parameter ($translator->setPresenterLanguageParam()
)., (*21)
Better way of this is setting it in config:, (*22)
translator:
class: LiveTranslator\Translator(en)
setup:
- setAvailableLanguages([en, de, fr])
- setPresenterLanguageParam(lang)
Now your panel will display links to switch the languages!, (*23)
Use skills of sprintf
If you give more arguments to translate method, it will be handed to php function
sprintf., (*24)
That means that $translator->translate('Call me %s.', 'Johan')
results in "Call me Johan", whereas
"Johan" will not be translated., (*25)
It can be used in latte too., (*26)
Translate plurals (1 apple → 2 apples)
You can say what plural-form each language uses via setAvailableLanguages
, this way:, (*27)
setup:
- setAvailableLanguages([
en: "nplurals=2; plural=(n==1) ? 0 : 1;",
cz: "nplurals=3; plural=((n==1) ? 0 : (n>=2 && n<=4 ? 1 : 2));",
])
(to understand this see plural forms), (*28)
Then the panel will let you translate the text even in plural. More you need to do is to give plural variants
of the default language to the translator, in array. And the number. Example:
$translator->translate( array( 'There is %d apple', 'There are %d apples' ), 3 )
or in latte: {_ ['There is %d apple', 'There are %d apples'], 3}
., (*29)
Using namespaces
When there is huge amount of texts at your web, it would be good to sort them somehow. Just give the namespace
to the translator dependent for example on module.
$translator->setNamespace('products')
, (*30)
Panel will separate all texts from another namespaces., (*31)
And now, enjoy., (*32)
Authors
(alphabetic order), (*33)
- Josef Kufner (jk@frozen-doe.net)
- Miroslav Paulík (https://github.com/castamir)
- Roman Sklenář (http://romansklenar.cz)
- Miroslav Smetana
- Jan Smitka
- Patrik Votoček (patrik@votocek.cz)
- Tomáš Votruba (tomas.vot@gmail.com)
- Václav Vrbka (gmvasek@php-info.cz)
- Vladislav Hejda
Under New BSD License, (*34)