Well, this isn’t entirely an original solution - more like an enhanced solution.
As biber Ltd. we were checking some of the solutions discussed in EE forums and wiki. At the end, we’ve decided to go with this solution Multi Language Site Alternative.
We’ve chosen that method because of the elegant URL structure. However, during implementation we’ve encountered several issues.
Main issue was with core language files. When users enter to http://www.site.com/language_code/ weblog entries appeared in requested language. However, system languages i.e. day or month names were used to appear in prev. selected language. For those to get translated into requested language, the user had to refresh the page.
Second main issue was about default language. Everything is fine when users enter site using the URL http://www.site.com/language_code/. But what if the user re-visits site using this URL http://www.site.com/. As is the method was setting the language to system defaults. In real life users expect the site to remember their language preference.
So we decided to study the Multi Language Site Alternative method and elaborate on it a bit more.
To solve the first issue, we’ve seen that there is a neeed of new hook in core language file [there may be other and better solutions].
To solve the second issue, we’ve made a complete new extension using some of the logic in Language Switcher Extension.
Also with this new solution we do not need the Template Language Plugin anymore.
Now that you get the idea; here is the complete step-by-step solution:
1. Think twice:
This solution is a bit advanced, includes a core hack. So you may encounter some errors if you are not comfortable with PHP or ExpressionEngine. Also you may have issues in future Expression engine updates.
One more thing, we have tested on only with servers suporing PHP5 and not PHP4.
- Backup your database.
- Backup your index.php located in root folder of your EE installation.
- Backup your path.php located in root folder of your EE installation.
- Backup your core.language.php located in “core” folder of your EE installation’s system folder.
2. Don’t worry:
Our site is in designing process. We are almost done with it. Don’t worry, that we don’t have a site up and running doesn’t mean we have not tested the solution. We have tested the solution both on Windows and Linux environments using Apache and PHP5.
3. Download our extension:
Now download Biber Ltd. Language Switcher Extension. Don’t do anying with it yet.
4. Follow the instructions at Multi Language Site Alternative:
Now, go to Multi Language Site Alternative and follow the steps 1 through 5.
5. Modify path.php in your installation’s root folder:
Using a text editor of your choice open the path.php located in your Expression Engine installation’s root folder. Select line 21 through 22 and replace it with this:
$global_vars = array(
'set_language' => 'no'
); // This array must be associative
Basically, you are adding a new global variable named set_language with the value set to no.
[This way we let our extension understand that user has typed in http://www.site_name.com in the browser’s URL locater. Therefore our extension will first try to automagically assign user’s (visitor’s) preferred language from his/her last visit. If there is no historical data about user, then the system default language will be used.]
6. Add few addional globals to fetch_language:
Now, go and open the “core” in your EE installation’s system folder. There you’ll see a file called core.lanuage.php. Open that file.
In line 51, right after fetch_language function decleration you’ll see this line:
global $IN, $OUT, $LANG, $SESS, $PREFS, $FNS;
replace it with this line
global $IN, $OUT, $LANG, $SESS, $PREFS, $FNS, $DB, $EXT;
Basically we are adding the needed globals into this function.
7. Add a new hook in fetch_language:
Right after above mentioned line, add this code:
/* -------------------------------------------
* 'language_fetch_start' hook.
* - Do all you want before language fetch routine
* - Added to EE 1.6.7 core | Modified by Can Berkol - biber Ltd.
*/
$edata = $EXT->call_extension('language_fetch_start');
if ($EXT->end_script === TRUE) return;
/*
/* -------------------------------------------*/
As you can see we defined a new hook named language_fetch_start. To solve the first issue we have encountered we did some test and found to have such a hook that fires before the language fetch routine will be the best way to take control over how the language files are loaded.
8. Upload & Extract:
Upload and extract Biber Ltd. Language Switcher Extension into you installaton’s extension folder.
9. Activate Extension:
Login to your admin panel, go to Administration / Utilities / Extension Manager and activate the extension.
10. Test:
Test your code and shoot any questistons you have.
Cheers,
Can Berkol
biber Ltd.
