Symfony @Route annotation i18n: Is it possible to have a fallback for languages not explicitly declared?

Viewed 49

Using the @Route annotation in Symfony I can do the following:

/**
 * @Route({
 *     "en": "/{_locale}/registration",
 *     "de": "/{_locale}/registrierung",
 * },
 * name="registration",
 * defaults={
 *     "_locale":"%kernel.default_locale%"
 * },
 * requirements={
 *     "_locale":"^[a-z]{2}?$"
 * })
 */
public function defaultAction() { }

Visitors with english language can call www.mysite.com/en/registration and visitors with german language can call www.mysite.com/de/registrierung then.

Is it possible to declare a default for languages not explicitly declared? Such that a route for e.g. es or fr visitors works like www.mysite.com/es/reg or www.mysite.com/fr/reg?

Pseudo-Code:

/**
 * @Route({
 *     "en": "/{_locale}/registration",
 *     "de": "/{_locale}/registrierung",
 *     "_default_": /{_locale}/reg"
 * },
 * name="registration",
 * defaults={
 *     "_locale":"%kernel.default_locale%"
 * },
 * requirements={
 *     "_locale":"^[a-z]{2}?$"
 * })
 */
public function defaultAction() { }
1 Answers

Currently it impossible to do it via one annotation. However you could create second fallback action. In addition them could be sorted by priority.

Example

/**
 * @Route({
 *     "en": "/{_locale}/registration",
 *     "de": "/{_locale}/registrierung",
 * },
 * name="registration",
 * defaults={
 *     "_locale":"%kernel.default_locale%"
 * },
 * priority="1",
 * requirements={
 *     "_locale":"^[a-z]{2}?$"
 * })
 */
public function defaultAction() {}

/**
 * @Route("/{_locale}/registration",
 * name="registration_fallback",
 * priority="0",
 * requirements={
 *     "_locale":"^[a-z]{2}?$"
 * })
 */
public function fallbackDefaultAction() {
    $this->defaultAction();
}
Related