Welcome Guest, Not a member yet? Register   Sign In
Easier Multiple Language Support - will this work?
#1

[eluser]haydenp[/eluser]
I must stress, I am VERY new to CI.

I'm looking to use it on a project but would like to get all my ducks in order before committing. One feature I am testing is it adaptability to Language Support. I have searched extensively on the forums and noted a few solutions – all of which seem a little long winded or needing major core file changes which I think should be avoided where possible for security reasons and maintenance when updating CI versions etc.

The URI_Language_Identifier method looked okay but I am not overly keen to maintain the user language in the URL and as such I searched for any options using sessions. In the end I started tinkering around with a few ideas and came up with the following solution (maybe its not?).

If anyone wished to comment or shoot it down please feel free. BUT be kind co's I've only had about 5 hours worth of CI under my belt so I’m not up to speed on CI core functionality etc. As such, I’m putting this forward as more of a "base concept" to see if anyone with more CI experience can give feedback whether or not this method would work, could be improved or built on.

In Short: It uses a library that is loaded by default. The libraries construct calls a method that:

1. Checks for an iso language value in the current url segments
2. If a language segment is found, it check if it is a supported language
3. If it is a supported language and is not the same as the language set in the users session then it sets the session to the new language and updates the config language

I think it's a pretty straight forward method, easy to implement, with minimal code and no changes to any core files etc. No redirects or url language identifier maintenance or anything of the sort … just a simple solution that works?

Let me know what you think!


CONFIG

application/config/config.php

Code:
$config['language'] = "english";

This remains the same, the default language of "english"

AUTOLOAD

application/config/autoload.php

Code:
$autoload['libraries'] = array('database', 'session', 'form_validation', 'my_language_selection_test');

Here you add your language selection library class

LIBRARIES

application/libraries/my_language_selection_test.php

Add your library class to the libraries folder and the basic code below was my test … it is REALLY crude coding as I'm just testing but it looks to work very well for me.
On every page load a quick check is done to see if the user has chosen a different language

To change to "portuguese" the url would be something along the lines of:

http://www.mywebsite.com/admin/registration/pt/

Having "pt" in the url is a once off, no need to maintain it. Once the session is set you can use it in your views or anywhere else you need language support.


Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class my_language_selection_test
{

    public function __construct()
    {
        $this->check_language();    
    }
    
    public function check_language()
    {    
        global $RTR;
                
        $CI =& get_instance();
        
        // Set you supported languages, ensure that these exists in your language folder
        $supported_languages = array('en' => 'english', 'pt' => 'portuguese');
        
        // Default the user language to NULL
        // Get the current url segments (in an array)
        $user_lang = NULL;
        $segs = $RTR->uri->segment_array();
                
        // Loop through the segments array check if any match against a supported language
        // if a match is found, the user has selected a supported language set the user_lang
        foreach($segs as $key => $iso_code)
        {
            if(array_key_exists($iso_code, $supported_languages))
            {
                $user_lang = $supported_languages[$iso_code];
            }    
        }    
        
        // Check if user_lang is set and does not match the current user_language held in the session
        // If both the above are TRUE
        // 1. Set the user_language session to the language the user has chosen
        // 2. Reset the config language to the language the user has chosen        
        if(($user_lang != NULL) && ($CI->session->userdata('user_language') != $user_lang))
        {
            
            $data['user_language'] = $user_lang;
            $CI->session->set_userdata($data);
            
            echo "About to set the config language to: " . $CI->session->userdata('user_language');
            $RTR->config->set_item('language', $user_lang);

        } elseif($CI->session->userdata('user_language')) {

             // Ensure the chosen language set in the session is maintained
             $RTR->config->set_item('language', $CI->session->userdata('user_language'));

        }                  
    }
        
}

?>
#2

[eluser]xwero[/eluser]
I just added another solution for the same problem without the need to load a library and using sessions.
#3

[eluser]squarebones[/eluser]
I just posted this in reply to a similar question:

http://ellislab.com/forums/viewthread/108690/
#4

[eluser]xwero[/eluser]
squarebones did you try my solution?
#5

[eluser]haydenp[/eluser]
xwero, squarebones thanks for responding

Since my original post I have cleaned the my_language_selection_test library class up on my test site and have it running 100% for a number of languages ... what I do like about my method is:

1. It's super non-complicated to get working
2. It only requires a few lines of code
3. No changes are required to any core files
4. No extra methods required in any of your controllers
5. Does not require any URI_Language_Identifier maintenance in your urls

As mentioned, I'm a novice CI user that happened to "stumble apon" what I think it the easiest way to provide multiple language support. That is ... "the easiest" way if compared to any other solutions I have come across in the forums!

Maybe it is a terrible solution and should be avoided at all costs? Maybe there is a WAY easier solution that I have missed on the forums?

These are the questions I am hoping those with way more CI experience will be able to answer.

I must say, I'm pretty stoked with the results from my solution. If anybody would like me to post a more detailed example of it, please shout!
#6

[eluser]xwero[/eluser]
Maybe the spiders accept cookies already but I think the problem with storing the language in a session is that spiders will not catch your other language pages.

And how are you going to let a user switch the language?
#7

[eluser]haydenp[/eluser]
Quote:And how are you going to let a user switch the language?

Code:
<?php echo anchor($this->uri->uri_string() . '/en', 'English'); ?>
<br />
&lt;?php echo anchor($this->uri->uri_string() . '/pt', 'Portuguese'); ?&gt;

These links feature on the top of the page in my header -OR- you could have flag icons or a select box of the languages you support for the user to choose from ... your choice.

Each language link links to the current page you are on with the selected language of your choice passed as a variable, your autoloaded library class "my_language_selection_test" picks up the variable as the page loads and loads the selected language.

NOTE: That is the only time the language variable will be present in the url, from then on in it is held in the session ... as such, no further URI_Language_Identifier maintenance is required and no routes.php changes etc etc ...
#8

[eluser]Jondolar[/eluser]
If you use a session variable to keep your language choice, then none of your alternate (non default) languages will get indexed by search engines. The only way to make sure search engines see your pages is to put the language identifier in the url.




Theme © iAndrew 2016 - Forum software by © MyBB