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.
	public function translate($message, ...$parameters): string
		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)

Template Translation

Translator can also be set for templates. Again, by using the setTranslator() method, for example in presenter:

protected function beforeRender(): void

After this, all expressions within the localization tags will be translated:

<li><a href="basket">{_'Basket'}</a> {_$item}

You can use an alternative pair notation:

<li><a href="order">{_}Order{/_}</a>