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

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

    When managing the actual files, things can get out of sync pretty easily unless you're really vigilant. So we've launched a (beta) free service called String which allows you to keep track of your language files easily, and collaborate with translators.

    You can either import existing language files (in PHP array, PHP Define, ini, po or .strings formats) or create your own sections from scratch and add content directly through the system.

    String is totally free so please check it out and tell us what you think.

    It's actually built on Codeigniter too! Check out the beta at http://mygengo.com/string

    0 讨论(0)
  • 2020-11-27 11:23

    Have you seen CodeIgniter's Language library?

    The Language Class provides functions to retrieve language files and lines of text for purposes of internationalization.

    In your CodeIgniter system folder you'll find one called language containing sets of language files. You can create your own language files as needed in order to display error and other messages in other languages.

    Language files are typically stored in your system/language directory. Alternately you can create a folder called language inside your application folder and store them there. CodeIgniter will look first in your application/language directory. If the directory does not exist or the specified language is not located there CI will instead look in your global system/language folder.

    In your case...

    • you need to create a polish_lang.php and english_lang.php inside application/language/polish
    • then create your keys inside that file (e.g. $lang['hello'] = "Witaj";
    • then load it in your controller like $this->lang->load('polish_lang', 'polish');
    • then fetch the line like $this->lang->line('hello'); Just store the return value of this function in a variable so you can use it in your view.

    Repeat the steps for the english language and all other languages you need.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 11:25

    Friend, don't worry, if you have any application installed built in codeigniter and you wanna add some language pack just follow these steps:

    1. Add language files in folder application/language/arabic (i add arabic lang in sma2 built in ci)

    2. Go to the file named setting.php in application/modules/settings/views/setting.php. Here you find the array

    <?php /*
    
     $lang = array (
      'english' => 'English',
    
      'arabic' => 'Arabic',  // i add this here
    
      'spanish' => 'Español'
    

    Now save and run the application. It's worked fine.

    0 讨论(0)
  • 2020-11-27 11:26

    Follow this https://github.com/EllisLab/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18n

    its simple and clear, also check out the document @ http://ellislab.com/codeigniter/user-guide/libraries/language.html

    its way simpler than

    0 讨论(0)
提交回复
热议问题