• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routing for multilingual urls

#1
Hi I'd like some help for setting up my routes properly for multilingual pages.

The url from my pages look like this:
Code:
controller/index
controller/method
controller/method/param1
controller/method/param1/param2 etc etc

What I'd like to do is to prepend on the url the selected language and have the ability to pass many params on my url, using a single route if possible.. So basicaly instead of using many routes, like this:
PHP Code:
$route['eng/(:any)'] = '$1/index/eng'// controller/index/lang
$route['eng/(:any)/(:any)'] = '$1/$2/eng'// controller/function/lang
$route['eng/(:any)/(:any)/(:any)'] = '$1/$2/$3/eng'// controller/function/param/lang
etc etc 

which is not very efficient actually, I was wondering whether there's a way to use a single route to do this, something like this:
PHP Code:
$route['eng/(:any)/(:any)/unlimited parameters'] = '$1/$2/unlimited parameters/eng'// which is controller/method/unlimited params/lang 
Reply

#2
(09-01-2015, 02:07 AM)Lykos22 Wrote: Hi I'd like some help for setting up my routes properly for multilingual pages.

The url from my pages look like this:

Code:
controller/index
controller/method
controller/method/param1
controller/method/param1/param2 etc etc

What I'd like to do is to prepend on the url the selected language and have the ability to pass many params on my url, using a single route if possible.. So basicaly instead of using many routes, like this:

PHP Code:
$route['eng/(:any)'] = '$1/index/eng'// controller/index/lang
$route['eng/(:any)/(:any)'] = '$1/$2/eng'// controller/function/lang
$route['eng/(:any)/(:any)/(:any)'] = '$1/$2/$3/eng'// controller/function/param/lang
etc etc 
 
which is not very efficient actually, I was wondering whether there's a way to use a single route to do this, something like this:

PHP Code:
$route['eng/(:any)/(:any)/unlimited parameters'] = '$1/$2/unlimited parameters/eng'// which is controller/method/unlimited params/lang 


Hello my friend! How are you?

I'm not sure why you want to such thing like this. When I have to work with multilingual applications I usually use the Language Library. You can set the actual language in a session ou cookie and then use the appropriate language as shown you the Library docs.
That will keep you code and urls clean.

I don't know if that solves your problem, but it's a solution for the multilingual issue.
The web is so cool! That you need to do cool stuff... Wink
Reply

#3
I have never really found a default method I like for dealing with languages. There seem to be so many issues surrounding it.

My preference is to have a default language for the site and language alternatives I write to a cookie and read that to decide which language content to deliver. Thus no change to url formats at all, and any bits of text I have missed are delivered in English, without any errors appearing. However I am not sure this is best practice for SEO reasons.

However, you can add as many params as you want to the URL, and any not collected in the controller are ignored.

I tried once and quite liked having non-default languages as sub domains, such as fr.mydomain.co.uk or thai.mydomain.co.uk which has worked well for me in the past, but maintenance was a pain and I don't think I would do that again.

However, I have not tried this, but I think it would be ideal to have different languages on different domains, so mydomain.co.uk in English, mydomain.fr in French etc etc. I am going to try that next time it arises although I can see some issues coming from it, but as I said, have not tried it yet.

I am not sure if that helps at all but I hope it does.

Paul.
Reply

#4
i've no idea but wouldn't it better to do this like


PHP Code:
$route['en|de/(:any)/(:any)'] = "$1/$2"


and after that - you create a MY_Controller where you've centralized access to your language - i.e.

PHP Code:
class MY_Controller extends CI_Controller
{
    protected 
$strCurrentActiveLanguage "en";
    private 
$arrValidLanguageTypes = array("en","de");

    public function 
__construct()
    {
        
$this->setLanguage();
    }

    public function 
setLanguage()
    {
        
$strLanguage $this->uri->segment(1);

        if (
in_array($strLanguage$this->arrValidLanguageTypes)) $this->strCurrentActiveLanguage $strLanguage;
    }




Now you can extend your Main Controller and you have access to your specific Language.
Reply

#5
That is very neat. But would you not now have to modify anything url related like site_url(), base_url(), form_open() etc etc.
Reply

#6
mh don't know why ?
The urls remain the same isn't it ?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.