Search code examples
phpcodeigniter

localization in codeigniter based on cookie value


I'm trying to rewrite asp.net mvc application to codeigniter.

Basically codeigniter follow mvc pattern so it's pretty much ok. Now I'm stuck at localization. I do not want to change url, in order that /Company/About remains the same in english and german. Inside view I had in asp.net Views/Index.cshtml as default for german and Views/Index.en.US.cshtml for english localized page.

I will describe scenario which works perfect on my asp website.

  1. User clicks on country flag
  2. Based on 1. step value cookie is populated with country value
  3. Helper load desired thred into current thread
  4. Views are localized

How can I apply this approach to codeigniter, or similar at least?


Solution

  • I think it will be not a good idea to have separate files for each language. You can do it easily using some other techniques, which are easy to implement.

    While working on one of my CI project, I need to make it to support multiple languages. As I worked in other multilingual systems like Prestashop, so I borrowed ideas from there and implemented it in my CI project.

    I have implemented it as followed:

    1. I am storing words in language files. Each language has its single file named as language ISO code, like for english its name is en.php In this language file, words are stored as file_name_md5(of the word) in array like below for Hello World in view file hello.php .

      $_lang = array( 'hello_b10a8db164e0754105b7a99be72e3fe5' => 'hallo Welt', ... ... ... )

    The key of the $_lang associative array is the world appended with file name, to be translated, and the value is the translation.

    Storing words and fetching the words into / from these language files, are handled by the Translation library I created.

    1. All my static text in views files are written in english. I created helper function for it called "l" , small L . Lets say I want Hello world to in my view (say hello.php) and should be translated into multiple languages. So in my view I write it like

      l('Hello World...', 'hello')?>
    2. Now the l helper function perform small operation on the arguments passed to it. It takes md5 of the world and append it with the file name as you can see above. Then it calls a member function of my Translation library, which looks into the $_lang array to find that match. If it finds the match, it returns the translation. If no translation is found for that word, then the l helper function returns the original text back.

    3. I have created my own controller library from which all my controllers are extended. To preserve CI features, my parent controllers are extended from default CI controller. In my parent controller, I load the language files according to the user language. This way the $_lang array is available for the Translation library to look into for the words.

    4. In my admin side, I have created a translation system, which reads all my View files for a specific pattern like the below one

      l('Hello World...', 'hello')?>

    The code generate a form in which a text field is created for each word. The text field name is the same as the $_lang array keys, like filename_md5_of_word . The text field label is the original word in this case "Hello World...". And the translation has to be written to the text field.

    On saving, the translations are stored in to specific language file for that particular language selected for translation.

    Using this method, you will be able to add as many languages as you like in future, without creating view files for each language, so it is flexible.

    I hope I have explained enough so you can take the idea of how easily you can implement translation system, and avoid separate view files for each language.