简体   繁体   English

在codeigniter中翻译多语言网站的路线(url)

[英]Translate routes (url ) for multilanguage website in codeigniter

I've started using frameworks(CodeIgniter) recently and after lots of research still have many doubts , specially regarding a multilingual website. 我最近开始使用框架(CodeIgniter),经过大量研究仍存在很多疑问,尤其是对于多语言网站。

I'm trying to "translate" the url , for instance, I have en/home and fr/home , but I'd like to have for each language its own translation like fr/accueil , pt/inicio , and so on... So far all my system is doing is to change the language but the url(controller) remains in the same language(english): en/home , fr/home , pt/home instead of fr/accueil and pt/inicio 我正在尝试“翻译” URL,例如,我有en/homefr/home ,但我希望每种语言都有自己的翻译,例如fr/accueilpt/inicio等等。 ..到目前为止,我的系统所做的只是更改语言,但url(controller)仍使用相同的语言(英语): en/homefr/homept/home而不是fr/accueilpt/inicio

What do I have to do to achieve this sort of "translation" or routing? 我必须怎么做才能实现这种“翻译”或路由? Is it better to create a controller for each language and call them separately? 为每种语言创建一个控制器并分别调用它们会更好吗? I started doing that but at some point I realized it was extremely massive and probably it isn't a good practice. 我开始这样做,但在某个时候我意识到它非常庞大,可能不是一个好习惯。 But I found much easier to create a controller for each language, but then I don't know where and in which file I have to set an "auto run function" to detect/save the language preferences. 但是我发现为每种语言创建一个控制器要容易得多,但是我不知道我必须在哪里以及在哪个文件中设置“自动运行功能”来检测/保存语言首选项。

Also when I type just the language in my url ( eg /en , /fr ) it doesn't "redirect" to the main controller (home): 另外,当我只在URL中键入语言时(例如/en/fr ),它不会“重定向”到主控制器(主页):

// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|fr|pt)$'] = $route['default_controller'];

This is not working for me unless I type " / " after the language: /fr/ works fine!, /fr doesn't work! 除非我在以下语言后键入“ / ”,否则这对我不起作用: /fr/正常!, /fr不起作用!

I successfully use the approach of setting a cookie for the language instead of having it in the URI, then having all URI segments set in my pages database, for each language, then in my page model I load the correct language URI and text based on the language cookie. 我成功地使用了为语言设置cookie的方法,而不是将其存储在URI中,然后在页面数据库中为每种语言设置了所有URI段,然后在页面模型中,根据以下内容加载了正确的语言URI和文本:语言Cookie。

Now the only remaining problem is routing, I set something like this in the end of my routes file, redirecting all remaining requests to the page controller method: 现在剩下的唯一问题是路由,我在路由文件的末尾设置了类似的内容,将所有剩余的请求重定向到页面控制器方法:

$route['(:any)'] = "main/pages";
$route['(:any)/(:any)'] = "main/pages";

This way I can have both page URI and submeny URIs with my localized version. 这样,我既可以具有页面URI,也可以具有本地化版本的子菜单URI。

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

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