schmittjoh / JMSI18nRoutingBundle

Allows you to internationalize your routing
http://jmsyst.com/bundles/JMSI18nRoutingBundle
358 stars 159 forks source link

Change default locale from database , is that possible ? #163

Open atrimech opened 9 years ago

atrimech commented 9 years ago

Hi all,

I have add a new configuration in my backend web site, that i can change the language of all website, so, it's work perfectly, but what i want to do now, is the web site open with the language set on database, but i'm already using JMSi18nRoutingBundle, where my default locale is set fr for exemple, how can i tell to JMSi18nRoutingBundle config to change the default locale from database.

Is that possible ?

migmolrod commented 9 years ago

My guess is you'll need to play with EventListeners and Dependency Injection. I'm not exactly an expert in those matters, but I'll try to help anyway.

Taking a quick look at JMSI18nRoutingBundle code, you can find this: https://github.com/schmittjoh/JMSI18nRoutingBundle/blob/master/EventListener/LocaleChoosingListener.php and specially https://github.com/schmittjoh/JMSI18nRoutingBundle/blob/master/Router/DefaultLocaleResolver.php

You should be able to make your own LocaleResolver (a class that implements LocaleResolverInterface) and inject the entity manager (@doctrine.entity_manager) into it. Then you can inject your LocaleResolver to an onRequest event or something similar to the above-mentioned LocaleChoosingListener. So on each request, you have your LocaleResolver (that relies on entity manager) to look into the database for the default locale

As per the bundle itself, I think what you want to do could be added as a feature. I mean, adding a config option in JMSI18nRoutingBundle, maybe something called jms_i18n_routing.locale_resolver, which gave us the oportunity to choose our own LocaleResolver class or service to be injected in the JMSI18nRoutingBundle/EventListener/LocaleChossingListener.php

migmolrod commented 9 years ago

This is actually a duplicated of https://github.com/schmittjoh/JMSI18nRoutingBundle/issues/150