I want different host and different routes in my multicountry web with Symfony 3. I used the JMS i18n routing for this.
My config YML file:
jms_i18n_routing:
default_locale: es_ES
locales: [es_ES, fr_FR, en_GB]
strategy: custom
hosts:
es_ES: www.aaa.local
fr_FR: www.aaa-fr.local
en_GB: www.aaa-uk.local
redirect_to_host: true
With this, I can use different host correctly, but I don't know how I can "translate" the routes, f.ex: /contacto /contact and /contact.
I want load differents routing.yml depends on the locale.
When using the JMSI18nRoutingBundle your routes are being Translated automatically based on your route_name using the routes domain (Symfony Translation Component).
So, assumed your route_name is contact, just create a Translations like this:
id/source: contact
domain: routes
locale: en
translation/target: /contact
id/source: contact
domain: routes
locale: es
translation/target: /contacto
Now your routes using the Symfony router (which is wrapped by the one from the bundle) will return automatically the translated route from the current locale.
If you want to translate it to a specific locale, just add the route parameter _locale like this:
{{ path("contact", {"_locale": "es"}) }}
or
$this->get('router')->generate('contact', array('_locale' => 'es'));
Related
Hello I come to because I need a little help,
I want to translate my website into 3 languages: en, fr, es
I translated everything in the translation file but the problem is not how to display the choice of language on the website and also how to display it in the url
in my balise html :
<html lang="{{ app.request.locale }}">
and my paramters.yaml :
parameters:
locale: fr
in my translation file I have 3 files like this :
messages.fr.yml
messages.en.yml
messages.es.yml
I want to display a URL like this
www.mydomaine.com/fr/accueil
www.mydomaine.com/en/home
www.mydomaine.com/es/acogida
And how show lists of the links? Towards it is languages ?
Well, the following solution is tested on symfony 3.4, might be a bit different for symfony 4.* versions.
In your config.yml set all the languages site will operate on under parameters option, like this:
parameters:
locale: fr
locales: [en, fr, es]
Again in config.yml set the translator fallback locale & paths to your translations:
framework:
translator:
fallback: "%locale%"
paths: ['%kernel.root_dir%/../src/AppBundle/Resources/translations']
Then define your routes this way (yml definition example):
home:
path: /{_locale}/home
defaults: { _controller: AppBundle:Default:home }
Then all may access your home page with every locale you provided (answering question how to display language in the url).
www.mydomaine.com/fr/home
www.mydomaine.com/en/home
www.mydomaine.com/es/home
Now if the question's is also about how to get translated content on your web site page - You can use different file formats for keeping translations, I will go again with .yml as for example. Usually translations are put to files like messages.en.yml (as for other languages, create same files with different locale, e.g. messages.fr.yml).
Then assuming you have a key foo in your translations files, e.g. foo: bar, you may display its value in twig using trans filter: {{ 'foo'|trans({}, 'messages')} }}.
More information you may find in official documentation
I am creating a Symfony 4 web app that supports multiple languages. The routing is done, so I can have /en/suchpage or /de/suchpage. I am now looking for a way to link to "suchpage" using the right locale.
Routing looks as follows:
suchpage:
path: /{_locale}/suchpage/
controller: App\Controller\SimplePageController::such
requirements:
maw: '%app.locales%'
defaults:
_locale: '%locale%'
I am using Twig, and it is from Twig templates that I need to be able to link to the right version of a page. This "works":
{{ path('suchpage', {'_locale': app.request.locale}) }}
It has two issues though:
I do not want to be repeating this all over the place. Something like {{ page('suchpage') }} would be much better. Does a function like this exist? Can path() be made to behave like this?
This fails if the locale was not specified in the request, as can happen, since it is not required on all pages (since some have a default).
There is a maintened bundle, JMSI18nRoutingBundle, to achieve what you need.
composer require jms/i18n-routing-bundle
Some usefull links about this bundle :
Packagist : https://packagist.org/packages/jms/i18n-routing-bundle
GitHub : https://github.com/schmittjoh/JMSI18nRoutingBundle
I have a Symfony2 project with multiple brandings (subdomains). Each subdomain should use the same site functions and the same routes (with different hostname). Therefor I want to make the subdomain in the route path to a variable. Now it looks like:
mybundle_route:
resource: "#MyProjectBundle/Controller/"
type: annotation
prefix: /
host: "{subdomain}.%base_host%"
requirements:
subdomain: sub1|sub2
and the routing itself works. But if I try to generate some routes (e.g. in twig), I get following error because no subdomain variable is set:
Some mandatory parameters are missing ("subdomain") to generate a URL for route "company_route".
I don't want to add the parameter to each path() function in the project.
It works perfekt for one subdomain if I add the defaults like this:
defaults:
subdomain: sub1
but the routes are wrong for the other subdomains. Also it doesn't work with
defaults:
subdomain: "%subdomain%"
All I need is to pass the %subdomain% variable to the defaults or to set it somewhere in the controller constructor. But I can't find the way how it is going. I would appreciate any help.
If there is no better solution you could try to set %subdomain% parameter inside configuration file:
Create subdomain_parameter.php inside config folder.
Inside this file you have access to ther $container so you can manually retrieve current subdomain from the url and pass it to the container.
Import subdomain_parameter.php in the config.yml
I'm creating a multi language website and I'm using BeSimple/BeSimpleI18nRoutingBundle for route translations. Route in my config looks like that:
about:
locales: { en: "/{_locale}/about-us", lt: "/{_locale}/apie-mus" }
defaults: { _controller: BaseBundle:Base:about }
However in this case I get duplicate route as if I enter /en/about-us or /lt/about-us it works in both ways. but in the second way it should be 404 error because the route for lt locale should be /lt/apie-mus which also works fine.
Is there any way of fixing that? Or maybe any better ways of translating routes?
However in this case I get duplicate route
There is one route per language:
$ app/console router:debug
about.en ANY ANY ANY /{_locale}/about-us
about.lt ANY ANY ANY /{_locale}/apie-mus
When you are on the URL /lt/about-us the UrlMatcher will match the en version:
$ app/console router:match /lt/about-us
Route "about.en" matches
[router] Route "about.en"
Name about.en
Path /{_locale}/about-us
Path Regex #^/(?P<_locale>[^/]++)/about\-us$#s
Host ANY
Host Regex
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: AppBundle:Default:index
_locale: en
Requirements NO CUSTOM
Options compiler_class: Symfony\Component\Routing\RouteCompiler
In this case, the local will be en and not lt.
But I don't think that you should worry about it because the only way that a visitor will visite /lt/about-us is by editing the URL himself.
After battle with BeSimple I switched my project to JMSI18nRoutingBundle in 10 minutes. It works well and resolves problem with routes. Firstly: it containes multiple modes of prefixation (with locale) and secondly it allows to translate every route.
My Silex routes are defined in a routing.yml config file.
In my php code I would like to add some new routes dynamically and I want these routes to have a higher priority than the routes defined in routing.yml.
Currently I'm adding my routes as in the following example, but they are added to the bottom, i.e. with the lowest priority.
$this->app->match('/page/{slug}', array($this, 'record'))
->bind('extrapages')
->method('GET|POST');
This route for example never gets matched because there is a route in routing.yml that matches the following path:
path: /{contenttypeslug}/{slug}
How do add my new routes above the existing routes?
As an aside I'm using the Bolt CMS, which is built on Silex, and trying to add these new routes in a bolt extension. As this question is about Silex routing the fact that I'm using Bolt shouldn't make a great deal of difference.
It's not a very clean solution but whenever I've overruled an extension's route in my routing.yml, I just add the extension's route to routing.yml again, pointing it to my extension code. If you put it near the top, it will get used, because in Silex routes are parsed 'top down'. First match gets used.
sitemap:
path: /sitemap
defaults:
_controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemap'
I apologize for the hackyness of this solution.