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 ...;
    }
}

Nette doesn't provide default implementation of ITranslator. However, we can choose from various solutions, for example on Componette.

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>