CodeIgniter uses multiple languages controlled by config to implement automatic conversion function based on browser language _ php instance

Source: Internet
Author: User
Tags codeigniter
This article mainly introduces CodeIgniter's use of multiple languages controlled by config to realize automatic Conversion Based on browser language, which is very practical, for more information about CodeIgniter, see the example below. CodeIgniter uses multiple languages controlled by config to implement automatic Conversion Based on browser languages, which is very practical for website development.

The procedure is as follows:

The language package file is as follows:

application\language\english\bm_lang.phpapplication\language\zh-cn\bm_lang.php

Note:
1. the prefix of "_ lang. php" must be consistent;
2. If there are other language packs, you can create the file application \ language \ *** \ bm_lang.php (*** for your own name, corresponding to different languages)

The implementation code is as follows:

Public function lang ($ line, $ param = array () {// judge the browser language $ default_lang_arr = $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']; $ strarr = explode (",", $ default_lang_arr); $ default_lang = $ strarr [0]; // echo '1 '. $ default_lang; // set the language if ($ default_lang = 'en-us' | $ default_lang = 'en') based on the browser type ') {$ this-> config-> set_item ('language ', 'English '); // load the language Pack according to the language type you set $ this-> load-> language ('bm ', 'English ');} else {$ this-> config-> set_item ('language ', 'zh-cn'); $ this-> load-> language ('bm ', 'zh-cn');} // current language // echo '2 '. $ this-> config-> item ('language'); // determines whether the language Pack $ line = 'title' is used based on the translation of a language mark in the language Pack '; $ param = array (); // $ CI = & get_instance (); // $ line = $ CI-> lang-> line ($ line ); // The above two rows are equivalent to the following row. & get_instance () is instantiated $ line = $ this-> lang-> line ('title'); if (is_array ($ param) & count ($ param)> 0) {array_unshift ($ param, $ line); $ line = call_user_func_array ('sprintf', $ param);} echo 'prop _^ '. $ line ;}

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.