-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRLanguageManager.m
319 lines (277 loc) · 11 KB
/
IRLanguageManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// IRLanguageManager.m
// IRPasscode
//
// Created by Phil on 2019/11/14.
// Copyright © 2019 Phil. All rights reserved.
//
#import "IRLanguageManager.h"
@import AVFoundation;
@interface IRLanguageManager()<AVSpeechSynthesizerDelegate>
{
__weak UILabel* inputLabel;
}
@property (strong, nonatomic) NSArray *languageCodes;
@property (strong, nonatomic) NSDictionary *languageDictionary;
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
@property (nonatomic) dispatch_semaphore_t semaphore;
typedef NS_ENUM(NSInteger, UYLSpeedControlIndex)
{
UYLSpeedControlQuarterSpeed = 0,
UYLSpeedControlHalfSpeed = 1,
UYLSpeedControlNormalSpeed = 2,
UYLSpeedControlDoubleSpeed = 3
};
typedef NS_ENUM(NSInteger, UYLPitchControlIndex)
{
UYLPitchControlDeepPitch = 0,
UYLPitchControlNormalPitch = 1,
UYLPitchControlHighPitch = 2
};
@property (assign, nonatomic) UYLSpeedControlIndex selectedSpeed;
@property (assign, nonatomic) UYLPitchControlIndex selectedPitch;
@property (strong, nonatomic) NSString *selectedLanguage;
@property (strong, nonatomic) NSString *restoredTextToSpeak;
@end
@implementation IRLanguageManager
@synthesize currentLanguage;
NSString *UYLPrefKeySelectedSpeed = @"UYLPrefKeySelectedSpeed";
NSString *UYLPrefKeySelectedPitch = @"UYLPrefKeySelectedPitch";
NSString *UYLPrefKeySelectedLanguage = @"UYLPrefKeySelectedLanguage";
NSString *UYLKeySpeechText = @"UYLKeySpeechText";
+ (instancetype)sharedInstance {
static IRLanguageManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (instancetype)init {
if ((self = [super init])) {
currentLanguage = nil;
currentLanguageBundle = nil;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:SETTING_LANGUALE_KEY]) {
NSString *path = [self getPathWithLanguage:[userDefaults objectForKey:SETTING_LANGUALE_KEY]];
if (path) {
currentLanguageBundle = [NSBundle bundleWithPath:path];
currentLanguage = [userDefaults objectForKey:SETTING_LANGUALE_KEY];
}else{
[userDefaults removeObjectForKey:SETTING_LANGUALE_KEY];
[userDefaults synchronize];
}
}
[self restoreUserPreferences];
}
return self;
}
- (void)setLanguage:(NSString *)languageName {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *path = [self getPathWithLanguage:languageName];
if (path) {
currentLanguageBundle = [NSBundle bundleWithPath:path];
currentLanguage = languageName;
[userDefaults setObject:languageName forKey:SETTING_LANGUALE_KEY];
}else{
currentLanguage = nil;
currentLanguageBundle = nil;
[userDefaults removeObjectForKey:SETTING_LANGUALE_KEY];
}
[userDefaults synchronize];
}
- (NSString *)stringFor:(NSString *)srcString {
if (srcString == nil) {
return nil;
}
if (currentLanguageBundle) {
return NSLocalizedStringFromTableInBundle(srcString, nil, currentLanguageBundle, nil);
}
return NSLocalizedString(srcString, nil);
}
- (NSString *)getPathWithLanguage:(NSString *)languageName {
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_EN", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_ENGLISH_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_TC", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_CHINESE_TRADITIONAL_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_SC", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_CHINESE_SIMPLIFIED_SHORT_ID ofType:@"lproj"];
}
#if SupportMultiLanguage
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_GE", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_Germany_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_IT", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_Italy_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_SW", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_Swedish_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_FR", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_French_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_SP", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_Spanish_SHORT_ID ofType:@"lproj"];
}
if ([languageName isEqualToString:NSLocalizedString(@"LANGUAGE_DU", nil)])
{
return [[NSBundle mainBundle] pathForResource:LANGUAGE_Dutch_SHORT_ID ofType:@"lproj"];
}
#endif
return nil; //is Auto
}
- (void)speakWithLabel:(UILabel*)label {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
if (label.text && !self.synthesizer.isSpeaking) {
self->inputLabel = label;
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.selectedLanguage];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:label.text];
utterance.voice = voice;
float adjustedRate = AVSpeechUtteranceDefaultSpeechRate * [self rateModifier];
if (adjustedRate > AVSpeechUtteranceMaximumSpeechRate)
{
adjustedRate = AVSpeechUtteranceMaximumSpeechRate;
}
if (adjustedRate < AVSpeechUtteranceMinimumSpeechRate)
{
adjustedRate = AVSpeechUtteranceMinimumSpeechRate;
}
utterance.rate = adjustedRate;
float pitchMultiplier = [self pitchModifier];
if ((pitchMultiplier >= 0.5) && (pitchMultiplier <= 2.0))
{
utterance.pitchMultiplier = pitchMultiplier;
}
[self.synthesizer speakUtterance:utterance];
}
});
});
}
- (void)stopSpeak {
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
#pragma mark - AVSpeechSynthesizer
// Language codes used to create custom voices. Array is sorted based
// on the display names in the language dictionary
- (NSArray *)languageCodes {
if (!_languageCodes) {
_languageCodes = [self.languageDictionary keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
return _languageCodes;
}
- (NSDictionary *)languageDictionary {
if (!_languageDictionary) {
NSArray *voices = [AVSpeechSynthesisVoice speechVoices];
NSArray *languages = [voices valueForKey:@"language"];
NSLocale *currentLocale = [NSLocale autoupdatingCurrentLocale];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *code in languages) {
dictionary[code] = [currentLocale displayNameForKey:NSLocaleIdentifier value:code];
}
_languageDictionary = dictionary;
}
return _languageDictionary;
}
- (AVSpeechSynthesizer *)synthesizer {
if (!_synthesizer) {
_synthesizer = [[AVSpeechSynthesizer alloc] init];
_synthesizer.delegate = self;
}
return _synthesizer;
}
- (float)rateModifier
{
float rate = 1.0;
switch (self.selectedSpeed)
{
case UYLSpeedControlQuarterSpeed:
rate = 0.25;
break;
case UYLSpeedControlHalfSpeed:
rate = 0.5;
break;
case UYLSpeedControlNormalSpeed:
rate = 1.0;
break;
case UYLSpeedControlDoubleSpeed:
rate = 2.0;
break;
default:
rate = 1.0;
break;
}
return rate;
}
- (float)pitchModifier {
float pitch = 1.0;
switch (self.selectedPitch)
{
case UYLPitchControlDeepPitch:
pitch = 0.75;
break;
case UYLPitchControlNormalPitch:
pitch = 1.0;
break;
case UYLPitchControlHighPitch:
pitch = 1.5;
break;
default:
pitch = 1.0;
break;
}
return pitch;
}
- (dispatch_semaphore_t)semaphore {
if (!_semaphore) {
_semaphore = dispatch_semaphore_create(1);
}
return _semaphore;
}
#pragma mark - State Restoration
- (void)restoreUserPreferences {
NSString *currentLanguageCode = [AVSpeechSynthesisVoice currentLanguageCode];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *defaults = @{ UYLPrefKeySelectedPitch:[NSNumber numberWithInteger:UYLPitchControlNormalPitch],
UYLPrefKeySelectedSpeed:[NSNumber numberWithInteger:UYLSpeedControlNormalSpeed],
UYLPrefKeySelectedLanguage:currentLanguageCode
};
[preferences registerDefaults:defaults];
self.selectedPitch = [preferences integerForKey:UYLPrefKeySelectedPitch];
self.selectedSpeed = [preferences integerForKey:UYLPrefKeySelectedSpeed];
self.selectedLanguage = [preferences stringForKey:UYLPrefKeySelectedLanguage];
}
#pragma mark - AVSpeechSynthesizerDelegate
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
if (inputLabel) {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:inputLabel.text];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:characterRange];
inputLabel.attributedText = text;
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:inputLabel.attributedText];
[text removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, [text length])];
inputLabel.attributedText = text;
inputLabel = nil;
dispatch_semaphore_signal(self.semaphore);
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:inputLabel.attributedText];
[text removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, [text length])];
inputLabel.attributedText = text;
inputLabel = nil;
dispatch_semaphore_signal(self.semaphore);
}
@end