Add locale and requirements to all routes - Symfony2

后端 未结 6 540
离开以前
离开以前 2021-01-04 14:12

I created an application which was not intended to have translations, but now I decided to add this feature. The problem is that all my routes look like this:



        
6条回答
  •  梦谈多话
    2021-01-04 14:40

    Configure symfony for localization:

    Add localization to the session(please note that the convention is /locale/action):

    goodbye:
    
        pattern: /{_locale}/goodbye
        defaults: { _controller: AcmeBudgetTrackerBundle:Goodbye:goodbye, _locale: en }
        requirements:
            _locale: en|bg
    

    Alternatively locale can be set manually:

    $this->get('session')->set('_locale', 'en_US');
    

    app/config/config.yml

    framework:
        translator: { fallback: en }
    

    In your response:

    use Symfony\Component\HttpFoundation\Response;
    
    public function indexAction()
    {
        $translated = $this->get('translator')->trans('Symfony2 is great');
    
        return new Response($translated);
    }
    

    Configure localization messages localized files:

    messages.bg

    
    
        
            
                
                    Symfony2 is great
                    Symfony2 е супер
                
                
                    symfony2.great
                    Symfony2 е супер
                
            
        
    
    

    messages.fr

    
    
        
            
                
                    Symfony2 is great
                    J'aime Symfony2
                
                
                    symfony2.great
                    J'aime Symfony2
                
            
        
    
    

    More on the topic: Official symfony documentation

提交回复
热议问题