翻译Silverstripe登录表单和其他内置页面


Translate Silverstripe login form and other built in pages

我使用Silverstripe可翻译插件提供多种语言的网站。然而,我也想翻译内置的页面,如登录或密码重置页面。仅仅在URL的末尾应用?local=en_US似乎没有帮助,也没有办法用第二语言创建专门的登录页面。有办法让它工作吗?

这是一个解决方案。它的工作原理是扩展Controller类,这个类足够低,可以对诸如处理登录的Security之类的控制器产生影响。

它记住前一页的语言环境,然后使用它来呈现"内置页面"。

ControllerDecorator.php

<?php
class ControllerDecorator extends Extension {
    function onBeforeInit() {
        // If we're on a page, use its Locale information
        if($this->getOwner() instanceof ContentController) {
            $locale = $this->getOwner()->Locale;
            i18n::set_locale($locale);
            Cookie::set('Locale', $locale);
        }
        // Otherwise, use the stored Locale
        else if(Cookie::get('Locale')) {
            i18n::set_locale(Cookie::get('Locale'));
        }
    }
}

config.yml

Controller:
  extensions:
    - ControllerDecorator