简体   繁体   English

如何在iPhone应用程序中更改语言运行时?

[英]How to change Language runtime in iphone app?

I am using below code. 我正在使用下面的代码。
This code saves Locale name, but how refresh whole application with new language. 该代码保存区域设置名称,但是如何使用新语言刷新整个应用程序。
Here langcode variable is dynamic as per user selection. 此处langcode变量根据用户选择是动态的。

NSString *langCode = @"fr";
NSArray *languages = nil;
languages = [NSArray arrayWithObject:langCode];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

Which is remains for change the language in runtime? 在运行时更改语言还有哪些? (From my app setting screen) (在我的应用设置屏幕上)

After making category on Language it will solve the issue. 在对语言进行分类后,它将解决此问题。

.h file .h文件

@interface NSBundle (Language)

+ (void)setLanguage:(NSString *)language;

@end

.m file .m文件

#import <objc/runtime.h>

static const char _bundle=0;

@interface BundleEx : NSBundle
@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}

@end
@implementation NSBundle (Language)

+ (void)setLanguage:(NSString *)language
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
                  {
                      object_setClass([NSBundle mainBundle],[BundleEx class]);
                  });
    objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:self];
}

From my setting view controller called above method of the category class and also fire post notification for whole application. 从我的设置视图控制器中,调用上述类别类的方法,并触发整个应用程序的发布通知。

[NSBundle setLanguage:langCode];

Store this variable in ApplicationDelegate file. 将此变量存储在ApplicationDelegate文件中。 Define its property and use anytime via appDelegate instance when you login. 定义其属性,并在登录时随时通过appDelegate实例使用。 Once selection is changed, change the application bundle as you desire also modify the text and all. 更改选择后,根据需要更改应用程序包,还可以修改文本和全部。 put the language selection code whether in loop or button selection as per your requirement. 根据需要将语言选择代码放入循环还是按钮选择中。

       NSPath* path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
    APP_DELEGATE.strLanguageSelectedFromLoginView = @"SPANISH";

or 要么

       NSPath* path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    APP_DELEGATE.strLanguageSelectedFromLoginView = @"ENGLISH";

and

        languageBundle = [NSBundle bundleWithPath:path];

and finally load the laguageBundle. 最后加载laguageBundle。 NSBundle* languageBundle to be defined in app delegate. NSBundle * languageBundle将在应用程序委托中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM