基于域的语言切换(base_url 切换)- 最佳实践是什么?
问:我有一个多语言网站。通过检查基本 url 自动切换语言的情况下,我们假设 www.myapp.fr (法语)和www.myapp.es(西班牙语)。我使用辅助函数来相应地执行 $this->config->set_item('base_url', $domain)
。我的问题是,我应该将辅助函数放置在哪里,最好的范围,这样用户就不会陷入使用错误的网址 www.domain.es ? MY_Controller 构造函数?
(*) 通常,人们对多语言网站只有一个域名。就我而言,我有相同的主机,有两个域名指向它。根据用户请求 uri,我进行切换!
我正在重写一个执行此操作的网站,并且运行良好。但我记得有一些问题,因为 base_url 没有正确切换,用户在 ES 域中使用 FR 语言导航(示例)。那时,我在每个控制器上都没有助手,没有 DRY 好的实践,所以我想,这就是问题所在。但 90% 的效果都很好!
Q: I've got a multilanguage site. Where the language is switched automatically by checking the base url, let's assume www.myapp.fr (for French) and www.myapp.es (for Spanish). I use a helper function to do $this->config->set_item('base_url', $domain)
accordingly. My question is, where should I place the helper function, the best scope, so that the user is not trapped into seeing the French site, with the wrong url www.domain.es ? MY_Controller constructor ?
(*) Usually people have one domain name for a multilanguage site. In my case, I've got same hosting with two domain names pointing to it. Based on the user request uri, I do the switch!
I'm rewriting a website that does this, and is working fine. But I remember having a few issues, because the base_url wasn't switched properly and users where navigating in language FR while in domain ES (example). In that time, I didn't had a helper on each controller, no DRY good practice, so I guess that, this was the problem. But 90% it worked fine!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要自己设置base_url。
CI 2.0 以后不需要设置base_url。
您可以使用此代码来设置 base_url
现在,您不再需要使用 base_url 了。您可以有一个钩子来根据域名确定使用哪种语言。
Don't set base_url yourself.
CI 2.0 onwards you don't need to set base_url.
you can use this code to set the base_url
Now, that you are free from base_url. You can have a hook to determine what language to use based on domain name.