Welcome Guest, Not a member yet? Register   Sign In
Language from URI
#1

[eluser]Ashley Snowdon[/eluser]
What I'm trying to achieve is to grab the language from the URI, i.e. example.com/en/users and then load the en language file. Likewise for /de/ or /fr/

In my routes.php I have this:
Code:
$route['en|de|fr([a-z]+)'] = "$1";

This allows me to have my application ignore the language from the URI so my controllers still function correctly. Although now I have the problem with storing this in some sort of config variable which I can then do something like:
Code:
$this->lang->load('error', $this->config->item('parsed_language'));

Although, I think I am going around this the wrong way. Because I would also like to have on my default controller a geo-locator that would automatically detect the location. But this would probably be for later.

Many thanks,
Ashley
#2

[eluser]Piter[/eluser]
Hi

Maybe use something like this

Code:
$route['(\w{2})/([a-zA-Z0-9/_-]+)'] = "$1";

Is langueage: $this->uri->segment('1'); ??
#3

[eluser]tomcode[/eluser]
Once the language segment converted to CI language names, I change the application language before loading any language file.

Code:
this->config->set_item('language', $cur_lang);

I do this either in the constructor of MY_Controller or I extend CI's Config class with a set_language() method.
#4

[eluser]Ashley Snowdon[/eluser]
So, in addition to having my routes.php with what I posted in my first post, where would I store your $cur_lang value?

If I add this in the constructor of MY_Controller, would it be something like this?

Code:
if (in_array($this->uri->segment('1'), $lang_array)) {
    $this->config->set_item('language', $this->uri->segment('1'));
}

If so, could I also set up a config variable for the $lang_array?
#5

[eluser]tomcode[/eluser]
I do not use CI language names (french, english, german) in the URL, but ISO 639.1 codes (fr, en de).
So I need to convert them.

Then, I use the language setting of the config/config.php to store the current language, the only way to nicely work with
the language files.

This requires to load all language files exclusively after the setting of the current language.

In a configuration file :
Code:
$config['languages']= array
(
    'en' => 'english',
    'fr' => 'french'
);

In the controller :
Code:
$languages = $this->config->item('languages');
$lang_segs = array_flip($languages);

// get the ISO ident for my default language
$default_lang = $this->config->item('language');
$default_seg = $lang_keys[$default_lang];


// retrieve the URI language segment, if none is passed,
// the default language is used
$seg = $this->uri->segment(1, default_seg);

$cur_lang = isset($languages[$seg]) ? $languages[$seg] : $languages[$default_seg];

// finish
$this->config->set_item('language', $cur_lang);

// load language files ...
#6

[eluser]Ashley Snowdon[/eluser]
Ah, okay. I was applying this in MY_Controller and autoloading it, thus ending up with the URI library not being available. I've got this working now, although my regex route seems to be a bit wrong. I'm currently using this: (since the other one was a bit broken)

Code:
$route['en|de|fr|\/([a-z]+)'] = "$1";

I also tried the regex in reply #1, but I couldn't get this to work at all. The problem with my own that I first posted was that it wouldn't accept the last value in the OR. I fixed it by adding another pipe on the end, but I'm almost certain this is incorrect.

I've immediately come across the problem that my links are not taking the language segment of the URI into account - how would I get around this? Do I have to prepend the language at the beginning of all my links? I.e. the ones that use anchor() and form_open(), etc.
#7

[eluser]tomcode[/eluser]
I use these routes :

Code:
/*
| -------------------------------------------------------------------------
| INTERNATIONAL ROUTING
| -------------------------------------------------------------------------
| Routing for sites where the first URI segment serves as language ident.
|
*/

// URI like '/en/about' -> use controller 'about'
$route['^[a-z][a-z]/(.+)$'] = "$1";

// '/en' and '/fr' URIs -> use default controller
$route['^[a-z][a-z]$'] = $route['default_controller'];


And yes, You have to prepend the language segment, I am working right now with an extension of the Config class and a helper method which does that Wink

I've just written them for the project I am on, I attach the files, but watch out, I haven't revised them yet, I use them in a special mode where the default language does not have a language segment

I have avoided to change the behaviour of the standard CI methods, because I need them from time to time and I don't like that anyway.
#8

[eluser]Ashley Snowdon[/eluser]
This is brilliant, thanks. Smile

Quote:..but watch out, I haven’t revised them yet, I use them in a special mode where the default language does not have a language segment

Do you mean that the default language would still apply like this (french being the default language example): http://www.example.com/class/function equals to http://www.example.com/fr/class/function
#9

[eluser]tomcode[/eluser]
[quote author="Ashley Snowdon" date="1274129453"]This is brilliant, thanks. Smile

Quote:..but watch out, I haven’t revised them yet, I use them in a special mode where the default language does not have a language segment

Do you mean that the default language would still apply like this (french being the default language example): http://www.example.com/class/function equals to http://www.example.com/fr/class/function[/quote]


Yeah, that's the idea.
#10

[eluser]Ashley Snowdon[/eluser]
That's good, because that's what I was aiming for originally. Smile

I haven't experimented with "MY_" libraries yet - how would I load them? In autoload or in a controller? I'm guessing if I am always dependant on it, I load it from autoload, otherwise controller-specific?




Theme © iAndrew 2016 - Forum software by © MyBB