【问题标题】:SilverStripe Translatable ModuleSilverStripe 可翻译模块
【发布时间】: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"&gt;

正如预期的那样,美国版主页的法文版显示,反之亦然。 但是,当我从带有法语浏览器的法语计算机访问该站点时,它会直接进入英文页面。我原以为这将由 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


【解决方案1】:

silverstripe Translatable 有两个关键项目可以回答您的问题。

  1. 系统中的每个页面都有一个唯一的 URL
  2. 您设置了默认语言环境

严格来说,对于 1 和 2,您可以将语言环境作为 _GET 变量传递 - 但这不是您的访问者会做的。

因此,简单的答案是,仅 Translatable 无法确定要显示的语言环境 - 但您可以通过基于任何方法更改默认语言环境来做到这一点...

一旦你有了这个,只需在 _config.php 中设置它...

Translatable::set_default_locale(<locale>);

【讨论】:

  • 这让我越界了。谢谢您的帮助。我将用我的确切代码更新我的问题
  • "you set the default locale"...好吧,您(或扩展程序)在每个页面请求上设置当前语言环境,不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多