Symfony2 translation: locale is ignored - symfony

Some time ago, I started translating the Symfony 2.1 website I'm working on, using the "correct way" through /{ _locale } in the URL. Now the translation worked fine for most routes, but some of them (/login, /register, ...) kept coming in the default language (fr in this instance), because they didn't have the { _locale } part in their url.
Through the debug panel I realised that the locale was not actually changed through url navigation (unlike this had me believe).
So I then went there, where I found information and link to set up a listener in order to change the session locale in function of the path that is called.
All defaults locale declarations (both in config.yml and the listener itself) are "fr".
When I switch languages, I can actually see on the debug panel that the session locale changes. However, it would appear that the translation has somehow been broken : every translation bit is made in english, even though the default language is french, and despite the session locale being "fr".
Where could that come from? How can I fix it?
Thanks in advance.
More technical information:
routing.yml:
AuraeLCUserBundle:
resource: "#AuraeLCUserBundle/Resources/config/routing.yml"
prefix: /{_locale}/
defaults: { _locale: fr }
requirements:
_locale: en|fr
a twig example:
<p>{{ 'layout.greeting.welcome'|trans({}, 'AuraeLCUserBundle') }}</p>

3 things here are critical.
default lang (setted in config) and if your using translatable doctrine extension:
# app/config/config.yml
framework:
session: { default_locale: 'fr_FR' }
translator: { fallback: 'en' }
.....
stof_doctrine_extensions:
default_locale: 'fr_FR'
They must be different because of circular reference exception appears when the default locale won't go.
Translator is looking for files yaml|xml|gettext with sufix fr_FR ex:
messages.fr_FR.yml
messages.en.yml
yourcatalogue.fr_FR.yml
yourcatalogue.en.yml
Read this for more details:
Change default locale in Symfony2
http://symfony.com/doc/2.0/book/translation.html
http://0hlsson.se/2011/07/27/symfony2-and-translatable-example/

Related

Add optional _locale to routes

I'd like to prefix all my urls by an optional _locale. BTW, I'm handling fr and en.
The point is, when _locale is given I'll use it thanks to symfony Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest, if not I'll use the locale saved in the database thanks to my own listener (priority: 15) which runs immediately after the previous one.
What I've tried so far is the following:
app/config/routing.yml
entrypoint:
resource: 'locale_prefixed_routing.yml'
prefix: /{_locale}
And locale_prefixed_routing.yml imports bundles routings.
/fr/url/example works but /url/example does not and returns 404 page.
How do I tell symfony to use the locale in the url, if does not exist use the one in the database?
I suppose your routes are defined like this in your locale_prefixed_routing.yml :
my_route:
path: /
defaults:
Actually routes like /url/example without any locale are not defined in your routing because you ask to prefix all the routes loaded in locale_prefixed_routing.yml by the locale. So it is normal to get an error when requesting such a route without locale.
You can try to make another routing file named non_localized_routing.yml containing the same routes except that you will use specific defaults.
Example :
route_example:
path: /url/example
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/url/example
permanent: true
Tell me if it permits you to get rid of your problem, I haven't test it yet.
For me it sounds weird to store the locale in the database when the locale is not provided by the user. You can simply add a default one:
framework:
default_locale: en

JMSI18nRoutingBundle and translatable routes catalog location

I am using JMSI18nRoutingBundle for internalisation of the routes of an application and it is some nice piece of software but unfortunately not very well documented and I am not sure how to achieve the translation of the strings in the routes for the different locales regardless of the fact it is given as an example in the docs.
What I would like is to have (default local without prefix, all others translated and with locale prefix):
/contact
/de/kontakt
The problem is with the following configuration:
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: prefix_except_default
The routes are generated as:
/contact
/de/contact
I could not see a setting where to input kontakt for the de route, is it part of some special message catalog or per-route configuration option?
I found out that it is handled by the translations catalog. The routes are loaded from it. There is a command to extract them from the router and put them into the catalog:
php bin/console translation:extract LANGUAGE --bundle=BUNDLE --enable-extractor=jms_i18n_routing --output-format=xliff --dir=RESOURCE_FOLDER --domain=routes
Where LANGUAGE is the target language, BUNDLE is the bundle to extract the routes from and RESOURCE_FOLDER is where to put the translations.

Symfony2 translations loaded but not translated

Im using FOS RestBundle and JMSSerializer (maybe its doing something i dont know) and want to returns a translated message in json. I wanted to use the translator service however the trans() method not working.
$this->get('translator')->trans('translator.key.here')
The translations seem to be loaded correctly as i dumped translator and found there the translated message under the current locale and 'translator.key.here' key. However the trans function simply gives back the key string.
Any idea?
Edit: In twig its working perfectly.
In my messages.hu yml file:
translator:
key:
here: some message
In the config also set fallbacks:
translator: { fallbacks: [hu] }
default_locale: "%locale%"
When i dumped translator in the object i found under en locale the right message key pair:
"translator.key.here" => "some message"
Try this in parameters.yml:
locale: hu
and in config.yml:
framework:
translator: { fallbacks: ["%locale%"] }
default_locale: "%locale%"
and in translation folder create file: messages.hu.yml
Well i found a solution.. still dont know why not working simply but it works if i set directly the translation domain.
translator->trans('key.goes.here', array(), 'messages); if somebody has the some issue in the future.

Symfony2: How to validate locale thats coming from the url

I have implemented the locale in my symfony2 project. Now the problem is if i manually change the locale in the url to something other than locale values it is just taking that value. How do i restrict this?
For example: http://myproject.com/en changing to http://myproject.com/asdf .
Now asdf is not a locale value in my config! but still it displays in my locale switcher as Country-asdf!!
How do i restrict url editing of locale?
Thanks.
You can define the route restrictions in your routing . If it is YML ,
homepage:
pattern: /{locale}
defaults: { _controller: AcmeDemoBundle:Main:homepage, locale: en }
requirements:
culture: en|fr|in

Routing : Import a routing.yml resource with a prefix and requirements not work

I try to load a localized_routes.yml inside the main routing.yml file with :
localized_routes:
resource: "routing_localized.yml"
prefix: /{_locale}
requirements: { _locale : fr|es|it|de }
But it doesnt work at all, the requirements are not in the generated URL checks and so I can use any locale I want .... I see in the Symfony source that only the prefix are taken, not requirements or defaults values, is it an issue ?

Resources