Welcome Guest, Not a member yet? Register   Sign In
Language related question
#11

[eluser]Jondolar[/eluser]
[quote author="Klaatush" date="1243950010"]I am also facing the same situation, actually a little more worse

We have currently 3 languages ENG/DUT/FRE (English, Dutch, French). Any of the language can be set to default and saved in DB.

for the default language we don't need the lang parameter but the rest will show with the lang in URI. Structure of URI is

http://domain.com/[lang/]controller/function/parameters...

so for example if eng is default lang of site, page will be

http://domain.com/news/show/1

but if the user selects DUT as language, the page URI will become

http://domain.com/dut/news/show/1


if you find the solution of your problem, kindly post here, so with the help of that solution, I can solve my problem.

Thanks in advance.[/quote]

There is a solution. You use URI routing so if the first section of the URI has 2 characters, rewrite it to the controller in the second section.
There are posts on how to do this but it is just one line and very simple:

Next, in your controller, look to see if the first section of the URI has 2 characters and if it does, assign your $lang variable to that. If not, assign your default lang to that variable. You now have the proper language.

When creating URLs, you can create a variable is assigned "/lang" if you are not on your default language and is assigned "" if you are on your default language. Then, echo out the lang variable in front of every URL. There are other ways to do this.

Good luck with your project.
#12

[eluser]Klaatush[/eluser]
played with CI for sometime and end up with this code

my_router.php to override some routing
Code:
class MY_Router extends CI_Router
{
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    function _validate_request($segments)
    {
        $tmp_segments = $segments;
        $lang_settings_b_done = $lang_settings_a_done = false;
        if(file_exists(APPPATH.'config/lang_settings_b'.EXT))
        {
            @include_once(APPPATH.'config/lang_settings_b'.EXT);
        }
        if(!$lang_settings_b_done && file_exists(APPPATH.'config/lang_settings_a'.EXT))
        {
            @include_once(APPPATH.'config/lang_settings_a'.EXT);
        }
        
        //checks if segment[0] is a lang
        if(isset($site_langs[strtolower($segments[0])]))
        {
            if($site_langs[$segments[0]]==0)
            {
                $segments[0]=$site_langs[0];
                header('location:/'.implode('/',$segments));
                exit;
            }
            array_shift($segments);
        }
        
        if($this->check_controller_exists(APPPATH.'controllers/', $segments))
        {
            return parent::_validate_request($segments);
        }
        else
        {
            return parent::_validate_request(array_merge(array('cms_page','show_page'),$segments));
        }
    }

    public function check_controller_exists($path, $segments)
    {
        if(empty($segments))
            return false;
        elseif(file_exists($path . $segments[0].EXT))
            return true;
        else
        {
            $seg = array_shift($segments);
            return $this->check_controller_exists($path.$seg.'/', $segments);
        }
    }
}

lang_stting_a.php and lang_setting_b.php holds same data and will be generated from DB....
using two files just to eliminate the chances that if the file is opened up for writing and have garbage data for the time..the routing class attempt to read lang_setting_b first but when generating files, lang_setting_a will be generated first..

lang_setting_a.php
Code:
$site_langs=array();
$site_langs[0]='eng';
$site_langs['eng']=1;
$site_langs['dut']=1;
$site_langs['fre']=0;

$lang_settings_a_done = TRUE;

if we have a language but its disables, e.g. French in above case, all request to the language will be transferred to default language


and there will be a CMS as well whose pages will be accessed as
http://domain.tld/[language/]page

but the actual path will be
http://domain.tld/[language/]cms_page/show_page/page

I think I have some mess in the code (though in initial testing it worked fine)..

looking forward to your comments to improve the code
#13

[eluser]Tottys Design[/eluser]
But what if I need something like this:

default language:
www.website.com/en/gallery

and portuguese:
www.website.com/pt/galeria

How can i do it?
Thanks!
#14

[eluser]Unknown[/eluser]
I have the same question on my mind, is it possible to have the following solution in CI

default language:
www.website.com/gallery

and portuguese:
pt.website.com/galeria

Any tips or hints would be very helpfull.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB