Git Product home page Git Product logo

symfony-i18n-routing's Introduction

Internationalized routing for Symfony 4

This bundle provides i18n routing for Symfony 4.

Author Build Status Coverage Status Software License Packagist Version Total Downloads

Purpose

This bundle provides a method of internationalization of route definitions. This means you can define a path per locale and still have them route to the same controller action.

Installation

composer req frankdejonge/symfony-i18n-routing

Register the bundle in bundles.php

<?php

return [
    FrankDeJonge\SymfonyI18nRouting\I18nRoutingBundle::class => ['all' => true],
    // ...
];

Note that if you want to use the annotations you'll need to ensure this bundle is loaded BEFORE the FrameworkBundle.

Configuration

frankdejonge_i18n_routing:
    default_locale: en
    use_annotations: false # set to true to enable annotation loading

Yaml usage

From your main config/routes.yml import your localized routes:

i18n_routes:
    resource: ./i18n_routes/routes.yml
    type: i18n_routes

Now you can define i18n routes in config/i18n_routes/routes.yml:

contact:
    controller: ContactController::formAction
    locales:
        en: /send-us-an-email
        nl: /stuur-ons-een-email

This is effectively the same as defining:

contact.en:
    controller: ContactController::formAction
    path: /send-us-an-email
    defaults:
        _locale: en

contact.nl:
    controller: ContactController::formAction
    path: /stuur-ons-een-email
    defaults:
        _locale: nl

As you can see this saves you a bit of typing and prevents you from having to keep 2 definitions in sync (less error prone).

Annotation usage

The annotation loader supports both normal route annotations and localized ones. The @I18nROute and @Route annotations can be be mixed at will.

<?php

use FrankDeJonge\SymfonyI18nRouting\Routing\Annotation\I18nRoute;

class ContactController
{
    /**
     * @I18nRoute({"en": "/send-us-an-email", "nl": "/stuur-ons-een-email"}, name="contact") 
     */
    public function formAction()
    {
        
    }
}

/** 
 * @Route("/prefix") 
 */
class PrefixedContactController
{
    /**
     * @I18nRoute({"en": "/send-us-an-email", "nl": "/stuur-ons-een-email"}, name="prefix_contact") 
     */
    public function formAction()
    {
        
    }
}

Generating routes

Generating routes can be done using the specified route name.

<?php
/** @var UrlGeneratorInterface $urlGenerator */
$urlWithCurrentLocale = $urlGenerator->generate('contact');
$urlWithSpecifiedLocale = $urlGenerator->generate('contact', ['_locale' => 'nl']);

symfony-i18n-routing's People

Contributors

cleentfaar avatar frankdejonge avatar jeroenvdgulik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jeroenvdgulik

symfony-i18n-routing's Issues

Annotation !!

hi i try to use with SF4 i deed al config but it das not see the name route ?

Config YAML =================
frankdejonge_i18n_routing:
    default_locale: '%locale%'
    use_annotations: true # set to true to enable annotation loading


Controller ======================
use FrankDeJonge\SymfonyI18nRouting\Routing\Annotation\I18nRoute;
.....
    /**
     * @I18nRoute({"en": "/error", "fr": "/erreur"}, name="SEO_maintenance")
     * @param Request                $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function maintenanceAction( Request $request )
    {
        $exception = $request->getSession()->get("exception");

        return $this->render('Core/exception/maintenance.html.twig', array(
            'ex'=> $exception,
        ));
    }

In listener ================
...........
                switch ($mode) {
                    case "main":

                        $url = $this->router->generate("SEO_maintenance");
                        break;
                    case "cons":
                        $url = $this->router->generate("SEO_maintenance");
                        break;

                    default:
                }
........

ERROR ===========

Unable to generate a URL for the named route "SEO_maintenance" as such route does not exist.

thank for your help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.