ios - 从右到左的语言在运行时更改本地化。无法切换 UI 必须重新启动应用程序
<p><p>问题是我们想在运行时切换语言。
我们有一个支持英语和阿拉伯语的应用程序。</p>
<p>我正在做的是更改“AppleLanguages”,然后重置 Root ViewController 。
我使用了以下代码:</p>
<pre><code>NSUserDefaults* userDefaults = ;
NSMutableArray* languages = ;
NSString *language = nil;
if([ isEqual:@"en"]) {
[ setObject: forKey:@"AppleLanguages"];
language = @"ar";
} else {
[ setObject: forKey:@"AppleLanguages"];
language = @"en";
}
UIViewController *initViewController = ;
;
appDelegate.window.rootViewController = ;
appDelegate.window.backgroundColor = ;
;
</code></pre>
<p>原生本地化发生变化,但必须重新启动应用才能看到对 UI 的影响。</p>
<p>提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题在于语言更改没有反射(reflect)在 bundle 上。
所以我无法更改 UI 以及本地化文本。
我们可以使用以下代码来更改捆绑设置。
以下代码将在运行时切换语言。
需要子类化 NSBundle 类,如下所示:-
@implementation BundleEx</p>
<pre><code>- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle *bundle = objc_getAssociatedObject(self, &kBundleKey);
if (bundle) {
return ;
}
else {
return ;
}
</code></pre>
<p>}</p>
<pre><code>@end
@implementation NSBundle (Language)
+ (void)setLanguage:(NSString *)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass(, );
});
BOOL appleTextDirection = NO;
BOOL rightToLeftWritingDirection = NO;
if () {
rightToLeftWritingDirection=YES;
appleTextDirection = NO;
if ([[ init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[ setSemanticContentAttribute:
UISemanticContentAttributeForceRightToLeft];
}
}else {
rightToLeftWritingDirection=NO;
appleTextDirection = YES;
if ([[ init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[ setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
[ setBool:appleTextDirection forKey:@"AppleTextDirection"];
[ setBool:rightToLeftWritingDirection forKey:@"NSForceRightToLeftWritingDirection"];
[ synchronize];
id value = language ? pathForResource:language ofType:@"lproj"]] : nil;
objc_setAssociatedObject(, &kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
</code></pre>
<p>以及用于切换的代码:-</p>
<pre><code>-(void)toggleTheLanguageWith:(NSString *)identifier{
NSUserDefaults* userDefaults = ;
NSMutableArray* languages = ;
NSString *language = nil;
if ([ rangeOfString:@"en"].location != NSNotFound) {
[ setObject: forKey:@"AppleLanguages"];
language = @"ar";
}elseif ([ rangeOfString:@"ar"].location != NSNotFound) {
[ setObject: forKey:@"AppleLanguages"];
language = @"en";
}
[synchronize];
;
UIStoryboard *mystoryboard = ;
self.window.rootViewController = ;
;
[UIView transitionWithView:self.window duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
}];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从右到左的语言在运行时更改本地化。无法切换 UI 必须重新启动应用程序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40857870/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40857870/
</a>
</p>
页:
[1]