Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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() { }
like image 252
Blackbam Avatar asked Dec 05 '25 18:12

Blackbam


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();
}
like image 111
FallDi Avatar answered Dec 08 '25 06:12

FallDi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!