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

后端 未结 11 1060
情深已故
情深已故 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:24

    In the controller add following lines when you make the cunstructor

    i.e, after

    parent::Controller();

    add below lines

        $this->load->helper('lang_translate');
        $this->lang->load('nl_site', 'nl'); // ('filename', 'directory')
    

    create helper file lang_translate_helper.php with following function and put it in directory system\application\helpers

    function label($label, $obj)
    {
        $return = $obj->lang->line($label);
        if($return)
            echo $return;
        else
            echo $label;
    }
    

    for each of the language, create a directory with language abbrevation like en, nl, fr, etc., under system\application\languages

    create language file in above (respective) directory which will contain $lang array holding pairs label=>language_value as given below

    nl_site_lang.php

    $lang['welcome'] = 'Welkom';
    $lang['hello word'] = 'worde Witaj';
    

    en_site_lang.php

    $lang['welcome'] = 'Welcome';
    $lang['hello word'] = 'Hello Word';
    

    you can store multiple files for same language with differently as per the requirement e.g, if you want separate language file for managing backend (administrator section) you can use it in controller as $this->lang->load('nl_admin', 'nl');

    nl_admin_lang.php

    $lang['welcome'] = 'Welkom';
    $lang['hello word'] = 'worde Witaj';
    

    and finally to print the label in desired language, access labels as below in view

    label('welcome', $this);

    OR

    label('hello word', $this);

    note the space in hello & word you can use it like this way as well :)

    whene there is no lable defined in the language file, it will simply print it what you passed to the function label.

提交回复
热议问题