CodeIgniter Multi-language Implementation using Config control _php instance based on browser language automatic conversion function

Source: Internet
Author: User

In this paper, an example is given to describe the CodeIgniter implementation of config control based on browser language automatic conversion function, for Web site development is very practical.

The specific method of operation is as follows:

The language pack file is as follows:

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

Attention:
1. The prefix of "_lang.php" should be consistent;
2. If there are other language packs, you can create the file application\language\***\bm_lang.php (* * for your own name, to correspond to different languages)

The implementation code is as follows:

Public Function lang ($line, $param = Array ()) {//Judge browser language $default _lang_arr = $_server[' http_accept_language '];
 $strarr = Explode (",", $default _lang_arr);
 $default _lang = $strarr [0];
 
 echo ' 1 '. $default _lang; Sets the language based on the browser type if ($default _lang = = ' en-US ' | | | $default _lang = = = $this->config->set_item (' language ', ' Englis
 h ');
 Load the language pack according to the set language type $this->load->language (' BM ', ' 中文版 ');
 }else{$this->config->set_item (' language ', ' ZH-CN ');
 $this->load->language (' BM ', ' ZH-CN ');
 
 }//Current language//echo ' 2 '. $this->config->item (' language '); 
 According to the translation of a language tag in a language pack, whether the language pack is used $line = ' title ';
 $param = Array ();
 $CI = & Get_instance ();
 $line = $CI->lang->line ($line);
 The above two lines are equivalent to the following line,& get_instance () 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 ' ^_^ '. $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.