Localization
Localization means adapting software for different languages, regional differences and technical requirements of the target market.
When creating multilingual application, you will probably need to render the
same page or form in various languages. Nette Framework offers an interface for
translation. Just create so-called translator, which is an object
implementing Nette\Localization\ITranslator
interface. The interface has only one method – translate().
class MyTranslator implements Nette\Localization\ITranslator
{
/**
* Translates the given string.
* @param string message
* @param int plural count
* @return string
*/
public function translate($message, $count = NULL)
{
return ...;
}
}
As an implementation of translator, you can use for example GNU gettext or Zend_Translate.
Form translation
Forms and also individual form controls have a setTranslator()
method, which can be used to set the translator for them.
$translator = new MyTranslator;
$form->setTranslator($translator); // set the translator for the form
From this point on, all labels but also all error messages or select box items get translated into another language.
For individual form controls, it is still possible to set another translator
or completely turn the translation off using the NULL value:
$form->addSelect('carModel', 'Model:', $cars)
->setTranslator(NULL);
Template translation
Translator can also be set for templates. Again, by using the
setTranslator() method, for example in presenter:
function beforeRender()
{
...
$this->template->setTranslator($translator);
}
After this, all expressions within the localization macros will be translated:
<li><a href="basket">{_'Basket'}</a> {_$item}
You can use an alternative pair notation:
<li><a href="order">{_}Order{/_}</a>

