0

I'm using codeigniter multilanguage and it works fine. The problem is when I try to do multilanguage in the URL... how can I do it?

I mean the controller has to be a file, with a name, and its functions too... so I can't figure how can I do it.

The only alternative I thought is create the same controllers for each language I need... but this is a lot of repeated code just for change the name of the controller and functions... and the maintenance will be a big trouble.

Any help?

tereško
  • 58,060
  • 25
  • 98
  • 150
Marc Pont
  • 988
  • 2
  • 14
  • 34
  • this might help: http://stackoverflow.com/a/19430987/727208 – tereško Feb 26 '15 at 15:09
  • You want to create routes that have the language as a parameter. Often this is the first parameter e.g. `example.com/en/login`. Use that parameter in your controller to load language files etc. You can do this with one route and one controller. – Matthew Rapati Feb 26 '15 at 15:18
  • @MatthewRapati The problem I'm asking is I want to translate the url... example.com/en/login, okay the segment en is dynamic, but the controller login will be always "login" i want to translate de controller and the functions too – Marc Pont Feb 26 '15 at 16:07
  • @tereško this will help me, but I found it a lit difficult... that part of translating urls – Marc Pont Feb 26 '15 at 16:11
  • @slayerbleast translating URLs is not only complicated, but also has various other issues. Most of international sites use `/en/` and `/de/` snippets in the URL and leave everything else in a single language. You can also use something like `/en/123/lorem-ipsum`, where `"123"` is actually ID of an article and `"lorem-ipsum"` is not really used for routing. Kinda like SO post/user links. – tereško Feb 26 '15 at 16:13
  • @slayerbleast you can use regular expressions in the routes https://ellislab.com/codeIgniter/user-guide/general/routing.html So you can do something like `$route[':language/(apple)|(pomme)']` – Matthew Rapati Feb 26 '15 at 16:14
  • yes I would think if its worth... because translating urls are just only for google robots... most users don't care about it – Marc Pont Feb 26 '15 at 16:17
  • @MatthewRapati, yes, I thought about it.... but the problem is if I do that I have to create the controller "apple" and "pomme". They have to exists both, and they will be exactly the same but with all functions name translated... this is a lot of work and seems messy to mantain.. and imagine if I have 8 languages... – Marc Pont Feb 26 '15 at 16:21
  • @slayerbleast no, you can that is one route which you can route to the same controller action! `$route[':language/(apple)|(pomme)'] = 'fruitcontroller/applemethod'` – Matthew Rapati Feb 26 '15 at 18:21

1 Answers1

0

Pass the language indicator as a "GET" value to your controller functions: eg.

base_url/controller/inventory/en

Then use it like this in your controller:

    /**
     * @desc This will get called when no method specified 
     * Will show home page (list of items)
     */
    function inventory($lang="en",$from=0){

        // load proper language file
        $this->lang->load('language_filename', $lang);

        // generate db where clause
        $where = array(
            "published"=>"1",
            "language"=>$lang
        );

        // paging
        $this->_setPagingLinks($this->newsModel->getTotalRecordsNumber($where),10,4,"inventory/".$lang,$lang);

        // loading items from db
        $this->data["news"] = $this->newsModel->getRecords($where,$from,10,"time");

        // load the view according to language
        $this->data["content"] = $this->load->view("$lang/news",$this->data,TRUE);
        $this->load->view("$lang/container",$this->data);
    }
Babak
  • 419
  • 2
  • 16
  • There is no problem translating the page content, I already did it. The problem is, I want to translate the URL too. In your example the url continues being base_url/controller/inventory/en. so the controller and the function inventory will be fixed for all languages – Marc Pont Feb 26 '15 at 16:03
  • Sounds like i got your question wrongly.. do you want to change the controller class name in the URL? if yes then you may want to take a look at URI Routing see this [link](https://ellislab.com/codeIgniter/user-guide/general/routing.html) – Babak Feb 26 '15 at 16:09
  • I answered this on comments of my question: (a)slayerbleast you can use regular expressions in the routes ellislab.com/codeIgniter/user-guide/general/routing.html So you can do something like $route[':language/(apple)|(pomme)'] – Matthew Rapati (a)MatthewRapati, yes, I thought about it.... but the problem is if I do that I have to create the controller "apple" and "pomme". They have to exists both, and they will be exactly the same but with all functions name translated... this is a lot of work and seems messy to mantain.. and imagine if I have 8 languages... – slayerbleast – Marc Pont Feb 26 '15 at 16:29