Internationalization in 6.0

Enonic version: 6.0
**OS:**WIndows 7 x64

I have read the given examples by link
http://xp.readthedocs.org/en/stable/reference/libraries/i18n/api/localize.html
about using standart tool for internationalization. But it somehow dont want to detect browser locale if you are not setting it manualy.

If you are trying to use

var message_multi_placeholder = i18n.localize({
key: ‘test’
});

without default properties file it return NOT_TRANSLATED

Browser have such language properties:
“Accept-Language”: “ru,en;q=0.8,uk;q=0.6”

and I have files in i18n folder:

phrases_en.properties
phrases_ru.properties

If you are setting locale manualy, like

var message_multi_placeholder = i18n.localize({
key: ‘test’,
locale: “en”
});

everything works ok.

And besides do you have a way of using those internationalization tool in thymeleaf templates, because I have found only old way of doing that throuhg the portal variable.

but it’s also not working without manualy setting locale, and I have not found any clues or docs how to set locale manualy in that case.

Hi,

You should in admin double click your Site and set the locale on it in the bottom of the form. This is then sent automatically to the localize function.

I’ve made that example work in 6.0 by also adding the “_locale” param and sending that in. Haven’t tried without yet, but if you say it doesn’t work you might need to “fetch” this in your Controller first and pass on to Thymeleaf.

Ok, I understand that, I should somehow tell to localization tool about current locale, or set that locale to site.

But could I provide auto-detect of locale without setting those locale to Site by hands? And if I can’t then can I set it programmaticaly(in case if I want to add choosing of language on main page)?

Besides, it’s working well without manualy set “_locale” if you have set it to Site before. But I am wondering about the way how can I automate that process with having only “Accept-Language”: “ru,en;q=0.8,uk;q=0.6” string, or a dropdown with languages, for example.

Hi…

You might have missed an important thing about localization. Every page created also has a language, so you would not want to “randomly” use the locale of the visitor - but use the actual locale in context of the spesific site/content.

However, should you want to force it, simply implement your own localize library doing this in your preferred way, invoking the localize function with your detected locale every time.

NB! You must create a default phrases.properties file - this is the fallback when everything else fails.

I see, I have not created default *.properties file just because of testing purposes(but I promise I will). If I have understood right the more native acrhitecture will be to create some sub-sites inside main site with contexts like “en/”, “no/” etc and apply language to them?

Something like that yes… Remember, if you are using page templates they may also contain text for a specific language - and tempaltes are reused across the entire site.

You can get the user’s browser language from the request object and check for “en” or “ru” and then pass those the the locale parameter or even set a redirect to the appropriate language page. But this might not be the best way to go about it.

Ok all that stuff looks reasonable pretty much, thanks for responding very much. The last questions will be - is there is any way to find out about current language of site. Because I have found post about that where mentioned that it will be added in 5.3.

https://discuss-3.enonic.com/t/how-do-i-get-current-language-from-a-content-or-site/105

Yes, that has been solved. When you use portal.getSite() then this returns the “language” and also the portal.getContent() will return the language of the content.

var site = portal.getSite();
var siteLanguage = site.language;

var content = portal.getContent();
var contentLanguage = content.language;
1 Like