如何翻译$ url_handlers?

时间:2016-08-26 09:01:48

标签: silverstripe

我有一种情况需要为不同的国家/地区翻译以下$url_handlers

因此,在英文网站上,网址如下所示:http://website.com/gyms/boston/group-training

我需要能够翻译网址的“群组培训”部分。我已经使用_t()方法翻译了网站的其余部分。

我目前的设置:

class GymLocationPage_Controller extends Page_Controller {

    private static $allowed_actions = array(
        'currentSpecials',
        'sevenDayFreeTrial',
        'groupTraining'
    );

    private static $url_handlers = array(
        'current-specials' => 'currentSpecials',
        'trial' => 'sevenDayFreeTrial',
        'group-training' => 'groupTraining'
    );


}

如何实现这一目标?

1 个答案:

答案 0 :(得分:6)

您可以在控制器的init()函数内更新配置,执行以下操作:

public function init() {

    parent::init();

    // Define your translated actions.
    $translatedCurrentSpecials   = _t('Actions.CURRENT_SPECIALS', 'aktuella-kampanjer');
    $translatedSevenDayFreeTrial = _t('Actions.SEVEN_DAY_TRIAL',  'sjudagars-prova-pa-period');

    // Define your url handlers.
    $urlHandlers           = $this->config()->url_handlers;
    $translatedUrlHandlers = [
        $translatedCurrentSpecials   => 'currentSpecials',
        $translatedSevenDayFreeTrial => 'sevenDayFreeTrial'
    ];

    // Update the config.
    Config::inst()->update(
        $this->class, 
        'url_handlers', 
        $translatedUrlHandlers + $urlHandlers // Important to prepend and not append.
    );

}