ifusion
ifusion

Reputation: 2233

How to translate $url_handlers?

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?

Upvotes: 3

Views: 181

Answers (1)

jjjjjjjjjjjjjjjjjjjj
jjjjjjjjjjjjjjjjjjjj

Reputation: 3118

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

}

Upvotes: 6

Related Questions