没想到啊

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  6 随笔 :: 379 文章 :: 97 评论 :: 24万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

全文: 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 and MY_Config.php in system/application/libraries

Configuration

  • You must be using pretty URLs (without index.php). With Apache it's usually achieved withmod_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

http://your_base_url/en/about

<p>I'm a man</p>
 
<p><a href="http://mywebsite.com/en/music">Shania Twain</a></p>

http://your_base_url/fr/about

<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 like home
  • now a request to / will be redirected to en/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 called german.
posted on   没想到啊  阅读(315)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 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的设计模式综述
点击右上角即可分享
微信分享提示