Skip to content

Commit 604d60b

Browse files
author
Lukas Koebis
committed
Push Translation
added locale/language detection localeIdentifier gets automatically populated whenever an installation is saved/updated
1 parent f02bd53 commit 604d60b

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

Parse/Internal/Installation/Constants/PFInstallationConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ extern NSString *const PFInstallationKeyAppName;
1717
extern NSString *const PFInstallationKeyAppVersion;
1818
extern NSString *const PFInstallationKeyAppIdentifier;
1919
extern NSString *const PFInstallationKeyTimeZone;
20+
extern NSString *const PFInstallationKeyLocaleIdentifier;
2021
extern NSString *const PFInstallationKeyBadge;
2122
extern NSString *const PFInstallationKeyChannels;

Parse/Internal/Installation/Constants/PFInstallationConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
NSString *const PFInstallationKeyAppVersion = @"appVersion";
1818
NSString *const PFInstallationKeyAppIdentifier = @"appIdentifier";
1919
NSString *const PFInstallationKeyTimeZone = @"timeZone";
20+
NSString *const PFInstallationKeyLocaleIdentifier = @"localeIdentifier";
2021
NSString *const PFInstallationKeyBadge = @"badge";
2122
NSString *const PFInstallationKeyChannels = @"channels";

Parse/Internal/Installation/PFInstallationPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
@property (nonatomic, copy, readwrite) NSString *deviceType;
2929
@property (nonatomic, copy, readwrite) NSString *installationId;
3030
@property (nonatomic, copy, readwrite) NSString *timeZone;
31+
@property (nonatomic, copy, readwrite) NSString *localeIdentifier;
3132

3233
@end

Parse/PFInstallation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ PF_ASSUME_NONNULL_BEGIN
8585
*/
8686
@property (PF_NULLABLE_PROPERTY nonatomic, copy, readonly) NSString *timeZone;
8787

88+
/*!
89+
@abstract The localeIdentifier for the `PFInstallation` ([language code]-[COUNTRY CODE]).
90+
*/
91+
@property (PF_NULLABLE_PROPERTY nonatomic, copy, readonly) NSString *localeIdentifier;
92+
8893
/*!
8994
@abstract The channels for the `PFInstallation`.
9095
*/

Parse/PFInstallation.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ + (void)initialize {
3838
protectedKeys = PF_SET(PFInstallationKeyDeviceType,
3939
PFInstallationKeyInstallationId,
4040
PFInstallationKeyTimeZone,
41+
PFInstallationKeyLocaleIdentifier,
4142
PFInstallationKeyParseVersion,
4243
PFInstallationKeyAppVersion,
4344
PFInstallationKeyAppName,
@@ -107,6 +108,7 @@ @implementation PFInstallation
107108
@dynamic installationId;
108109
@dynamic deviceToken;
109110
@dynamic timeZone;
111+
@dynamic localeIdentifier;
110112
@dynamic channels;
111113
@dynamic badge;
112114

@@ -199,6 +201,12 @@ - (void)setTimeZone:(NSString *)timeZone {
199201
[self _setObject:timeZone forKey:PFInstallationKeyTimeZone onlyIfDifferent:YES];
200202
}
201203

204+
- (void)setLocaleIdentifier:(NSString *)localeIdentifier {
205+
[self _setObject:localeIdentifier
206+
forKey:PFInstallationKeyLocaleIdentifier
207+
onlyIfDifferent:YES];
208+
}
209+
202210
- (void)setChannels:(NSArray *)channels {
203211
[self _setObject:channels forKey:PFInstallationKeyChannels onlyIfDifferent:YES];
204212
}
@@ -256,6 +264,7 @@ - (void)_updateAutomaticInfo {
256264
[self _updateTimeZoneFromDevice];
257265
[self _updateBadgeFromDevice];
258266
[self _updateVersionInfoFromDevice];
267+
[self _updateLocaleIdentifierFromDevice];
259268
}
260269
}
261270
}
@@ -301,6 +310,37 @@ - (void)_updateVersionInfoFromDevice {
301310
}
302311
}
303312

313+
/*!
314+
@abstract Save localeIdentifier in the following format: [language code]-[COUNTRY CODE].
315+
316+
@discussion The language codes are two-letter lowercase ISO language codes (such as "en") as defined by
317+
<a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1</a>.
318+
The country codes are two-letter uppercase ISO country codes (such as "US") as defined by
319+
<a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3">ISO 3166-1</a>.
320+
321+
Many iOS locale identifiers don't contain the country code -> inconsistencies with Android/Windows Phone.
322+
*/
323+
- (void)_updateLocaleIdentifierFromDevice {
324+
NSLocale *currentLocale = [NSLocale currentLocale];
325+
NSString *language = [currentLocale objectForKey:NSLocaleLanguageCode];
326+
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
327+
328+
if (language.length == 0) {
329+
return;
330+
}
331+
332+
NSString *localeIdentifier = nil;
333+
if (countryCode.length > 0) {
334+
localeIdentifier = [NSString stringWithFormat:@"%@-%@", language, countryCode];
335+
} else {
336+
localeIdentifier = language;
337+
}
338+
339+
if (localeIdentifier.length > 0 && ![localeIdentifier isEqualToString:self.localeIdentifier]) {
340+
self.localeIdentifier = localeIdentifier;
341+
}
342+
}
343+
304344
///--------------------------------------
305345
#pragma mark - Data Source
306346
///--------------------------------------

0 commit comments

Comments
 (0)