简体   繁体   English

如何根据语言更改CodeIgniter的URI段

[英]How to change CodeIgniter's URI segments based on a language

Question: 题:
How could I make my URL language specific, for example: 如何使我的URL语言特定,例如:

  • English 英语
    www.website.com / controller-in-en / method-in-en www.website.com/zh-CN/controller-in-en
  • Other language 其他语言
    www.website.com / controller-in-oth-lang / method-in-oth-lang www.website.com/controller-in-oth-lang/method-in-oth-lang
    (I want this URL to be automatically redirected to www.website.com / controller-in-en / method-in-en based on a 'controller-in-en' and 'method-in-en' translation in a language file). (我希望根据语言文件中的“ en-controller-in-en”和“ in-method-en-en”翻译,将此URL自动重定向到www.website.com/controller-in-en/method-in-en )。

Additional information: 附加信息:
In the future, I plan to redirect users for different languages to different subdomains, eg lt.website.com, but right now I want URI segments to be translated depending on users language. 将来,我计划将不同语言的用户重定向到不同的子域,例如lt.website.com,但是现在我希望根据用户的语言来翻译URI段。

I want to make clear that: 我想明确指出:

  1. I already know how to create a language switcher and change language. 我已经知道如何创建语言切换器和更改语言。
  2. I DO NOT want language code to be included in the url, like '/en' or '/lt'. 我不希望语​​言代码包含在url中,例如'/ en'或'/ lt'。

My (bad) ideas: 我的(坏)想法:
I thought this could be done via translated routing addresses $route[lang('url_market')] = 'market/index'; 我认为可以通过转换后的路由地址完成$route[lang('url_market')] = 'market/index'; but when routing is done, language library is not yeat loaded. 但是在完成路由后,语言库尚未加载。

Based On @AdrienXL answer, Here's a sample without using a database. 基于@AdrienXL答案,这是不使用数据库的示例。

$config['routes_translation']['en'] = array(
'home' => 'home'
);

$config['routes_translation']['fr'] = array(
'home' => 'accueil'
);

Obviously, You know the language for the user using a session, And you have the controller name, You can get the translation easily by accessing the config item. 显然,您知道使用会话的用户所使用的语言,并且拥有控制器名称,您可以通过访问config项轻松获得翻译。

$translatedNamed = $config['routes_translation'][$language_code][$controller] 

which should give you 'home' for en & 'accueil' for fr 其中应该为您提供“住所”和“应计”的fr

The only way I see is through the db. 我看到的唯一方法是通过数据库。

Build a table that looks like this : 建立一个看起来像这样的表:

routes_translation routes_translation

*****************************************
*id |  controller   | translation | lang*
*****************************************
* 1 | home          |    home     | en  *
* 2 | home          |  accueil    | fr  *
                   ...
*****************************************

Then in your routes.php 然后在你的routes.php

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get('routes_translation');
$result = $query->result();
foreach( $result as $row )
{
    $route[$row->translation] = $row->controller;
} 

Simple.... 简单....

  1. Get the user select language either from db or store it in SESSION 从数据库获取用户选择的语言或将其存储在SESSION中
  2. Pass the selected laguage while loading the language library 加载语言库时传递所选语言

Eg: $this->lang->load("message",$SESSION['selected_language']); 例如:$ this-> lang-> load(“ message”,$ SESSION ['selected_language']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM