Codeigniter语言 [英] Codeigniter language
问题描述
我想创建一个多语言网站,但我不想让该语言出现在URI像example.com/fr/about(我不想这样)。我只是想改变文本语言。我的问题是,我做的第一个加载语言是永远。为什么?
如果我这样做:
I wanna make a multilanguage website, but I do not want that the language appear in URI like example.com/fr/about (I do not want this). I just want to change the text language. My problem is that the first load language that I do is for ever. why? If I do:
$this->config->set_item(‘language’,‘english’);
$this->lang->load(‘messages’);
$this->config->set_item(‘language’,‘french’);
$this->lang->load(‘messages’);
或
$this->lang->load(‘messages’,‘english’);
$this->lang->load(‘messages’,‘french’);
如何解决此问题?
我的配置语言自动加载为空。
My config language autoload is empty.
感谢您的帮助。
推荐答案
我使用钩子。
function pick_language() {
require_once(APPPATH.'/config/language.php');
session_start();
// Lang set in URL via ?lang=something
if(!empty($_GET['lang']))
{
// Turn en-gb into en
$lang = substr($_GET['lang'], 0, 2);
$_SESSION['lang_code'] = $lang;
}
// Lang has already been set and is stored in a session
elseif( !empty($_SESSION['lang_code']) )
{
$lang = $_SESSION['lang_code'];
}
// Lang has is picked by a user.
// Set it to a session variable so we are only checking one place most of the time
elseif( !empty($_COOKIE['lang_code']) )
{
$lang = $_SESSION['lang_code'] = $_COOKIE['lang_code'];
}
// Still no Lang. Lets try some browser detection then
else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
{
// explode languages into array
$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));
// Check them all, until we find a match
foreach ($accept_langs as $lang)
{
// Turn en-gb into en
$lang = substr($lang, 0, 2);
// Check its in the array. If so, break the loop, we have one!
if(in_array($lang, array_keys($config['supported_languages'])))
{
break;
}
}
}
// If no language has been worked out - or it is not supported - use the default
if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
{
$lang = $config['default_language'];
}
// Whatever we decided the lang was, save it for next time to avoid working it out again
$_SESSION['lang_code'] = $lang;
// Load CI config class
$CI_config =& load_class('Config');
// Set the language config. Selects the folder name from its key of 'en'
$CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);
// Sets a constant to use throughout ALL of CI.
define('CURRENT_LANGUAGE', $lang);
}
不仅会为您设置正确的语言,
Not only will that set the correct language for you but it will give you a constant CURRENT_LANGUAGE which contains the language they are using ('en', 'de', etc).
可用的语言来自一个配置项目:
The available languages for this come from a config item:
/*
|--------------------------------------------------------------------------
| Supported Languages
|--------------------------------------------------------------------------
|
| Contains all languages your site will store data in. Other languages can
| still be displayed via language files, thats totally different.
|
| Check for HTML equivalents for characters such as � with the URL below:
| http://htmlhelp.com/reference/html40/entities/latin1.html
|
*/
$config['supported_languages'] = array(
'en'=> array('name' => 'English', 'folder' => 'english'),
'es'=> array('name' => 'Español', 'folder' => 'spanish'),
'fr'=> array('name' => 'Français', 'folder' => 'french'),
'de'=> array('name' => 'German', 'folder' => 'german')
);
/*
|--------------------------------------------------------------------------
| Default Language
|--------------------------------------------------------------------------
|
| If no language is specified, which one to use? Must be in the array above
|
| en
|
*/
$config['default_language'] = 'en';
这将从GET中获取正确的语言( http://somesite.com/?lang=de ),然后检查会话变量(由正确的匹配填充),然后检查浏览器是否接受 - lang header。
This will pick up the correct language from GET (http://somesite.com/?lang=de), then check session variable (populated by a correct match) then checks the browser for accept-lang header.
将使用先匹配的那一个。
Whichever has a match first will be used.
这篇关于Codeigniter语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!