OGeek|极客世界-中国程序员成长平台

标题: ios - 使用相同的本地化按钮切换语言应用程序 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:42
标题: ios - 使用相同的本地化按钮切换语言应用程序

我正在开发具有两种语言的 iOS 应用程序。 我为每种语言本地化了主 Storyboard,现在我尝试使用相同的按钮(切换按钮文本)在语言之间切换。

我发现了许多代码可以用按钮更改应用程序语言,例如 thisAdvance-Localization-in-ios-apps但我不知道如何根据按钮中选择的语言重新加载带有 .strings 的 Storyboard 。 请问有什么帮助吗?谢谢

localized Main.storyboard English strings file



Best Answer-推荐答案


恐怕你不能使用 Main.strings 文件。您最终会将 Storyboard 中的所有可本地化文本移动到 Localizable.strings 并使用诸如 NSLocalizedString(key, comment) 之类的方法从那里加载它们。

这里是 Working sample project在 GitHub 上。如下图所示:

enter image description here

演示 View Controller 的完整代码

#import "ViewController.h"

//--------------- Modify NSBunle behavior -------------
#import <objc/runtime.h>

@interface CustomizedBundle : NSBundle
@end

@implementation CustomizedBundle
static const char kAssociatedLanguageBundle = 0;

-(NSString*)localizedStringForKeyNSString *)key
                            valueNSString *)value
                            tableNSString *)tableName {

    NSBundle* bundle=objc_getAssociatedObject(self, &kAssociatedLanguageBundle);

    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] :
    [super localizedStringForKey:key value:value table:tableName];
}
@end

@implementation NSBundle (Custom)
+ (void)setLanguageNSString*)language {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object_setClass([NSBundle mainBundle], [CustomizedBundle class]);
    });

    objc_setAssociatedObject([NSBundle mainBundle], &kAssociatedLanguageBundle, language ?
                             [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end

//--------------- Demo ---------------------------------
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (nonatomic, assign) BOOL usingArabic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self localizeTexts];
}

- (void)localizeTexts {
    self.label.text = NSLocalizedString(@"explanation_message", nil);
    [self.button setTitle:NSLocalizedString(@"language_switch_title", nil) forState:UIControlStateNormal];
}


- (IBAction)switchLanguageTouchedid)sender {
    _usingArabic = !_usingArabic;
    NSString *targetLang = _usingArabic ? @"ar" : @"en";

    [NSBundle setLanguage:targetLang];

    [[NSUserDefaults standardUserDefaults] setObject:targetLang forKey"selectedLanguage"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [NSBundle setLanguage:targetLang];

    [self localizeTexts];
}

@end

您的 Localizable 文件将如下所示:

enter image description here

希望这会有所帮助。 编码愉快!

编辑: 如果不调用上面示例中的“localizeTexts”之类的方法来“刷新”正在显示的文本值,就不可能反射(reflect)当前选择的语言。该对象在内存中,因此您必须重新创建或更新其值。

关于ios - 使用相同的本地化按钮切换语言应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606991/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4