全文: http://maestric.com/doc/php/codeigniter_i18n
What it does
Have the language in the URL
-
maestric.com/en/about
-
maestric.com/fr/about
Keep using CodeIgniter Language Class
Example
View
<p> <?=lang('about.gender')?> </p>
English language file
$lang['about.gender'] = "I'm a man";
French language file
$lang['about.gender'] = "Je suis un homme";
Result with maestric.com/en/about
<p>I'm a man</p>
Result with maestric.com/fr/about
<p>Je suis un homme</p>
Installation
-
Put
MY_Language.php
andMY_Config.php
insystem/application/libraries
Configuration
-
You must be using pretty URLs (without index.php). With Apache it's usually achieved with
mod_rewrite
through an.htacess
In config.php
-
$config['base_url']
must correspond to your configuration. -
$config['index_page'] = ””
In config/routes.php
add
// URI like '/en/about' -> use controller 'about' $route['^fr/(.+)$'] = "$1"; $route['^en/(.+)$'] = "$1"; // '/en' and '/fr' URIs -> use default controller $route['^fr$'] = $route['default_controller']; $route['^en$'] = $route['default_controller'];
Use
Let's build a bilingual English/French page.
language files
system/application/language/english/about_lang.php
<?php $lang['about.gender'] = "I'm a man"; /* End of file about_lang.php */ /* Location: ./system/language/english/about_lang.php */
system/application/language/french/about_lang.php
<?php $lang['about.gender'] = "Je suis un homme"; /* End of file about_lang.php */ /* Location: ./system/language/french/about_lang.php */
controller
system/application/controllers/about.php
<?php class About extends Controller { function index() { // you might want to just autoload these two helpers $this->load->helper('language'); $this->load->helper('url'); // load language file $this->lang->load('about'); $this->load->view('about'); } } /* End of file about.php */ /* Location: ./system/application/controllers/about.php */
view
system/application/views/about.php
<p><?=lang('about.gender')?></p> <p><?=anchor('music','Shania Twain')?></p>
Test
<p>I'm a man</p> <p><a href="http://mywebsite.com/en/music">Shania Twain</a></p>
<p>Je suis un homme</p> <p><a href="http://mywebsite.com/fr/music">Shania Twain</a></p>
Notes
-
You might need to translate some of CodeIgniter's language files in
system/language
. Example: if you're using the “Form Validation” library for French pages, translatesystem/language/form_validation_lang.php
tosystem/application/language/french/form_validation_lang.php
.
-
links to internal pages are prefixed by the current language, but links to files are not.
site_url('about/my_work'); // http://mywebsite.com/en/about/my_work site_url('css/styles.css'); // http://mywebsite.com/css/styles.css
-
Get the current language
$this->lang->lang(); // en
-
Switch to another language
anchor($this->lang->switch_uri('fr'),'Display current page in French');
-
the root page (/) is supposed to be some kind of splash page, without any specific language. This can be changed: see “No splash page” below.
How it works
MY_Config.php
contains an override of site_url()
: language segment added (when appropriate) to URLs generated with anchor()
, form_open()
...
Options
Special URIs
A special URI is not prefixed by a language. The root URI (/) is by default a special URI.
You might need other special URIs, like for an admin section, which would be in just one language.
In system/application/libraries/MY_Language.php
, add admin
to the $special
array. Now links to admin
won't be prefixed by the current language.
site_url('admin'); // http://mywebsite.com/admin
No splash page
In system/application/libraries/MY_Language.php
-
remove
””
from the$special
array -
set
$default_uri
to something else likehome
-
now a request to
/
will be redirected toen/home
, if English is your default language -
the default language is the first item of the
$languages
array
Add a new language
-
system/application/libraries/MY_Language.php
: add new language to$languages
array
// example: German (de) 'de' => 'german',
-
config/routes.php
: add new routes
// example: German (de) $route['^de/(.+)$'] = "$1"; $route['^de$'] = $route['default_controller'];
-
create corresponding language folder in
system/application/language
. For this “German” example, it would be calledgerman
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述