How to start developing multilanguage website with Nette Framework and Kdyby\Translation
Last updated
Was this helpful?
Last updated
Was this helpful?
Many people on Nette forum asked how to develop a multilingual website. This was my question as well and I'd like to share my solution with you. Filip Prochazka has developed , an adapter for Symfony\Transaltion for Nette Framework, and has lots of super truper small features which you will love.
Here is a four minute long videocast, an introduction to how easy it is to start using Kdyby\Translation.
1) Install Nette web-project: $ composer create-project nette/web-project
2) Install Kdyby/Translation: $ composer require kdyby/translation
in config.neon
we will add an extension to enable Kdyby\Translation:
In our BasePresenter we will add the variables $locale
and $tranlator
. Using [Dependency Injection | doc:di] mechanism we get an injected Kdyby\Translation\Translator service. (In my example, I don't use any BasePresenter, instead I only use the HomepagePresenter).
The variable $locale
is and will now be automatically held in the url.
Update the original router definition in RouterFactory.php
(In the video, locale defaults to cs.)
If we want a specific translation with localization, e.g. French speaking Canadians, we will use fr_CA
.
Keeping both the language and the country in the culture is necessary because you may have a different French translation for users from France, Belgium or Canada, and a different Spanish content for users from Spain or Mexico. The language is a code of two lowercase characters, according to the ISO 639-1 standard (for instance, en for English). The country is a code of two uppercase characters, according to the ISO 3166-1 standard (for instance, GB for Great Britain).
ui.cs_CZ.neon
ui.en_US.neon
In our template, we will use the macro {_ ui.title}
for putting translated phrase.
It's really easy to start.
We will add the two files ui.cs_CZ.neon
and ui.en_US.neon
to the folder app/lang
. Here ui
is the name of our dictionary and cs_CZ
is a combination of language and state. ( and )
The default syntax is Neon, you can read more about its amazing syntax .
For more information read the .