Taking into consideration that the standard url would be http://example.com/fr/controller/method we could of course duplicate all our controllers inside some sort of language directories (application/controllers/fr/controller_in_french, application/controllers/ro/controller_in_romanian), but that would just be too hard on the long run: when you will change something inside a controller, you would have to make the same changes in all controllers of different languages.

So why not do something that will allow us to send all requests, no matter the language to the same controllers, the difference being that when working with the database tables, it will only ask for info in its own language.

But we will have to leave the users the impression that they are accessing a language specific url. What do I mean by this? If in english someone is asking to see all posts for a certain category it will go to http://example.com/category/name_of_category. Now, if someone is asking to see all posts for a certain category in french, he/she will have to go to http://example.com/fr/categorie/name_of_category, but behind the scene he/she will actually be served by the Category.php controller.

To do this we will work inside the application/config/development/routes.php.

First of all, we associate “controllers” and “methods” of other languages to the controllers and methods we have defined (this one is done manually, unless you think of another method):

PHP

1

2

3

4

5

6

7

8

9

10

$controllers_methods=array(

'de'=>array(

'willkommen/list'=>'welcome/list',

'willkommen'=>'welcome'

),

'fr'=>array(

'bienvenu/list'=>'welcome/list',

'bienvenu'=>'welcome'

)

);

The thing is that, when you write the uris you must write the biggest uris first (the ones that have methods attached to controllers), and leave the shortest ones (the ones that have only controllers) at the end.

17 thoughts on “Step 4.3 – Use “personalized” controllers and methods for a multilanguage site in CodeIgniter”

Sorry for the silly question…but what does exactly mean $route[‘^(\w{2})$’] or, better, what’s the purpose of that regular expression?
Letterally it should mean a string starting with a word of two letters till the end of the string…but I don’t understand the purpose of using that expression.
Thanks for any feedback

That means that when someone changes language, the application won’t start to look for a controller that has two letters and then output a 404. With this route, the application will know that whatever link has only two letters, it will be directed to the default controller (Welcome.php). The Welcome.php, extending the MY_Controller, will know what language is asked for and output the content accordingly.

Hello. If you followed all the episodes, you must have got to the part where you set up the languages. In there there is a checkbox that will let you set the default language when you insert or edit languages. If you set one as default that will be taken as default and won’t need anything else to be appended to the url.

Question about routing controllers and methods to use the language selected. I was testing one for Spanish. I have if they go to ‘../es/bienvendia’ it goes to the welcome page just fine. But then if I go to say ‘../es/admin’ it goes to a 404 error page. How do I get it to work if I don’t have a controller or method not listed in the $controllers_method array? Or do I now have to add to the array every controller/method I want to use for that language?

Hello… sorry about that. In the previous tutorial I’ve written a line especially for that, but it seems that during this episode I forgot to put that line again. Just before: “$route[‘^(\w{2})$’] = $route[‘default_controller’];” you must insert: “$route[‘^(\w{2})/(.*)$’] = ‘$2’;”. I’ve modified this tutorial’s code too. Thank you for noticing.

Sorry again. I added the line but it is now saying that $new_link is not a variable, which would make since if there is not link in the $controllers_methods array. So I tried to move the ‘return $new_link;’ where the ‘break’ is in the foreach, that removed the error, but then still only directed me to the default_controller.

For every link i add to the $controllers_method it works, but I am wanting to make sure that it does not break if I do not add a link to the array. So using Admin/Users as an example, and not adding it to the array, it only directs to the Welcome controller (default_controller).

You are right about your assumption. The first thing to do before iterating through $controller_methods is to see if there is a $controllers_methods having the language slug as key. Will update the tutorial in 10 minutes.

Wow, just when I thought I’d exhausted all the online tutorials on Codeigniter, I find a new one – and one that gives me something I can actually use, and with an updated version of CI. I’ve completed the Ion Auth and the CMS tutorials because they are something I can use for my own projects. Thanks, Adrian.

PS: I note that there are a lot of controller files (with matching model files). Could the CMS project for example be extended into an HMVC framework? I am looking for tutorials on HMVC with Codeigniter and there are not a lot…

Hello all, to Fionn’s question, I am an enthusiast of hmvc and am converting the cms here being developed into hmvc. So far, i have a working admin but i am not sure i can post the tutorial as this our hero did here, however, i got to try when i am do and make due reference here.
Thanks to Mr. Avenir for his wonderful tutorials