Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow locale change with chain #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2015
Merged

Allow locale change with chain #408

merged 1 commit into from
Feb 17, 2015

Conversation

AntoineLemaire
Copy link
Contributor

Need to change the locale dynamically in PHP for after use it with the CacheProvider of GeocoderBundle

@@ -38,6 +40,9 @@ public function geocode($address)
{
$exceptions = [];
foreach ($this->providers as $provider) {
if($provider instanceof LocaleAwareProvider && $this->getLocale() !== null){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use the coding standards of this project?

  • 1 space before (
  • null !== ... rather than the other way around

Does it make sense to check whether locale is null? It does not really matter IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if registered providers are configured with a locale?
ping @Baachi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willdurand This should not be a problem, because the chainprovider delegate each call.
Change the locale dynamicaly isn't supported yet. And in my opinion, such functionality made this code more complex.

If someone need this functionality, just create more than one provider like provider_de, provider_en and so on.
I think the Geocoder class should be decide which provider should be used not the provider itself. A provider should be invisible for the end user. And if someone really need this functionality, here you go we are in OOP so extend the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is when using GeocoderBundle.
For exemple, I got a default locale for Bing Map

providers:
    bing_maps:
       api_key:  ~
       locale:    fr_FR

I'd like some times set a different locale, but if not, it should use the default one, not erase the default by null

@willdurand
Copy link
Member

ah right, then fix CS and I'll merge this PR :)

@AntoineLemaire
Copy link
Contributor Author

Done ! Sorry for this mistake.

@@ -38,6 +40,9 @@ public function geocode($address)
{
$exceptions = [];
foreach ($this->providers as $provider) {
if ($provider instanceof LocaleAwareProvider && $this->getLocale() !== null){
$provider->setLocale($this->getLocale());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can lead to an logical error, see this example:

$bing = new BingMaps($http, 'secret', 'de_DE'); // note the locale here
$chain = new Chain([$bing]);

$geocoder = new Geocoder();
$geocoder->registerProvider($bing);
$geocoder->registerProvider($chain);

$geocoder->geocode('Paris'); // Will now use de_DE locale
$chain->setLocale('en_US') // Chain now use en_US
$geocoder->use('chain')->geocoder('Paris'); // I get en_US results

$geocoder->geocode('Paris'); // I expect the locale is de_DE, but it will be en_US because the chain provider will override them.

I would suggest to clone the provider or set the original locale back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@toin0u
Copy link
Member

toin0u commented Feb 16, 2015

👍

@willdurand
Copy link
Member

@AntoineLemaire could you please squash your commits?

Fix coding standards

Prevent logical error

#408 (comment)
@AntoineLemaire
Copy link
Contributor Author

Done ! Thanks

Baachi added a commit that referenced this pull request Feb 17, 2015
Allow locale change with chain
@Baachi Baachi merged commit 0a65fca into geocoder-php:master Feb 17, 2015
@Baachi
Copy link
Member

Baachi commented Feb 17, 2015

Thank you very much for your contribution!

@willdurand
Copy link
Member

\o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants