CodeIgniter Forums

Full Version: URI localization: Translating the uri-segments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum


I'm working on a website that has to be presented in at least 3 languages (probably 4). Therefor I'd like to use good looking URI's (
For example a page about tools would be /en/tools in English, but in Dutch is should be /nl/Gereedschappen . The language thing (nl, en, ...) is already handled, bt I'm looking for the last part to translate ...

Does anyone have an idea how to solve this?

El Forum

You can do this using routes or in the .htaccess. I guess routes is more easy to set up and maintain.

$route['en/tools'] = 'tools';
$route['en/tools/(.+)'] = 'tools/$1';
$route['nl/gereedschappen'] = 'tools';
$route['nl/gereedschappen/(.+)'] = 'tools/$1';

Or are you talking about how to automatically create an anchor in different languages?

El Forum


Thanks for the reply, but it has to be, as you thought, be autmotically ...
When the site admin make a new page in the different languages, or there is a new language, there may be no scripting needed anymore ...

El Forum

Then I would go for a pre-controller hook that rewrites the uri segments in the languages that is selected.

El Forum


Do you have a lead on how to write a pre-controller hook?
Something on the wiki or so?

El Forum

for an example of a pre-system hook.

There is information about hooks in the user guide aswell

Just get hooks working and a pre-controller hook that echoes a word and go from there

El Forum

I don't think you need to go through all that trouble creating a hook..
Personally I'd do something like this:

Routes config:
// reroute to controller 'tomtom', method 'map', with arguments $1 and $2.
  $route['(:any)/(:any)'] = 'tomtom/map/$1/$1';

Tomtom.php controller
class Tomtom extends Controller {
    function __construct() {

    function map($lang, $method) {
      $this->db->where('lang', $lang);
      $get = $this->db->get('local_methods');
      if($get->num_rows() <= 0) {
        // this lang is not known. throw error!
        log_message('error', 'language not found, redirecting to default language');

        // map to the global controller, doing the real action.
      } else {
        // language found. continue
        $language = $get->row();
        if($language->{$method} !== false) {
          // the method exists for this language. map to the 'real' method.


In short, what your code does..
1. You send an url like: "nl/gereedschappen/" to the system.
2. The Router maps that url to the language handler controller.
3. The TomTom controller checks the db if the language exists, and if so, if the method exists.
4. The TomTom controller takes the 'real' data handling method and controller to which the localized controller should point from the database and sends the user through that controller
5. Be sure not to use redirect(), since it'll change the url!!

Any questions, or want more code examples? Drop me a line at fabian [at] dotbrilliance [dot] nl


El Forum


Your solution seems interesting as I'm currently working on localization for a project to get information from the database based on where the user is at.

My database tables will have a url-style location for filtering(eg. "buffalo.ny.usa" or ""). There are two ways that I've been considering implementing localization:

1) Using subdomains. So any subdomain would be remapped to the main site, and the subdomain would be parsed, cleaned and used to filter by city. And every visitor will be required to select their location. So,



// with filter applied:
"WHERE location like ''"


2) Using the URI method below, the result would be the same as above.



The problem with #1 is that my current hosting solution doesn't support wildcard subdomains, so I can't easily localize by city like that. (If I start spending more money on hosting, then it will support it). Also, my current home internet setup (which I am not in control of) makes it difficult to setup a proper test environment for developing this solution for production.

The problem with #2 is that I don't know how to do it... I'd really like to just pull the city out, store it in a variable, and do the equivalent of an internal redirect to end up with the result in the second code block above. I don't want to have to worry about the order of segments when users start filtering data by price, condition, etc.

Do you have any suggestions?

El Forum

Hi Nalorin,

Well, you could use my solution, but I recon it'll be useless when you have a variable amount of parameters.
Here's an update for it:

In config/routes.php
// replace the dots by the number of variables you'll need, until $n
// parameter one is always the city, two always the controller, three the method. $4 to $n are the max number of parameters
$route['(:any)/(:any)/(:any)/.../(:any)'] = "reroute/to/$1/$2/$3/.../$n";

Create a controller: controllers/reroute.php
&lt;?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

class Reroute extends Controller {

    public function __construct() {
    public function to() {
        $args = func_get_args();
        $city = $args[0];
        $uri = '';

        // set the city in a session.
        $this->session->set_userdata('city', $city);

        for($i = 1; $i < count($args); $i += 1) {
            $uri .= $args[$i] . '/';



The reroute class' to() method fetches the arguments passed to the method, caches the first parameter as the city, and parses the rest into a redirect url (to another class, method and parameters)

User requests:

The route routes this request to the to() method of the reroute class, and this method fetches an array containing:
[0] => chicago,
[1] => category,
[2] => product,
[3] => 876,
[4] => pictures

The to() method sets a session containing the city, and parses a redirect url:

El Forum

Thanks for your response, Fabdrol.

Your code looks like it will do the work I need, but may do one thing that is undesirable - redirect the user's browser. I was hoping to have this function transparently, similar to an internal redirect using rewriting.

Here's what I was hoping to accomplish, assuming a URL of

Left column is the URI shown in the user's browser. Right column is the URI the server is working with.

1) '/chicago/fruit/pear/123'    '/chicago/fruit/pear/123'
2) '/chicago/fruit/pear/123'    '/fruit/pear/123'
3) '/chicago/fruit/pear/123'    ''// Output sent. Apache thread terminates
Step 1: Server receives URI request from browser
Step 2: Server parses URI and extracts city, then continues executing category controller
Step 3: Script execution completes, thread is destroyed, server waits to receive new request

This way, if the user doesn't find what they want in chicago, they can simply change the city to milwaukee to see if what they're looking for is there:
and the page is loaded with the new city's results

Is it better (or even possible) to grab the city from the URL as a hook or something, store it as a variable, and then load that variable (eg. $city) from the controller's constructor? Or, is it possible to adapt your method to have it function like this?
Pages: 1 2