Is there a way to force a specific language file on session on a template?
Example, the Wiki or forum templates use variables like the following:
{lang:navigation}
The problem is that unless someone logs in to EE, it will not display the language for that user and use the default language for the site.
What if I have a different template for languages? Isn’t there a way to force the template to load the variables from a different language manually?
Example: {lang_italian:navigation}
And it would load the Italian language files?
Or better. Set it on the template like: {set_lang:italian]
And then all the variables on that specific template would load in Italian?
Of course if the user then clicks a link and goes away it would use the default language for the visitor session but all I want is just to manually present a specific template in a different language if they go visitors go directly to that template file. I had this question for years now. Since all modules could benefit from this. I don’t see a point on having language files if you can’t use them manually and they can only be loaded with a setting on the user account. This is fine for people that are members but I would like to force a language on visitors as well depending if they visit a template that has a specific variable.
Is this possible?
Hi Nibb,
Thanks for bringing this up. When you get into language localization and multi-lingual as a whole (specifically in templates, add-ons, etc and the flexibility EE offers) things start getting a bit more complicated as a whole. The language files we’re really designed to support multi-lingual in the Control Panel.
I would recommend taking a peek at Transcribe and Publisher for more robust multi-lingual options.
I don’t believe it’s possible to pull the language files into the templates from the core. However it could probably be done with a fairly simple plugin.
I completely understand and I already use a plugin for multi language that lets me add different variables depending on the locale from the user. That works perfectly fine and there are plenty of solutions for multi language sites, but this works only for logged users.
What I tried to achieve is actually very, very simple.
I’m not trying to overwrite the user language or the user language setting, but only specify what language to use for that specific page load.
Let me try to explain the different scenarios.
You have 2 templates, one for each language. You load the text strings manually. It works because they are hard coded into the template. You just send the user to the URL you want for each language, you have separated sites for languages.
Or, you have them with some plugin, and they are loaded from the database depending on the user language. This is how it works now for EE.
Now, let me make another example, you have the wiki module or the forum module installed. It will display by default in English assuming you have that as default. This means the wiki or forum displays in English for everyone, until they log in, then it will use the proper user language from the user account.
But you can also hard code them the variables. Nothing stops you from loading the same forum or the same wiki module with a different template and specify a different theme. Now we can leave the default theme as it is, English, but load a different theme for a different language. Now we replace all the lang variables in that theme for the language. Same as above, I just send the visitor to a forum that loads on a different template, each template loads a different theme. I then replace the dynamic variables in that theme with my localized text (hardcoded)
The result is that you can link the same module for your different language file and present it to visitors in the language you are using. Now, this is just the initial page.
This is a bit annoying and requires more work because you need to maintain a different theme for each language, in particular because the language files already exists, except they can only be loaded with a session for existing logged users. It’s annoying because you would need to maintain a different theme for each language instead of just loading the text from the locale you already have.
This could be solved every simple. Just like you can specify on the forum or wiki module to load a specific theme using EE parameters, also specify the language as a parameter. This would then load the same forum and same theme, but replace all the lang variables with the locale file from EE, this means you don’t need to create a separate theme anymore for each module. And this would work for the login page, registration, etc.
I have another system that does this. You can for example add to the URL &language=english
And the visitor will use that language. Otherwise, it will use the default from the system. My idea would be something similar but using EE parameters inside a template. There is a parameter to specify a theme, but not a language.
Have a variable, just like the theme one, that lets you specify what language file to use when that template file is executed. Now, I have no idea if EE uses sessions for anonymous visitors, if yes, that setting would be remembered only for that visitor until the session expires, unless the parameters is not loaded, or for everyone else it would use the default as usual, default is only overwritten for that visitor if it’s loaded in a template. If visitors don’t create sessions, no problem, it could then work only for that specific page load but not further on.
The trick here is having a way to tell EE to load the language you want instead of the default. Expression Engine can absolutely do this with a theme, so I’m not sure why this can’t be done equally for a language file. In the end in both cases you are just telling the software what internal files you want to use.
This would be extremely useful for people that have EE in different languages.
You can load a different lang file even when The user isn’t logged in but you have to do it through an extension hook, probably core_boot. Here is an example of how Publisher does it:
// Assuming german language pack is installed
ee()->config->set_item('deft_lang', 'german');
// Just an insurance policy, no telling what a 3rd party add-on might do to this object.
if (isset(ee()->session) && isset(ee()->session->userdata)) {
ee()->session->userdata['language'] = 'german';
}
// Force a new load of the core language file now that the language is set.
ee()->lang->loadfile('core');
Setting the config item and that loadfile call are whats important.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.