Skip to content

Commit 58b7f9c

Browse files
authored
Feature/set locale (#467)
* Add setLocale
1 parent 273ee8a commit 58b7f9c

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

LeanplumSDK/LeanplumSDK/Classes/Internal/Leanplum.m

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,8 @@ + (void)startWithUserId:(NSString *)userId
862862
// Setup parameters.
863863
NSString *versionName = [self appVersion];
864864
UIDevice *device = [UIDevice currentDevice];
865-
NSLocale *currentLocale = [NSLocale currentLocale];
866-
NSString *currentLocaleString = [NSString stringWithFormat:@"%@_%@",
867-
[[NSLocale preferredLanguages] objectAtIndex:0],
868-
[currentLocale objectForKey:NSLocaleCountryCode]];
865+
NSString *currentLocaleString = [self.locale localeIdentifier];
866+
869867
// Set the device name. But only if running in development mode.
870868
NSString *deviceName = @"";
871869
if ([LPConstantsState sharedState].isDevelopmentModeEnabled) {
@@ -2249,6 +2247,35 @@ + (void)setTrafficSourceInfoInternal:(NSDictionary *)info
22492247
[[LPRequestSender sharedInstance] send:request];
22502248
}
22512249

2250+
+ (NSLocale *)systemLocale {
2251+
static NSLocale * _systemLocale = nil;
2252+
if (!_systemLocale) {
2253+
NSLocale *currentLocale = [NSLocale currentLocale];
2254+
NSString *currentLocaleString = [NSString stringWithFormat:@"%@_%@",
2255+
[[NSLocale preferredLanguages] objectAtIndex:0],
2256+
[currentLocale objectForKey:NSLocaleCountryCode]];
2257+
_systemLocale = [[NSLocale alloc] initWithLocaleIdentifier:currentLocaleString];
2258+
}
2259+
return _systemLocale;
2260+
}
2261+
2262+
static NSLocale * _locale;
2263+
+ (NSLocale *)locale
2264+
{
2265+
return _locale ?: self.systemLocale;
2266+
}
2267+
2268+
+ (void)setLocale:(NSLocale *)locale
2269+
{
2270+
_locale = locale;
2271+
if ([self hasStarted]) {
2272+
LPRequest *request = [LPRequestFactory setUserAttributesWithParams:@{
2273+
LP_KEY_LOCALE: [locale localeIdentifier]
2274+
}];
2275+
[[LPRequestSender sharedInstance] send:request];
2276+
}
2277+
}
2278+
22522279
+ (void)advanceTo:(NSString *)state
22532280
{
22542281
[self advanceTo:state withInfo:nil];

LeanplumSDK/LeanplumSDK/Classes/Leanplum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,12 @@ NS_SWIFT_NAME(setUserId(_:attributes:));
526526
+ (void)setTrafficSourceInfo:(NSDictionary *)info
527527
NS_SWIFT_NAME(setTrafficSource(info:));
528528

529+
/**
530+
* Updates a user locale after session start.
531+
*/
532+
+(void)setLocale:(NSLocale *)locale
533+
NS_SWIFT_NAME(setLocale(_:));
534+
529535
/**
530536
* @{
531537
* Advances to a particular state in your application. The string can be

LeanplumSDKApp/LeanplumSDKTests/Classes/Extensions/Leanplum+Extensions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
@interface Leanplum(UnitTest)
3333

34+
+ (NSLocale *)systemLocale;
35+
3436
+ (void)reset;
3537

3638
+ (void)maybePerformActions:(NSArray *)whenConditions

LeanplumSDKApp/LeanplumSDKTests/Classes/LeanplumTest.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,47 @@ - (void) test_set_device_id_after_start
10751075
XCTAssertTrue([[LPAPIConfig sharedConfig].deviceId isEqualToString:deviceId]);
10761076
}
10771077

1078+
/**
1079+
* Tests setting the user locale before Leanplum start
1080+
*/
1081+
- (void) test_set_locale_before_start
1082+
{
1083+
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];
1084+
1085+
XCTestExpectation *request_expectation = [self expectationWithDescription:@"request_expectation"];
1086+
[LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
1087+
XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[Leanplum.systemLocale localeIdentifier]]);
1088+
XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
1089+
[request_expectation fulfill];
1090+
}];
1091+
1092+
[Leanplum setLocale:locale];
1093+
XCTAssertTrue([LeanplumHelper start_development_test]);
1094+
[self waitForExpectations:@[request_expectation] timeout:1.0];
1095+
}
1096+
1097+
/**
1098+
* Tests setting the user locale after Leanplum start
1099+
*/
1100+
- (void) test_set_locale_after_start
1101+
{
1102+
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];
1103+
1104+
XCTestExpectation *request_expectation = [self expectationWithDescription:@"request_expectation"];
1105+
[Leanplum onStartResponse:^(BOOL success) {
1106+
XCTAssertTrue(success);
1107+
[Leanplum setLocale:locale];
1108+
[LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
1109+
XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[Leanplum.systemLocale localeIdentifier]]);
1110+
XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
1111+
[request_expectation fulfill];
1112+
}];
1113+
}];
1114+
1115+
XCTAssertTrue([LeanplumHelper start_development_test]);
1116+
[self waitForExpectations:@[request_expectation] timeout:1.0];
1117+
}
1118+
10781119
/**
10791120
* Tests track with events of same type , priority and countdown.
10801121
*/

0 commit comments

Comments
 (0)