2
votes

I'm using symfony2.8. I made translation yaml files for couple lang (en|de|cz). Then I added requirements to routing:

(global) app\config\routing.yml

comflex_w2:
    resource: "@ComflexW2Bundle/Resources/config/routing.yml"
    defaults: { _controller: ComflexW2Bundle:Home:index, _locale: en } 
    prefix:   /{_locale}
    requirements:
        _locale: en|de|cz

and inside bundle src\Comflex\W2Bundle\Resources\config\routing.yml

comflex_w2_home:
    pattern: /
    defaults: { _controller: ComflexW2Bundle:Home:index}


comflex_w2_login_check:
    path: /login_check
    defaults: { _controller: ComflexW2Bundle:User:authorization }

Now, when I go to host/web/app_dev.php/de or .../en, or .../cz then transations are worked, but when request doesn't contain locale like host/web/app_dev.php, then I get

"No route found for "GET /" with 404 error

How to set locale in url automatically from session files (only after user logged) or from browser local or finally set as en when nobody is logged.

I found entry default_locale: "%locale%" in app\config\config.yml but i have no idea how it's work and where "%locale% set is.

1
You get error because _locale is required in route _locale: en|de|cz. This parameter than defines locale in your application. Look at this question stackoverflow.com/q/34109527/5298034, same problem. - Max P.

1 Answers

0
votes

You don't have a route defined for '/', that is the reason why you get this error.

When you are working with locale prefixes in your urls, it's always a good idea to redirect the user from / to a locale-prefixed url, e.g. /en/.

You should add a controller that matches / and redirects to the localized home url:

public function redirectAction(Request $request)
{
    $locale = $request->getPreferredLanguage(['en', 'de', 'cz']);

    return $this->redirectToRoute('comflex_w2_home', ['_locale' => $locale], 301);
}