Welcome Guest, Not a member yet? Register   Sign In
Full internationalized urls
#1

[eluser]xwero[/eluser]
The URI Language Identifier is a nice solution but the urls are still in one language. So i was looking for a multi language url solution and i came up with a (post_controller_constructor) hook.
Code:
<?php
function set_lang()
{
    // get config file content
    $config =& get_config();
    // get the first segment of the url
    $CI =& get_instance();
    $url_controller = $CI->uri->segment(1);
    // find the url_controller in the controllers array
    foreach($config['first_segments'] as $langs)
    {
        foreach($langs as $lang=>$controller)
        {
            if($url_controller == $controller)
            {
               $CI->config->set_item('language',$lang);
               $CI->config->set_item('other_languages',array_diff(array_keys($langs),array($lang)));
               break;
            }
        }
    }
}
In the config.php file you add the setting first_segments and you add an array of arrays like this;
Code:
$config['first_segments'] = array(
                              array('lang1'=>'lang1value','lang2'=>'lang2value')
                            );
In the routes.php file you use the setting as following
Code:
$first_segments = config_item('first_segments');
$route['(?:'.implode('|',$first_segments[0]).')'] = 'controller/method';
To make it easier on yourself you can add keys to the first dimension of the fist_segments array. And in the routes.php you could add an array with the all the $route values that can be put in one or more loops.

As a bonus you don't need to set the language in the config.php and you get the other available languages.

I didn't benchmark it yet but it's a more expensive solution than the url identifier as you can imagine. If you have any tips on how to make it less expensive please add them here.
Another thing i don't know for certain is how much languages can be alike? For example British and American English distinction is out of the question because they share too much words.
#2

[eluser]Erdal Demirtas[/eluser]
Hi,

I have done something similar last year.
I posted about this last month in EE-Forums.
I am happy to see more people working with this approach.
Maybe it is a bit expensive, but I think it is better.

In my case all my controllers extends a base controller which determines the language through segment_1. The configuration is in an array as you do (but in a library).

I have one controller for each section of my site and everything is being routed to the related (configured) controller.

I hope that this way will be a part of CI and EE.
#3

[eluser]xwero[/eluser]
I was thinking about a more flexible approach. It doesn't matter if the first segment is a controller, directory or method. The routes know where the content is for that url.

The problem is if you go from one language to another keeping the surfer on the same page. This could mean a massive segments file and that for several languages.

I put it on the forum to see if there are alternatives.
#4

[eluser]Erdal Demirtas[/eluser]
[quote author="xwero" date="1228791777"]I was thinking about a more flexible approach. It doesn't matter if the first segment is a controller, directory or method. The routes know where the content is for that url.
[/quote]

Yes, it is correct, the solution must be flexible.
If the mapping is done through the Routes, it does not matter what serves the request. In my case it was a controller, but it could have been a method as you say.

[quote author="xwero" date="1228791777"]
The problem is if you go from one language to another keeping the surfer on the same page. This could mean a massive segments file and that for several languages.
[/quote]

This is really a big problem.
But if you can't or don't need to translate everypage one to one, this is not a problem any more. The visitor begins from the first level (segment_1) every time the language is changed. Of course it is not a global solution, but it was what I need.

I hope we hear more alternatives and ideas about doing all this better.
#5

[eluser]Muser[/eluser]
Interesting solution but...

what about if first_segment matches in 2 or more languages?

A solution for this cases could be to concatenate the language abbreviation... Tongue

"word-en"
"word-dk"
#6

[eluser]xwero[/eluser]
Muser the idea is to remove the language from the url so adding it on top of the language specific words would defy the purpose of the code.

I've been thinking a database powered first segment search would be more memory efficient and faster. This also opens the possibility to check a second segment if it is present and it is a language segment but how will the url segments be flagged to be language dependent or not?

For the switching links i am thinking about a per controller/method language file that extends a common language file. This is, again, more memory efficient.

I will see later how the new ideas work out in practice.




Theme © iAndrew 2016 - Forum software by © MyBB