CodeIgniter Multi-language Implementation method _php example

Source: Internet
Author: User
Tags pack codeigniter

In this paper, an example is given to analyze the CodeIgniter implementation method. Share to everyone for your reference, specific as follows:

The CI application directory has a language Language pack directory that is used to configure many different languages. The language configuration is located in the config file with the following configuration format:

Copy Code code as follows:
$config [' language '] = ' 中文版 ';

Defining language files

Language has an empty Chinese directory, which is the system default language directory defined above, from which the system loads Language pack files. If you want to define a different language pack, such as ZH_CN, create a ZH_CN directory and configure the appropriate language pack file. It should be noted that the language file suffix must be _lang.php, language translation through the array of key value pairs, to prevent duplicate key names, you can add a uniform prefix for the key name.

The configuration can be referenced in the following ways:

$lang [' menu_system_title '] = ' system settings ';
$lang [' menu_system_user_admin '] = ' user management ';
$lang [' menu_system_user_list '] = ' user list ';
$lang [' menu_system_user_detail '] = ' user Details ';
$lang [' menu_system_setting '] = ' configuration management ';
/* End of File user_menu_lang.php
////* Location:./system/language/zh_tw/user_menu_lang.php/*

Use of language packs

You need to load the language pack before you can use the language pack to load and use the following methods:

Load mode one, loading without the need to pass the _lang
$this->load->language (' User_menu ', ' user_message ');
Loading mode two
$this->lang->load (' User_menu ');
Use the way one
echo $this->lang->line (' Language_key ');
Use mode two (need to load language help function first)
$this->load->helper (' language ');
echo Lang (' Menu_system_title ');

It can be said that the use of language packs is quite simple, not long ago also made a Taiwan version of the system, by the way the CI in the use of language packs and need to pay attention to the problem.

1, $config [' language '] directly configured into ZH_CN what will happen?

Some default language packs in the system are located in the System/language/english directory, and the corresponding language packs are loaded when using some of the classes provided in the system. At this point the system will first go to the LANGUAGE/ZH_CN directory to find, and then go to the SYSTEM/LANGUAGE/ZH_CN directory to find, can not find a hint that the language pack error. So if you change the language configuration, it's best to copy the files under System/language/english to the corresponding language directory.

Here seems a little strange, why not this thinking: first go to the LANGUAGE/ZH_CN directory to find, can not find the System/language/english directory down to find. It may not be appropriate to call 中文版, which should be called the system default language pack.

2. Do you need to use a language pack?

In a system where this is found, some errors are prompted by the language pack, some directly used in Chinese, may be because of the reasons for the development of many people, the name of the key is not too standard, often need to compare the corresponding language files, resulting in the program to read the relative trouble, and Chinese is not the problem, It's also a bit of a hassle for individuals to use language packs in the system. Therefore, do not need to consider the multilingual program as far as possible or do not use the language pack good.

3, how to automatically detect language packs?

Typically, depending on the language type of the browser, the $_server[' http_accept_language ' in PHP can get the value, and then get to the client browser default language type based on string separation or regular matching. After getting to pass

Copy Code code as follows:
$this->config->set_item (' language ', ' zh_cn ');
Set.

For the use of language packs, you can rewrite the Lang function, with ^_^ as the language pack, 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, multilingual implementations are implemented in the form of arrays, and some are. Mo format, l18n multi-language implementation, PHP needs to Php_gettext extended support, the details can be consulted on the relevant information.

More readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.