This article analyzes the CodeIgniter multi-language implementation method with examples. Share it with everyone for your reference, the details are as follows:
There is a language package directory under the CI application directory, which is used to configure multiple different languages. The language configuration is located in the config file, and the configuration format is as follows:
Copy code The code is as follows: $config['language'] = 'english';
Definition language file
There is an empty english directory underlanguage, which is the system default language directory defined above. The system will load the language pack file from this directory. If you want to define different language packs, such as zh_cn, just create the zh_cn directory and configure the corresponding language pack files. It should be noted that the suffix of the language file must be _lang.php. Language translation is implemented through the key-value pairs of the array. To prevent duplicate key names, a unified prefix can be added to the key names.
Please refer to the following configuration method:
$lang['menu_system_title'] = '系统设置'; $lang['menu_system_user_admin'] = '用户管理'; $lang['menu_system_user_list'] = '用户列表'; $lang['menu_system_user_detail'] = '用户详情'; $lang['menu_system_setting'] = '配置管理'; /* End of file user_menu_lang.php */ /* Location: ./system/language/zh_tw/user_menu_lang.php */
Using language pack
You need to load the language pack before using it. The loading and usage methods are as follows:
//加载方式一,加载时不需要传入_lang $this->load->language(array('user_menu', 'user_message')); //加载方式二 $this->lang->load('user_menu'); //使用方式一 echo $this->lang->line('language_key'); //使用方式二(需先加载language帮助函数) $this->load->helper('language'); echo lang('menu_system_title');
It can be said that the use of language packs is quite simple. I also made a Taiwan version of the system not long ago. By the way, let me talk about the use of language packs in CI and the issues that need attention.
1. What will happen if $config['language'] is directly configured as zh_cn?
There are some default language packages in the system located in the system/language/english directory. When an error is reported using some classes provided by the system, the corresponding language package will be loaded. At this time, the system will first search in the language/zh_cn directory, and then search in the system/language/zh_cn directory. If it cannot find it, it will prompt an error that the language package cannot be found. Therefore, if you change the language configuration, it is best to copy the files under system/language/english to the corresponding language directory.
This seems a bit strange, why not think like this: first go to the language/zh_cn directory to find it, and if you can’t find it, go to the system/language/english directory. It may not be appropriate to call it english. It should be called the system default language pack.
2. Do I need to use a language pack?
I found this situation in a certain system. Some error prompts use language packages, and some use Chinese directly. Maybe because of multiple people developing, the naming of keys is not standardized. It is often necessary to compare the corresponding ones. Language files make it relatively troublesome to read the program, but the Chinese version does not have this problem. Personally, I also find it a bit troublesome to use language packs in the system. Therefore, if you do not need to consider multi-language programs, it is best not to use language packs.
3. How to automatically detect language packs?
Usually, it can be judged based on the language type of the browser. The value can be obtained by $_SERVER['HTTP_ACCEPT_LANGUAGE'] in PHP, and then the client browser can be obtained according to string separation or regular matching. Default language type. After obtaining it, copy the code through . The code is as follows: $this->config->set_item('language', 'zh_cn'); and you can set it.
As for whether the language pack is actually used, you can rewrite the lang function. If ^_^ is used, the language pack is used. The reference is as follows:
function lang($line, $param = array()) { $CI =& get_instance(); $line = $CI->lang->line($line); if(is_array($param) && count($param) > 0) { array_unshift($param, $line); $line = call_user_func_array('sprintf', $param); } return '^_^'.$line; }
In many cases, multi-language implementation is implemented in the form of arrays, and some are in .mo format, l18n multi-language implementation requires php_gettext extension support in php, please refer to relevant information for details.
Readers who are interested in more CodeIgniter related content can check out the special topics on this site: "codeigniter introductory tutorial" and "CI (CodeIgniter) framework advanced tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.