Welcome Guest, Not a member yet? Register   Sign In
passing language identificator via URL in multilingual site
#1

[eluser]El Oscuro[/eluser]
Hello everyone!

I try to create a bilingual site (English and Russian) and encounter the following problem:

My idea is to pass language identificator (eg "EN" or "RU") in URL, for example:

http://www.mysite.com/ru/news/1

I tried using Routes.php, here's what I have now in that file:
Code:
$route['^(en|ru)$'] = "{$route['default_controller']}/index/$1";
$route['(en|ru)/(.+?)/(.+)$'] = "$2/$3/$1";
$route['(en|ru)/(.+)$'] = "$2/index/$1";
But it simply says "Page not found" (CodeIgniter's message).

I have also tried to play with .htaccess file but it didn't get me too far as well.
Code:
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /codeIgniter/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
In other words all I want is that if "EN" or "RU" is found in the beginning or in the middle or the URL, it would just be ALWAYS transferred to the very end of it. Is it possible??

Does anybody have any ideas? Maybe someone faced similar problem...

P.S. I'm quite new to this kind of development and will appreciate any help!

Thanks in advance!
#2

[eluser]Mirage[/eluser]
Hi Alexey -

Not sure why your routes fail based on the example code posted. I do know that you should not include the start/end anchors as you did. The Router adds those automatically. So

Code:
$route['^(en|ru)$'] = "{$route['default_controller']}/index/$1";

should be

Code:
$route['(en|ru)'] = "{$route['default_controller']}/index/$1";

Furtermore, I've recently built a site with multiple language support that solves the problem by adding language routes for all defined routes. That way, a language identifier can be optional, plus I don't have to worry about language when adding more routes. The loop is a bit lame, because it only works with a fixed number of expected substitution variables (up to 3 right now), but I was in a hurry and it should be easy enough to adapt:

Code:
/* the basic routes (could be anything */
$route['(consumer|retail)/plants/(.+)']='plants/plant/$2';
$route['(consumer|retail)/enews(.*)']='enews$2';
$route['retail/imagelibrary/(.*)']='retail/imagelibrary/$1';
$route['(consumer|retail)(.*)']='$1/$1$2';


/* add routes for supported languages */
$lc="(en|fr|de|se|en-us|en-gb|en-ca|nl-be|fr-be|fr-ca)";
foreach ($route as $k=>$v)
{
    $v=str_replace('$3','$4',$v);
    $v=str_replace('$2','$3',$v);
    $v=str_replace('$1','$2',$v);
    $route["{$lc}/{$k}"]=$v;
}
$route["{$lc}"]='main';

unset ($lc, $k, $v);

Hope that helps.

J
#3

[eluser]El Oscuro[/eluser]
Thanks for the answer.

I tried rapidly to remove '^' and '$' from my route constructions but it didn't change anything.

I think the problem resides very deeply and at the same time on the surface. :-)

My working day is closing to its end so I will investigate this issue tomorrow.

Thanks again!

P.S. Any other ideas will also be greatly appreciated!
#4

[eluser]El Oscuro[/eluser]
One more thing.

Don't you use server rerouting abilities, e.g. RewriteRule for Apache?

I mean, if user enters URL like "http://www.yourserver.com/en/consumer/plants/something", web server first would try to locate file or folder named 'en', of course would not find it and then it would give back an error, thus not giving Code Igniter even a chance to do any rerouting!

Am I wrong? I'm completely confused with this issue... %-)
#5

[eluser]Michael Wales[/eluser]
I would only use server-side routing to remove the index.php from within CI's link structure. Then use CI's routing so the framework isn't looking for a controller named 'en' but understands that it is merely a language identifier.
#6

[eluser]El Oscuro[/eluser]
CI Mirage, walesmd,

thank you both for participation in solving this problem.

The problem was in a little bit incorrect CI's rerouting behaviour.

I created a separate thread for this question.




Theme © iAndrew 2016 - Forum software by © MyBB