We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Another method for front end multi-language support

Development and Programming

biberltd's avatar
biberltd
54 posts
16 years ago
biberltd's avatar biberltd

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.

       
Brendon Carr's avatar
Brendon Carr
135 posts
16 years ago
Brendon Carr's avatar Brendon Carr

As a multilanguage user myself, I was interested to read this and eager to see the Biber Ltd. Language Switcher Extension, but I think your download link takes us to an error page. Since I don’t speak the language there, I can’t be sure.

What language is that, anyway? I’m going to take a guess and say Turkish. Am I right?

       
biberltd's avatar
biberltd
54 posts
16 years ago
biberltd's avatar biberltd

iiiuuuu…. Thanks Brendon. The system is off for testing. I totally forgot about that. I’ve moved the download into a sub-domain and updated the links above.

Just incase the new link is this.

Oh, and yes it is in Turkish, and basically saying that the system is off for maintenance. We haven’t prepared all the English translations & all the site templates yet, sorry :)

By the way the code in extension is open to a lot of improvements. We’re still in process of understanding EE way of thing things so there may be some unvital lines of code there.

The other thing that I forgot, kind of the last step missing:

In your site’s root folder you have to create an .htaccess file as also told in Alternative Method. To our experience, the best practice right now is to remove both index.php and template group name from URLs. I’m not talking about security issues etc. It’s mainly important to achive correct auto created links in EE after the above mentione modifications.

Our .httaccess file looks like this:

#Handle removal of index.php and template group from EE URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/template_group_name/?$1 [L]

Lastly, you still may need to use Find & Replace plugin to fix pagination URLs.

       
Cem Meric's avatar
Cem Meric
210 posts
15 years ago
Cem Meric's avatar Cem Meric

Nicely done Can, it works very well on your site.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
15 years ago
Lisa Wess's avatar Lisa Wess

Added this one to the wiki here

       
biberltd's avatar
biberltd
54 posts
15 years ago
biberltd's avatar biberltd

Cem thank you for the comment & Lisa thank you for adding it into wiki.

We have made couple changes. Now the extension has a settings page where the user can define a default language and a list of site-wide available languages.

Some parts of the extension needed a manual code editing - therefore required more coding experience. Now vital stuff can be set using the control panel.

The site owner still needs to follow all the steps above and in addition to those steps he/she should define site default language and all languages used by the site. These settings used to be manual entries in extension code and they were required by the extension during decision making logic. And there was no way to change them without editing the code. Now, there is a chance for that.

On the settings there are two fields named respectively

1. Available Languages Available languages are those languages that the site is using. For each language there should have been a folder defined as stated in the above instructions.

On the extension’s setting page site admin needs to enter all the site-wide used language details according to this format:

i.e. deutsch,de|turkish,tr|english,en

Language names and their respective codes are sepereated by a comma. Each language and language code pair is seperated with | character.

The order is not important; however, language names and codes must match to those language names and codes as defined in path.php

2. Default Language Default language defines the default language that site will be shown if there is no cookie set for the visitor.

There must be only one language, language code pair. Similar to above format this language and language code pair must be seperated with comma and language name must be written first following by the code.

Once again the language name and code must match to those stated in path.php.


Here is the v1.1.0 of extension.

       
biberltd's avatar
biberltd
54 posts
15 years ago
biberltd's avatar biberltd

one more note thanks to Ross Curry. For this extension only replace all occurences of country_code with language_code.

This method is somewhat the evolved version of alernative method and some of the variable names may have been renamed. The above mentionet country_code is one of them. Although I doubt there may be few others.

       
biberltd's avatar
biberltd
54 posts
15 years ago
biberltd's avatar biberltd

Figured out a bug-like problem. The extension was conflicting with Brandon Kelly’s FieldFrame module. I was debugging and then I realized that the problem was with the addition of the hook. Since I am still learning the core of EE, such things unfortunately may happen. Anyhow, if the language switcher causes any conflicts with other extensions here is a possible fix: You need to use the blow updated step 7 during installation of this extension:

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.
         */
        if ($EXT->active_hook('language_fetch_start') === TRUE)
        {
            $edata = $EXT->call_extension('language_fetch_start');
            if ($EXT->end_script === TRUE) return;
        }
        /*
        /* -------------------------------------------*/

Notice the if statment! Before - the extension was called every time regardless which hook was called. Not it is fixed.


oops and I forgot to include the updated extension… here is v1.1.1

       
Brian Litzinger's avatar
Brian Litzinger
693 posts
15 years ago
Brian Litzinger's avatar Brian Litzinger

It should be possible to do this without a core hack.

Add a hook for sessions_start, and call extend_language_class.

function extend_language_class()
{
        global $LANG;
        $LANG = new My_Language;
}

In your extension file, add a new class, and add your own copy of fetch_language_file.

class My_Language extends Language
{
function fetch_language_file($which = '', $package = '')
    {
        global $IN, $OUT, $LANG, $SESS, $PREFS, $FNS, $EXT;

// more code

// add your hook here

This is the same method I used for Custom System Messages. It’s just extending the main Language file, replacing fetch_language_file with your version and updating the global $LANG variable.

       
biberltd's avatar
biberltd
54 posts
15 years ago
biberltd's avatar biberltd

Hmmm nice 😊 I still find that hook useful but as you say there is no need for hack.

Thanks, Can Berkol

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.