Skip to content
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

Feature/set locale #467

Merged
merged 5 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions LeanplumSDK/LeanplumSDK/Classes/Internal/Leanplum.m
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,8 @@ + (void)startWithUserId:(NSString *)userId
// Setup parameters.
NSString *versionName = [self appVersion];
UIDevice *device = [UIDevice currentDevice];
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *currentLocaleString = [NSString stringWithFormat:@"%@_%@",
[[NSLocale preferredLanguages] objectAtIndex:0],
[currentLocale objectForKey:NSLocaleCountryCode]];
NSString *currentLocaleString = [self.locale localeIdentifier];

// Set the device name. But only if running in development mode.
NSString *deviceName = @"";
if ([LPConstantsState sharedState].isDevelopmentModeEnabled) {
Expand Down Expand Up @@ -2249,6 +2247,35 @@ + (void)setTrafficSourceInfoInternal:(NSDictionary *)info
[[LPRequestSender sharedInstance] send:request];
}

+ (NSLocale *)systemLocale {
static NSLocale * _systemLocale = nil;
if (!_systemLocale) {
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *currentLocaleString = [NSString stringWithFormat:@"%@_%@",
[[NSLocale preferredLanguages] objectAtIndex:0],
[currentLocale objectForKey:NSLocaleCountryCode]];
_systemLocale = [[NSLocale alloc] initWithLocaleIdentifier:currentLocaleString];
}
return _systemLocale;
}

static NSLocale * _locale;
+ (NSLocale *)locale
{
return _locale ?: self.systemLocale;
}

+ (void)setLocale:(NSLocale *)locale
{
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the use case - would this be called before starting Leanplum or at some point after it started?
If it has started, the locale in the start API call will be the system one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was for calling it after Leanplum is started, in order to simplify a bit complex variations of starting.

If you think it is better to move it to start, and/or to add some stored property for overriding the system's locale, so the start would take that one, I'm ok with it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Improved in a way that setLocale now can be called both before and after Leanplum is started

_locale = locale;
if ([self hasStarted]) {
LPRequest *request = [LPRequestFactory setUserAttributesWithParams:@{
LP_KEY_LOCALE: [locale localeIdentifier]
}];
[[LPRequestSender sharedInstance] send:request];
}
}

+ (void)advanceTo:(NSString *)state
{
[self advanceTo:state withInfo:nil];
Expand Down
6 changes: 6 additions & 0 deletions LeanplumSDK/LeanplumSDK/Classes/Leanplum.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ NS_SWIFT_NAME(setUserId(_:attributes:));
+ (void)setTrafficSourceInfo:(NSDictionary *)info
NS_SWIFT_NAME(setTrafficSource(info:));

/**
* Updates a user locale after session start.
*/
+(void)setLocale:(NSLocale *)locale
NS_SWIFT_NAME(setLocale(_:));

/**
* @{
* Advances to a particular state in your application. The string can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

@interface Leanplum(UnitTest)

+ (NSLocale *)systemLocale;

+ (void)reset;

+ (void)maybePerformActions:(NSArray *)whenConditions
Expand Down
41 changes: 41 additions & 0 deletions LeanplumSDKApp/LeanplumSDKTests/Classes/LeanplumTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,47 @@ - (void) test_set_device_id_after_start
XCTAssertTrue([[LPAPIConfig sharedConfig].deviceId isEqualToString:deviceId]);
}

/**
* Tests setting the user locale before Leanplum start
*/
- (void) test_set_locale_before_start
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];

XCTestExpectation *request_expectation = [self expectationWithDescription:@"request_expectation"];
[LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[Leanplum.systemLocale localeIdentifier]]);
XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
[request_expectation fulfill];
}];

[Leanplum setLocale:locale];
XCTAssertTrue([LeanplumHelper start_development_test]);
[self waitForExpectations:@[request_expectation] timeout:1.0];
}

/**
* Tests setting the user locale after Leanplum start
*/
- (void) test_set_locale_after_start
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];

XCTestExpectation *request_expectation = [self expectationWithDescription:@"request_expectation"];
[Leanplum onStartResponse:^(BOOL success) {
XCTAssertTrue(success);
[Leanplum setLocale:locale];
[LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[Leanplum.systemLocale localeIdentifier]]);
XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
[request_expectation fulfill];
}];
}];

XCTAssertTrue([LeanplumHelper start_development_test]);
[self waitForExpectations:@[request_expectation] timeout:1.0];
}

/**
* Tests track with events of same type , priority and countdown.
*/
Expand Down