全文: 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.phpandMY_Config.phpinsystem/application/libraries
Configuration
-
You must be using pretty URLs (without index.php). With Apache it's usually achieved with
mod_rewritethrough 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.phptosystem/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$specialarray -
set
$default_urito 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
$languagesarray
Add a new language
-
system/application/libraries/MY_Language.php: add new language to$languagesarray
// 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.

浙公网安备 33010602011771号