10

When using my app in a foreign country, the google GMSGeocoder is returning the response in local language automatically. how can I set it to always return the the response in English?

Im using GMS SDK 1.7 and my code is something like this:

GMSGeocoder *geoCoder = [[GMSGeocoder alloc] init];


[geoCoder reverseGeocodeCoordinate:self.cellLocation.coordinate completionHandler:^(GMSReverseGeocodeResponse *respones, NSError *err) {
    if([respones firstResult]) {

        GMSAddress* address = [respones firstResult];
        NSString* fullAddress = [NSString stringWithFormat:@"%@, %@",address.thoroughfare, address.locality];

        self.theTextField.text = fullAddress; 

    } else {
        self.theTextField.text = @"";
    }
}];
jszumski
  • 7,430
  • 11
  • 40
  • 53
Boaz Saragossi
  • 958
  • 10
  • 32

2 Answers2

1

Using a GMSGeocoder category can solve this issue, inspired by @DaNLtR After that , It can set geocoder result as English .

@implementation GMSGeocoder (Load)

+(void)load {
    [[self class] setUserLanguage:@"en-CN"];// set your wanted language.
    NSLog(@"GMSGeocoder + load!");
}
- (void)dealloc {
    [[self class] resetSystemLanguage];
    NSLog(@"GMSGeocoder + dealloc!");
}
+ (void)setUserLanguage:(NSString *)userLanguage
{

    if (!userLanguage.length) {
        [[self class] resetSystemLanguage];
        return;
    }

    [[NSUserDefaults standardUserDefaults] setValue:userLanguage forKey:@"UserLanguage"];
    [[NSUserDefaults standardUserDefaults] setValue:@[userLanguage] forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize]; 
}

+ (void)resetSystemLanguage
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserLanguage"];
    [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

Why should in category?

A:I tested setLanguage: before GMSGeocoder reverseGeocodeCoordinate method, it can't affect geocoder result. After I saw DaNLtR's answer , I think we can setLanguge in load method.

Why should reset language?

A:Avoide affect other module or framework .

Levi Han
  • 21
  • 5
  • Don't use `-[NSUserDefaults synchronize]`. From [Apple's documentation](https://developer.apple.com/documentation/foundation/nsuserdefaults/1414005-synchronize?language=objc) _"this method is unnecessary and shouldn't be used."_ – Ashley Mills Oct 11 '18 at 14:28
0

Just change it to your language parameters

int main(int argc, char * argv[])
{
    @autoreleasepool {

        //For Google Maps hebrew response
        //[[NSUserDefaults standardUserDefaults] setObject:@[@"en"] forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] setObject:@[@"he",@"he-IL"] forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }
}
DaNLtR
  • 561
  • 5
  • 21