JMSI18nRoutingBundle and translatable routes catalog location - symfony

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.

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

configure jms_i18n_routing for InvalidConfigurationException

I want to use the JMSi18nRoutingBundle. So i already have installed it in my project. Now when i try to run the server the commandline says:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "default_locale" at path "jms_i18n_routing" must be configured.
But the path in this file is dynamic and will be only changed through the setPath() method. So where and how i have to configure it?
ok solved it!
the configuration to the JMSi18nRoutingBundle:
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: prefix
need to be written in the config.yml. Additionally to this, the Translator in the Config has to be enabled. That´s all.

Symfony2 - integration of ckeditor and sonata media bundle

I am trying to integarte IvoryCKEditor with sonata media bundle. Perpose is to allow image uploads in ckeditor. I tried it using a CoopTilleulsCKEditorSonataMediaBundle but i keep getting an error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_sonata_media_media_ckeditor_browser" as such route does not exist.") in "IvoryCKEditorBundle:Form:ckeditor_widget.html.twig".
Thanks :)
You should check your routes with the command:
$ php app/console debug:router | grep ckeditor
Then you have to replace admin_sonata_media_media_ckeditor_browser and admin_sonata_media_media_ckeditor_upload by routes founded with this command, in the config of ivory_ck_editor (maybe in your config.yml or your ivory_ckeditor.yml file)
If you don't have route for ckeditor, I think you should check if you install correctly the bundle.
1) install SonataFormatterBundle 2) add the bundle and its dependencies in AppKernel 3) config files 4) CLEAR CACHE 5) roll
Reason of the issue
This problem arises when trying to integrate CKEditor through the
SonataFormatterBundle without using the SonataAdminBundle.
In fact, the integration proposed by the SonataFormatterBundle is meant to only work for the SonataAdminBundle, and no easy integration for a custom admin bundle is currently available.
Note that this dependency is not specified in the documentation at the moment.
How to solve the problem
Simply install the SonataAdminBundle following this installation process. You do need to configure the bundle entirely as specified in the documentation. To add the routes that were missing, such as admin_sonata_media_media_ckeditor_browser, simply add the following to your config/routes.yml:
# This is your custom admin bundle
admin:
resource: "#AdminBundle/Controller/"
type: annotation
prefix: /admin/
# Import SonataAdminBundle routes
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin/sonata # put whatever prefix here
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
Just to add something.
If you are SURE that you have installed, all dependencies, and set all configs correctly, and still getting this error, then the cause may be that you have defined your 'sonata_media' prefix in routing.yml differently.
You can check all urls in your project in:
app/cache/dev/appDevUrlGenerator.php
In there you will find ALL routes in a variable $declaredRoutes
Afterwards simply put correct route name for browsing.
It will not work without SonataMediaBundle - you will need to install it with SonataFormatterBundle
Sonata become too complicated as for me, so I just use IvoryCKEditorBundle directly

JMS I18n routes translations are ignored

I'm trying to use the JMSI18nRoutingBundle to translate my routes. I've installed and configured it like it's shown in the documentation :
composer.json:
"jms/i18n-routing-bundle": "1.1.*#dev",
AppKernel.php:
new JMS\I18nRoutingBundle\JMSI18nRoutingBundle(),
new JMS\TranslationBundle\JMSTranslationBundle(),
And config.yml:
jms_i18n_routing:
default_locale: %locale%
locales: [en, fr]
catalogue: routes
strategy: custom
Then I'm trying to extract the translations for the routes. The command given in the doc
php app/console translation:extract fr --enable-extractor=jms_i18n_routing
doesn't work at all, but I found this command on internet that works better:
php app/console translation:extract fr --enable-extractor=jms_i18n_routing --bundle="TeduleCoreBundle" --domain="routes"
With this, the translations are extracted in a routes.fr.yml file in AcmeCoreBundle/Resources/translations. The file contains the translations for all the routes in all the locales, instead of only the routes for the AcmeCoreBundle in the fr locale like specified in the command, but oh well, it works...
However, when I do a php app/console router:debug, none of the routes are translated. Instead, I end up with something like this:
en_fr__RG__tedule_app_account_profile ANY ANY ANY /myaccount
en__RG__tedule_app_account_profile ANY ANY ANY /myaccount
fr__RG__tedule_app_account_profile ANY ANY ANY /myaccount
en_fr__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
en__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
fr__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
en_fr__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
en__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
fr__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
As you can see, each route appears 3 times (en_fr, en and fr), and the pattern isn't translated according to what I wrote in the routes.fr.yml file.
I tried clearing the cache, putting the routes.fr.yml file in app/Resources/translations, separating the translations between two files routes.fr.yml and routes.en.yml, nothing works...
That happens to me too but I solved it by moving the translation files of the routes to the "app/Resources/translations"
This works to me:
php app/console translation:extract en_US --bundle=MyBundle --enable-extractor=jms_i18n_routing --output-format=xliff --domain=routes --keep --output-dir=app/Resources/translations

Symfony2 translation: locale is ignored

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/

Resources