Skip to content

Commit e24076e

Browse files
authored
Merge pull request #948 from OneSignal/feature/set_language
Feature app defined setLanguage
2 parents c91fdfe + 8b197c5 commit e24076e

21 files changed

+737
-5
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 66 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import "LanguageProvider.h"
30+
31+
NS_ASSUME_NONNULL_BEGIN
32+
33+
@interface LanguageContext : NSObject
34+
35+
- (void)setStrategy:(NSObject<LanguageProvider>*)strategy;
36+
37+
@property (nonatomic, nonnull)NSString* language;
38+
39+
@end
40+
41+
NS_ASSUME_NONNULL_END
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "LanguageContext.h"
29+
#import "LanguageProvider.h"
30+
#import "OneSignalUserDefaults.h"
31+
#import "OneSignalHelper.h"
32+
#import "LanguageProviderDevice.h"
33+
#import "LanguageProviderAppDefined.h"
34+
35+
@implementation LanguageContext {
36+
37+
NSObject<LanguageProvider> *strategy;
38+
}
39+
40+
- (instancetype)init {
41+
self = [super init];
42+
let languageAppDefined = [OneSignalUserDefaults.initStandard getSavedStringForKey:OSUD_LANGUAGE defaultValue:nil];
43+
if (languageAppDefined == nil) {
44+
strategy = [LanguageProviderDevice new];
45+
} else {
46+
strategy = [LanguageProviderAppDefined new];
47+
}
48+
return self;
49+
}
50+
51+
- (void)setStrategy:(NSObject<LanguageProvider>*)newStrategy {
52+
strategy = newStrategy;
53+
}
54+
55+
- (NSString *)language {
56+
return strategy.language;
57+
}
58+
@end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
@protocol LanguageProvider <NSObject>
29+
30+
@property (readonly, nonnull)NSString* language;
31+
32+
@end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import "LanguageProvider.h"
30+
31+
NS_ASSUME_NONNULL_BEGIN
32+
33+
@interface LanguageProviderAppDefined : NSObject<LanguageProvider>
34+
35+
@property (nonnull)NSString* language;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
#import "LanguageProviderAppDefined.h"
28+
#import "OneSignalUserDefaults.h"
29+
#import "OneSignalHelper.h"
30+
#import "LanguageContext.h"
31+
32+
@implementation LanguageProviderAppDefined
33+
34+
- (void)setLanguage:(NSString *)language {
35+
[OneSignalUserDefaults.initStandard saveStringForKey:OSUD_LANGUAGE withValue:language];
36+
}
37+
38+
- (NSString *)language {
39+
return [OneSignalUserDefaults.initStandard getSavedStringForKey:OSUD_LANGUAGE defaultValue:DEFAULT_LANGUAGE];
40+
}
41+
42+
@end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import "LanguageProvider.h"
30+
31+
NS_ASSUME_NONNULL_BEGIN
32+
33+
@interface LanguageProviderDevice : NSObject<LanguageProvider>
34+
35+
@property (readonly, nonnull)NSString* language;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "LanguageProviderDevice.h"
29+
#import "OneSignalHelper.h"
30+
#import "LanguageContext.h"
31+
32+
@implementation LanguageProviderDevice
33+
34+
- (NSString *)language {
35+
let preferredLanguages = [NSLocale preferredLanguages];
36+
if (preferredLanguages && preferredLanguages.count > 0)
37+
return [preferredLanguages objectAtIndex:0];
38+
return DEFAULT_LANGUAGE;
39+
}
40+
41+
@end

iOS_SDK/OneSignalSDK/Source/OSStateSynchronizer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ withSMSAuthHashToken:(NSString * _Nullable)hashToken
7676
withSuccess:(OSMultipleSuccessBlock _Nullable)successBlock
7777
onFailure:(OSMultipleFailureBlock _Nullable)failureBlock;
7878

79+
- (void)updateLanguage:(NSString * _Nonnull)language
80+
appId:(NSString * _Nonnull)appId
81+
withSuccess:(OSUpdateLanguageSuccessBlock _Nullable)successBlock
82+
withFailure:(OSUpdateLanguageFailureBlock _Nullable)failureBlock;
83+
7984
@end

iOS_SDK/OneSignalSDK/Source/OSStateSynchronizer.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,41 @@ - (void)callSMSSuccessBlockOnMainThread:(OSSMSSuccessBlock)successBlock withSMSN
381381
});
382382
}
383383
}
384+
385+
- (void)updateLanguage:(NSString * _Nonnull)language
386+
appId:(NSString * _Nonnull)appId
387+
withSuccess:(OSUpdateLanguageSuccessBlock)successBlock
388+
withFailure:(OSUpdateLanguageFailureBlock)failureBlock {
389+
let stateSyncronizer = [self getStateSynchronizers];
390+
let requests = [NSMutableDictionary new];
391+
for (OSUserStateSynchronizer* userStateSynchronizer in stateSyncronizer) {
392+
requests[userStateSynchronizer.getChannelId] = [userStateSynchronizer setLanguage:language withAppId:appId];
393+
}
394+
395+
[OneSignalClient.sharedClient executeSimultaneousRequests:requests withSuccess:^(NSDictionary<NSString *,NSDictionary *> *results) {
396+
if (results[OS_PUSH] && results[OS_PUSH][OS_SUCCESS] && [results[OS_PUSH][OS_SUCCESS] boolValue]) {
397+
[OneSignalUserDefaults.initStandard saveStringForKey:OSUD_LANGUAGE withValue:language];
398+
}
399+
else if (results[OS_EMAIL] && results[OS_EMAIL][OS_SUCCESS] && [results[OS_EMAIL][OS_SUCCESS] boolValue]) {
400+
[OneSignalUserDefaults.initStandard saveStringForKey:OSUD_LANGUAGE withValue:language];
401+
}
402+
else if (results[OS_SMS] && results[OS_SMS][OS_SUCCESS] && [results[OS_SMS][OS_SUCCESS] boolValue]) {
403+
[OneSignalUserDefaults.initStandard saveStringForKey:OSUD_LANGUAGE withValue:language];
404+
}
405+
else {
406+
NSError *error = [NSError errorWithDomain:@"com.onesignal.language" code:0 userInfo:@{@"error" : @"Network Error"}];
407+
failureBlock(error);
408+
return;
409+
}
410+
411+
if (successBlock)
412+
successBlock(results);
413+
} onFailure:^(NSDictionary<NSString *, NSError *> *errors) {
414+
if (failureBlock) {
415+
NSError *error = (NSError *)[self getFirstResultByChannelPriority:errors];
416+
failureBlock(error);
417+
}
418+
}];
419+
}
420+
384421
@end

0 commit comments

Comments
 (0)