在 codeigniter 中为多语言网站翻译路由 (url) [英] Translate routes (url ) for multilanguage website in codeigniter

查看:46
本文介绍了在 codeigniter 中为多语言网站翻译路由 (url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用框架(CodeIgniter),经过大量研究后仍然有很多疑问,特别是关于多语言网站.

I've started using frameworks(CodeIgniter) recently and after lots of research still have many doubts , specially regarding a multilingual website.

我正在尝试翻译" url ,例如,我有 en/homefr/home ,但我想为每个语言自己的翻译,如 fr/accueilpt/inicio 等等...到目前为止,我的系统所做的只是更改语言,但 url(控制器)保持相同的语言(英语):en/home , fr/home, pt/home 而不是 fr/accueilpt/inicio

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

我该怎么做才能实现这种翻译"或路由?为每种语言创建一个控制器并分别调用它们是否更好?我开始这样做,但在某些时候我意识到它非常庞大,可能这不是一个好的做法.但是我发现为每种语言创建控制器要容易得多,但是我不知道我必须在何处和在哪个文件中设置自动运行功能"来检测/保存语言首选项.

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.

此外,当我只在我的 url 中输入语言(例如 /en/fr )时,它不会重定向"到主控制器(主页):

Also when I type just the language in my url ( e.g /en , /fr ) it doesn't "redirect" to the main controller (home):

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

除非我在语言后输入/",否则这对我不起作用:/fr/ 工作正常!/fr 不起作用!

This is not working for me unless I type "/" after the language: /fr/ works fine!, /fr doesn't work!

推荐答案

我成功地使用了为语言设置 cookie 而不是将它放在 URI 中的方法,然后在我的页面数据库中设置所有 URI 段,为每个语言,然后在我的页面模型中,我根据语言 cookie 加载正确的语言 URI 和文本.

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.

现在唯一剩下的问题是路由,我在路由文件的末尾设置了这样的东西,将所有剩余的请求重定向到页面控制器方法:

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";

这样,我的本地化版本就可以同时拥有页面 URI 和子菜单 URI.

This way I can have both page URI and submeny URIs with my localized version.

这篇关于在 codeigniter 中为多语言网站翻译路由 (url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆