嗨,我找到了我的问题的解决方案,我将分享它。我将把整个国际化过程。
首先我在我的 config.php 中添加
$config['seo_language'] = TRUE; //This will put language in the url
$config['languages'] = array('bg'=>'bulgarian','en'=>'english');
其次,我做了钩子,所以我可以为 config.php 中的每个控制器自动加载语言
$config['enable_hooks'] = TRUE;
并在 config/hooks.php 中注册我们的钩子
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);
所以现在 hooks/ 文件夹中的类 LanguageLoader.php
class LanguageLoader
{
function initialize() {
$ci =& get_instance();
$ci->load->helper('language');
$class = $ci->router->class;
if($class!='language'){
//Get all available languages which are set in our config file.
$languages = config_item('languages');
/* Try all */
//Try url
$module = $ci->router->fetch_module();
$position = !empty($module) ? 2 : 1;
$url_lang = trim(strtolower($ci->uri->segment($position)));
//Try session
$session_lang = $ci->session->userdata('site_lang');
//Try post
$post_lang = @$_POST['site_lang'];
//Try cookie
$cookie_lang = $ci->input->cookie('site_lang');
//The lang with highest priority is that one which is coming from url
$found = false;
if(!empty($url_lang)){
if(array_key_exists($url_lang, $languages)){
$lang = $url_lang;
$found = true;
if($cookie_lang!=$url_lang){
//Set it in cookie and session, so we can used later if is not in the url
$cookie = array(
'name' => 'site_lang',
'value' => $url_lang,
'expire' => 7*24*86500,
'secure' => false
);
$ci->input->set_cookie($cookie);
//add lang in session as well
$ci->session->set_userdata('site_lang', $url_lang);
}
}
}
//Second is post cookie
if(!$found and !empty($cookie_lang)){
if(array_key_exists($cookie_lang, $languages)){
$lang = $cookie_lang;
$found = true;
}
}
//Third one is post
if(!$found and !empty($post_lang)){
if(array_key_exists($post_lang, $languages)){
$lang = $post_lang;
$found = true;
}
}
//Last check in the session
if(!$found and !empty($session_lang)){
if(array_key_exists($session_lang, $languages)){
$lang = $session_lang;
$found = true;
}
}
//If still we dont have language, load default one
if(!$found){
$default_language = config_item('language');
$lang = array_search($default_language, $languages);
$ci->load->language($class, $default_language);
}else{
$ci->load->language($class, $languages[$lang]);
}
//Define global variable we need this for our view helper to build our seo links
define('SITE_LANG', $lang);
}
}
}
接下来是替换 site_url 函数。在 application/core/ 如果您不使用模块化扩展,请删除前两行并取消注释第三行
require APPPATH."third_party/MX/Config.php";
class MY_Config extends MX_Config {
//class MY_Config extends CI_Config {
function site_url($uri = '')
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}
if (function_exists('get_instance'))
{
$CI =& get_instance();
$uri = $CI->lang->localized($uri);
}
return parent::site_url($uri);
}
}
替换 application/helpers my_language_helper.php 中的 lang 助手
function lang($line, $id = '')
{
$CI =& get_instance();
$line = $CI->lang->line($line);
$args = func_get_args();
if(is_array($args)) array_shift($args);
if(is_array($args) && count($args))
{
foreach($args as $arg)
{
$line = str_replace_first('%s', $arg, $line);
}
}
if ($id != '')
{
$line = '<label for="'.$id.'">'.$line."</label>";
}
return $line;
}
function str_replace_first($search_for, $replace_with, $in)
{
$pos = strpos($in, $search_for);
if($pos === false)
{
return $in;
}
else
{
return substr($in, 0, $pos) . $replace_with . substr($in, $pos + strlen($search_for), strlen($in));
}
}
还有 routers.php 中的最后一个
$route['^(en|bg)/(.+)$'] = "$2";
$route['^(en|bg)$'] = $route['default_controller'];
//MODULE ADMIN - It Has to be added for each module
$route['admin/(en|bg)'] = 'admin';
$route['admin/(en|bg)/(:any)'] = 'admin/$2';
$route['admin/(:any)'] = 'admin/$1';
我希望它会帮助任何想要在不使用 i18n 的情况下将其网站国际化的人