【发布时间】:2016-03-29 01:09:25
【问题描述】:
我最近刚刚安装了 silverstripe Translatable 来为我们处理多语言网站。我想我已经正确设置了一切,但不能确定。也许我遗漏了一些东西,或者我误解了模块。
在我的站点 _config.php 我有以下内容
i18n::set_locale('en_US');
i18n::set_locale('fr_FR');
Translatable::set_default_locale('en_US');
SiteTree::add_extension('Translatable');
SiteConfig::add_extension('Translatable');
我已经浏览并创建了我们主页的翻译。我可以导航到翻译页面没问题。我还可以验证主页和翻译都意识到它们与Page.ss中的以下代码链接
<% if Translations %>
<% loop Translations %>
$Locale.RFC1766
$Link
<% end_loop %>
<% end_if %>
我还加了lang="$ContentLocale" xml:lang="$ContentLocale" xmlns= "http://www.w3.org/1999/xhtml">
正如预期的那样,美国版主页的法文版显示,反之亦然。 但是,当我从带有法语浏览器的法语计算机访问该站点时,它会直接进入英文页面。我原以为这将由 silverstripe 的 i18n 部分或可翻译自动处理。我需要自己处理这个还是我错过了什么?
基于 Barry 的回答 我最终在我的 _config.php 中得到了以下代码行
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; //Get the languag from the client
$lang = substr($lang,0,strpos($lang,','));//Strip out the unnecesary stuff after the comma
$lang = str_replace("-","_",$lang);//Replace the hyphen with an underscore.
Translatable::set_default_locale($lang);//Set locale
【问题讨论】:
-
Translatable::set_default_locale()用于设置 default 语言环境,而Translatable::set_current_locale()用于设置用于过滤 SQL 查询的 current 语言环境当前语言环境。您还可以使用Translatable::set_allowed_locales()在该站点上添加一组允许的语言环境。
标签: php silverstripe