the best way to make codeigniter website multi-language. calling from lang arrays depends on lang session?

后端 未结 11 1099
情深已故
情深已故 2020-11-27 10:34

I\'m researching hours and hours, but I could not find any clear, efficient way to make it :/

I have a codeigniter base website in English and I have to add a Polish

11条回答
  •  长情又很酷
    2020-11-27 11:14

    I am using such code in config.php:

    $lang = 'ru'; // this language will be used if there is no any lang information from useragent (for example, from command line, wget, etc...
    
    if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
    $tmp_value = $_COOKIE['language'];
    if (!empty($tmp_value)) $lang = $tmp_value;
    switch ($lang)
    {
        case 'ru':
            $config['language'] = 'russian';
            setlocale(LC_ALL,'ru_RU.UTF-8'); 
            break;
            case 'uk':
            $config['language'] = 'ukrainian';
            setlocale(LC_ALL,'uk_UA.UTF-8'); 
                    break;
            case 'foo':
            $config['language'] = 'foo';
            setlocale(LC_ALL,'foo_FOO.UTF-8'); 
                    break;
            default:
            $config['language'] = 'english';
            setlocale(LC_ALL,'en_US.UTF-8'); 
            break;
    }
    

    .... and then i'm using usualy internal mechanizm of CI

    o, almost forget! in views i using buttons, which seting cookie 'language' with language, prefered by user.

    So, first this code try to detect "preffered language" setted in user`s useragent (browser). Then code try to read cookie 'language'. And finaly - switch sets language for CI-application

提交回复
热议问题