How to translate $url_handlers?

筅森魡賤 提交于 2019-12-21 05:52:03

问题


I have a situation where I need to translate the following $url_handlers for different countries.

So on an english site the URL looks like this: http://website.com/gyms/boston/group-training

I need to be able to translate the "group-training" part of the URL. I have translated the rest of the site using the _t() method throughout.

My current setup:

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'
    );


}

How would one achieve this?


回答1:


You could update the config inside the controller's init() function, doing something like this:

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.
    );

}


来源:https://stackoverflow.com/questions/39162402/how-to-translate-url-handlers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!