Skip to content

Commit

Permalink
fix locale string helper to work with chinese
Browse files Browse the repository at this point in the history
ios concepts of chinese locale identifiers don't
map cleanly onto what our servers expect

also, update the locale server identifier
to be an instance method (more sensible and
more testable)
  • Loading branch information
alexshepard committed Jul 15, 2023
1 parent dc83832 commit e819e70
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
4 changes: 1 addition & 3 deletions INaturalistIOS/API Endpoints/Node API/INatAPI.m
Expand Up @@ -44,9 +44,7 @@ - (void)requestMethod:(NSString *)method path:(NSString *)path query:(NSString *
components.query = query;
}

NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
// node expects locales like fr-FR not fr_FR
NSString *serverLocaleIdentifier = [localeIdentifier stringByReplacingOccurrencesOfString:@"_" withString:@"-"];
NSString *serverLocaleIdentifier = [[NSLocale currentLocale] inat_serverFormattedLocale];
NSURLQueryItem *localeQueryItem = [NSURLQueryItem queryItemWithName:@"locale" value:serverLocaleIdentifier];
if (components.queryItems) {
components.queryItems = [components.queryItems arrayByAddingObject:localeQueryItem];
Expand Down
8 changes: 2 additions & 6 deletions INaturalistIOS/API Endpoints/Node API/TaxaAPI.m
Expand Up @@ -131,9 +131,7 @@ - (void)suggestionsForObservationId:(NSInteger)observationId handler:(INatAPISug
components.path = [NSString stringWithFormat:@"/v1/computervision/score_observation/%ld", (long)observationId];;

// add locale to the request
NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
// node expects locales like fr-FR not fr_FR
NSString *serverLocaleIdentifier = [localeIdentifier stringByReplacingOccurrencesOfString:@"_" withString:@"-"];
NSString *serverLocaleIdentifier = [[NSLocale currentLocale] inat_serverFormattedLocale];
NSURLQueryItem *localeQueryItem = [NSURLQueryItem queryItemWithName:@"locale" value:serverLocaleIdentifier];
if (components.queryItems) {
components.queryItems = [components.queryItems arrayByAddingObject:localeQueryItem];
Expand Down Expand Up @@ -212,9 +210,7 @@ - (void)suggestionsForImage:(UIImage *)image location:(CLLocationCoordinate2D)co
NSData *imageData = UIImageJPEGRepresentation(thumb, 0.9);

// add locale to the request
NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
// node expects locales like fr-FR not fr_FR
NSString *serverLocaleIdentifier = [localeIdentifier stringByReplacingOccurrencesOfString:@"_" withString:@"-"];
NSString *serverLocaleIdentifier = [[NSLocale currentLocale] inat_serverFormattedLocale];
params[@"locale"] = serverLocaleIdentifier;

// use afnetworking to deal with icky multi-part forms
Expand Down
2 changes: 1 addition & 1 deletion INaturalistIOS/Extensions/NSLocale+INaturalist.h
Expand Up @@ -10,6 +10,6 @@

@interface NSLocale (INaturalist)

+ (NSString *)inat_serverFormattedLocale;
- (NSString *)inat_serverFormattedLocale;

@end
30 changes: 27 additions & 3 deletions INaturalistIOS/Extensions/NSLocale+INaturalist.m
Expand Up @@ -10,10 +10,34 @@

@implementation NSLocale (INaturalist)

+ (NSString *)inat_serverFormattedLocale {
- (NSString *)inat_serverFormattedLocale {
// start with the iOS locale identifier
NSString *serverLocaleIdentifier = [self localeIdentifier];

// iOS gives us zh-Hans_cn, server expects zh-CN
// iOS can also give us zh_SG and zh_MY but we want zh-CN
if ([serverLocaleIdentifier containsString:@"zh-Hans"]) {
serverLocaleIdentifier = @"zh-CN";
} else if ([serverLocaleIdentifier isEqualToString:@"zh_SG"]) {
serverLocaleIdentifier = @"zh-CN";
} else if ([serverLocaleIdentifier isEqualToString:@"zh_MY"]) {
serverLocaleIdentifier = @"zh-MY";
}

// iOS gives us zh-Hant_tw, server expects zh-TW
if ([serverLocaleIdentifier containsString:@"zh-Hant"]) {
if ([serverLocaleIdentifier containsString:@"TW"]) {
serverLocaleIdentifier = @"zh-TW";
} else if ([serverLocaleIdentifier containsString:@"HK"]) {
serverLocaleIdentifier = @"zh-HK";
} else if ([serverLocaleIdentifier containsString:@"MO"]) {
serverLocaleIdentifier = @"zh-MO";
}
}

// iOS gives us en_US, server expects en-US
NSString *localeString = [[NSLocale currentLocale] localeIdentifier];
NSString *serverLocaleIdentifier = [localeString stringByReplacingOccurrencesOfString:@"_" withString:@"-"];
serverLocaleIdentifier = [serverLocaleIdentifier stringByReplacingOccurrencesOfString:@"_" withString:@"-"];

return serverLocaleIdentifier;
}

Expand Down
8 changes: 4 additions & 4 deletions INaturalistIOS/Helpers/LoginController.m
Expand Up @@ -23,6 +23,7 @@
#import "PeopleAPI.h"
#import "ExploreUserRealm.h"
#import "ExploreTaxonRealm.h"
#import "NSLocale+INaturalist.h"

static const NSTimeInterval LocalMeUserValidTimeInterval = 600;

Expand Down Expand Up @@ -94,10 +95,9 @@ - (void)createAccountWithEmail:(NSString *)email
username:(NSString *)username
site:(NSInteger)siteId
license:(NSString *)license {

NSString *localeString = [[NSLocale currentLocale] localeIdentifier];
// format for rails
localeString = [localeString stringByReplacingOccurrencesOfString:@"_" withString:@"-"];

NSString *localeString = [[NSLocale currentLocale] inat_serverFormattedLocale];

// default to english
if (!localeString) { localeString = @"en-US"; }

Expand Down

0 comments on commit e819e70

Please sign in to comment.