0
votes

I have Library for multi lang. and simple switcher. I have 3 lang. English, German and Russian. The default is German When is check default language the site url look like mysite.com, if change to en or ru the url look like mysite.com/en and mysite.com/ru when i change from en-ru or ru-en the url look like mysite.com/en/ru and gives 404 error.

The switcher is :

                <?php
            foreach ($languages as $language) {
                if ($language['abbr'] == MY_DEFAULT_LANGUAGE_ABBR) {
                    $cr = trim(uri_string(), '/');
                    if (strlen($cr) == 2) {
                        $change_url = base_url();
                    } else {
                        $change_url = base_url(preg_replace('/' . MY_LANGUAGE_ABBR . '\//', '', uri_string()));
                    }
                } else {
                    $u = preg_replace('/' . $language['abbr'] . '\//', '', uri_string());
                    $change_url = base_url($language['abbr'] . '/' . $u);
                }
                ?>
                <li>
                    <a href="<?= $change_url ?>">
                        <img src="<?= base_url('attachments/langflags/' . $language['flag']) ?>" alt="<?= $language['name'] ?>">
                        <span><?= ucfirst($language['name']) ?></span>
                    </a>
                </li>
            <?php } ?> 
1
i think it would be better to save the lang preference in a cookie or session variable. the way you are doing it is going to wreck havoc on the routes e.g. mysite.com/en/ru is looking for controller en with method ru hence the 404 - Alex
I could never understand why somebody would want to add lang to a uri segment. If you simply do it with subdomains, all your uri segments stay the same, and you can set the lang by checking which domain you're on. See here: expressionengine.com/forums/archive/topic/219463/… - Brian Gottier
ok @BrianGottier i'll keep in minde for next time. Do you know how to fix this? - Martinovska

1 Answers

1
votes

Try with this:

$replace= array('en/','ru/');
$u = preg_replace('/' . $language['abbr'] . '\//', '', uri_string());
$change_url = base_url($language['abbr'] . '/' . str_replace($replace,'', $u));