CodeIgniter using config-controlled multi-language implementation _php instance based on browser language auto-conversion function

Source: Internet
Author: User
Tags codeigniter
In this paper, we use the example to describe the multi-language implementation of CodeIgniter using Config control, which is very useful for website development according to the browser language automatic conversion function.

Here's how it works:

The language pack files are as follows:

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

Attention:
1. The prefix of "_lang.php" should be consistent;
2. If you have additional language packs, you can create a 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 ()) {//Judging browser language $default _lang_arr = $_ server[' Http_accept_language ']; $strarr = Explode (",", $default _lang_arr); $default _lang = $strarr [0];  echo ' 1 '. $default _lang; Set language if according to browser type ($default _lang = = ' en-US ' | | $default _lang = = ' en ') {$this->config->set_item (' language ', ' 中文版 '); Load the language pack according to the language type set $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 ');  Determines whether a language pack is used $line = ' title ', based on the translation of a language tag in the language pack; $param = Array (); $CI = & Get_instance (); $line = $CI->lang->line ($line); The above two lines are equivalent to the following line of,& get_instance () instantiation $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.