Welcome Guest, Not a member yet? Register   Sign In
How to run routine before anything else (setting site language)
#1

[eluser]ksorbo[/eluser]
I have a site that is multilingual. The user can select display language. It is saved in a cookie.

I want to be able to set load the language before anything else executes so that all strings in the site will be pulled from the loaded language file.

Where do I run this type of routine? Right now I am trying to run it in the constructor of each controller. It is messy and doesn't work well.

It has to somehow replace the default language which is autoloaded via the config.php file.
#2

[eluser]Aken[/eluser]
You can create your own base controller that every page controller extends, and include whatever code you'd like to run in its constructor.

Then, when you create controllers for your application, you extend your custom base controller instead of the standard CI one:

Code:
// application/core/MY_Controller.php

class MY_Controller extends CI_Controller {

public function __construct()
{
  parent::__construct();
  
  // Do your processing here.
}

}

// Your normal application/controllers files

// OLD:
class Example extends CI_Controller {}

// NEW:
class Example extends MY_Controller {}
#3

[eluser]JoostV[/eluser]
Yup, just follow Aken's instructions.

You can find a video tutorial on base controllers or my_controller here: http://codeigniter.tv/a-10/Extending-the...and-beyond
#4

[eluser]PhilTem[/eluser]
You might also create a hook, that is run before almost every code that CI runs through. But I'd suggest to use the MY_Controller approach, too Wink
#5

[eluser]benjamincurrie[/eluser]
[quote author="PhilTem" date="1327563036"]You might also create a hook, that is run before almost every code that CI runs through. But I'd suggest to use the MY_Controller approach, too Wink[/quote]

I needed Multi-Language support for a project I have been working on and went with the Hook approach in an attempt not to mess with CodeIgniter core files. Put the code online for everyone to enjoy:

https://github.com/benjamincurrie/CodeIg...age-Switch

My main goal was to support multi-lingual search engine friendly URL's, for example:
http://mysite.com/home (home page in English)
http://mysite.com/accueil (home page in French)

This does require a config file to keep track of controller and function translations, for those who don't wish to maintain translations it reverts to URL's like this:
http://mysite.com/fr/home

There is also an option to use a query string:
http://mysite.com/home?lang=fr

I've put the project up on Github and would love some feedback, I have only tested in my own environments so far. Also, feel free to fork and improve on the code.
A lot of people seem to want to use cookies for storing user language preference, however I don't believe this is the best practice for search engine crawlers. Will eventually get to supporting this method too (as well as subdomain).

Ben




Theme © iAndrew 2016 - Forum software by © MyBB