Symfony2 default locale in routing

前端 未结 12 2289
猫巷女王i
猫巷女王i 2020-12-01 04:41

I have a problem with routing and the internationalization of my site built with Symfony2.
If I define routes in the routing.yml file, like this:

exampl         


        
12条回答
  •  醉话见心
    2020-12-01 05:20

    We created a custom RoutingLoader that adds a localized version to all routes. You inject an array of additional locales ['de', 'fr'] and the Loader adds a route for each additional locale. The main advantage is, that for your default locale, the routes stay the same and no redirect is needed. Another advantage is, that the additionalRoutes are injected, so they can be configured differently for multiple clients/environments, etc. And much less configuration.

    partial_data                       GET      ANY      ANY    /partial/{code}
    partial_data.de                    GET      ANY      ANY    /de/partial/{code}
    partial_data.fr                    GET      ANY      ANY    /fr/partial/{code}
    

    Here is the loader:

    projectDir = $projectDir;
        $this->additionalLocales = $additionalLocales;
    }
    
    public function load($resource, $type = null)
    {
        $collection = new RouteCollection();
        // Import directly for Symfony < v4
        // $originalCollection = $this->import($resource, 'annotation')
        $originalCollection = $this->getOriginalRouteCollection($resource);
        $collection->addCollection($originalCollection);
    
        foreach ($this->additionalLocales as $locale) {
            $this->addI18nRouteCollection($collection, $originalCollection, $locale);
        }
    
        return $collection;
    }
    
    public function supports($resource, $type = null)
    {
        return self::NAME === $type;
    }
    
    private function getOriginalRouteCollection(string $resource): RouteCollection
    {
        $resource = realpath(sprintf('%s/src/Controller/%s', $this->projectDir, $resource));
        $type = 'annotation';
    
        return $this->import($resource, $type);
    }
    
    private function addI18nRouteCollection(RouteCollection $collection, RouteCollection $definedRoutes, string $locale): void
    {
        foreach ($definedRoutes as $name => $route) {
            $collection->add(
                $this->getI18nRouteName($name, $locale),
                $this->getI18nRoute($route, $name, $locale)
            );
        }
    }
    
    private function getI18nRoute(Route $route, string $name, string $locale): Route
    {
        $i18nRoute = clone $route;
    
        return $i18nRoute
            ->setDefault('_locale', $locale)
            ->setDefault('_canonical_route', $name)
            ->setPath(sprintf('/%s%s', $locale, $i18nRoute->getPath()));
    }
    
    private function getI18nRouteName(string $name, string $locale): string
    {
        return sprintf('%s.%s', $name, $locale);
    }
    }
    

    Service definition (SF4)

    App\Routing\I18nRoutingLoader:
        arguments:
            $additionalLocales: "%additional_locales%"
        tags: ['routing.loader']
    

    Routing definition

    frontend:
        resource: ../../src/Controller/Frontend/
        type: i18n_annotation #localized routes are added
    
    api:
        resource: ../../src/Controller/Api/
        type: annotation #default loader, no routes are added
    

提交回复
热议问题