CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能
这篇文章主要介绍了CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能,非常实用,需要的朋友可以参考下
本文以实例讲述了CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能,对于网站开发来说非常实用。
具体操作方法如下:
语言包文件如下:
application\language\english\bm_lang.php application\language\zh-cn\bm_lang.php
注意:
1. “_lang.php”的前缀要一致;
2. 如果还有其他语言包,可创建文件application\language\***\bm_lang.php(***为自己取的名字,以对应不同的语言)
实现代码如下:
public function lang($line,$param = array()) { //判断浏览器语言 $default_lang_arr = $_SERVER['HTTP_ACCEPT_LANGUAGE']; $strarr = explode(",",$default_lang_arr); $default_lang = $strarr[0]; // echo '1'.$default_lang; // 根据浏览器类型设置语言 if( $default_lang == 'en-us' || $default_lang == 'en'){ $this->config->set_item('language', 'english'); // 根据设置的语言类型加载语言包 $this->load->language('bm','english'); }else{ $this->config->set_item('language', 'zh-cn'); $this->load->language('bm','zh-cn'); } // 当前语言 // echo '2'.$this->config->item('language'); // 根据语言包中的某个语言标记的翻译,判断是否使用了语言包 $line = 'title'; $param = array(); // $CI = & get_instance(); // $line = $CI->lang->line($line); // 上面两行等价于下面一行,& get_instance()实例化 $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; }
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

How to use vue and Element-plus to achieve multi-language and international support Introduction: In today's globalized era, in order to cope with the needs of users in different languages and cultures, multi-language and international support have become essential features for many front-end projects . This article will introduce how to use vue and Element-plus to achieve multi-language and international support so that the project can flexibly adapt to the needs of different language environments. 1. Install Element-plusElement-plus is vue official

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

With the continuous development of internationalization, more and more websites and applications need to support multi-language switching functions. As a popular front-end framework, Vue provides a plug-in called i18n that can help us achieve multi-language switching. This article will introduce common techniques for using i18n to implement multi-language switching in Vue. Step 1: Install the i18n plug-in First, we need to install the i18n plug-in using npm or yarn. Enter the following command at the command line: npminst

How to use Flask-Babel to achieve multi-language support Introduction: With the continuous development of the Internet, multi-language support has become a necessary feature for most websites and applications. Flask-Babel is a convenient and easy-to-use Flask extension that provides multi-language support based on the Babel library. This article will introduce how to use Flask-Babel to achieve multi-language support, and attach code examples. 1. Install Flask-Babel. Before starting, we need to install Flask-Bab first.

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

With the development of mobile Internet, instant messaging has become more and more important and popular. For many companies, live chat is more like a communication service, providing a convenient communication method that can quickly and effectively solve business problems. Based on this, this article will introduce how to use the PHP framework CodeIgniter to develop a real-time chat application. Understand the CodeIgniter framework CodeIgniter is a lightweight PHP framework that provides a series of simple tools and libraries to help developers quickly

How to use Layui to develop a website that supports multi-language switching. With the development of globalization, more and more websites need to support multi-language switching to meet the needs of different users. Layui is a very popular front-end framework that provides a series of easy-to-use components and tools that can help us quickly develop beautiful websites. This article will introduce how to use Layui to develop a website that supports multi-language switching, and provide specific code examples. First, we need to introduce Layui related files into the web page. Can
