【问题标题】:Runtime change the language/localization in Three20运行时更改 Three20 中的语言/本地化
【发布时间】:2011-12-02 03:26:10
【问题描述】:

是否可以在运行时更改 Three20 语言/本地化而不重新启动应用程序?

目前,我设法通过更改 main.m 中 AppleLanguages 的值来更改语言

【问题讨论】:

    标签: iphone objective-c ios xcode three20


    【解决方案1】:

    有一个“黑客”。您可以使用本地化文本加载您自己的 NSBundle,并改用该 NSBundle。请注意,如果缺少本地化语言文件,应用程序将无法运行,因此请确保设置正确的语言。

    在你的 AppDelegate 实现之上,添加一个自定义的 NSBundle 声明:

    static NSBundle *bundle = nil;
    

    然后将您想要的语言加载到该包中:

    [[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"he", nil] forKey:@"AppleLanguages"];
    
    NSLocale* locale = TTCurrentLocale();  
    NSString *path = [[NSBundle mainBundle] pathForResource:[locale localeIdentifier] ofType:@"lproj" ];
    bundle = [[NSBundle bundleWithPath:path] retain];
    

    您将在 AppDelegate 中添加一个自定义函数来获取本地化文本(而不是 NSLocalizedString)

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    + (NSString*)get:(NSString*)key  {
       return [bundle localizedStringForKey:key value:nil table:nil];
    }
    

    为方便起见,您可以在 pch 文件中添加一个静态函数:

    #import "AppDelegate.h"
    
    #define MyLocalizedString(key, alt) [AppDelegate get:key]
    

    【讨论】:

    • 我应该把你答案的第二个代码块放在哪里?在我的应用委托中?我会在“changeLanguageToSpanish”按钮中添加什么?
    • 答案仍然相关吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多