iOS手动本地化应用程序然后切换

时间:2014-01-08 14:25:05

标签: ios objective-c localization nslocalizedstring

自从我第一次使用Localization时,我一直面临iOS编程问题,

我使用以下内容来了解​​我所拥有的语言:

- (NSString*) getLanguage{

NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];

NSString *preferredLang = [languages objectAtIndex:0];

return preferredLang;}

并且有更改lang的按钮调用函数来更改lang

- (void) setLanguage:(NSString*) l{
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:l, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];}

语言变化成功,但是我需要关闭并重新打开应用程序才能看到结果,是否还有其他方法可以在切换lang后自动更改本地化?

注意:它也应该用故事板唤醒。

2 个答案:

答案 0 :(得分:0)

@Tudorizer在您翻译文件后在this link中进行了描述

使用此解决方案:

将此宏放在Prefix.pch中:

#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]

并且您需要使用本地化字符串:

NSLocalizedStringFromTableInBundle(@"GalleryTitleKey", nil, currentLanguageBundle, @"");

设置语言使用:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"de"] forKey:@"AppleLanguages"];

多次:

NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"fr"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"it"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"de"] forKey:@"AppleLanguages"];
NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));

答案 1 :(得分:0)

您可以使用以下类在应用程序中切换语言: (请注意,无论如何,您应该刷新存储在内存中的所有控制器的内容以获得完全的效果。)

// AMLocalization.h

#define SetAppLanguage(language) [[AMLocalization sharedLocalization] setLanguage:language]
#define GetAppLanguage() [[AMLocalization sharedLocalization] language]
#define LSC(key, comment) [[AMLocalization sharedLocalization] localizedStringForKey:(key) value:(comment)]
#define UDKeyAppLanguage @"UDKeyAppLanguage"

typedef enum {
    UILanguageUnknown,
    UILanguageEnglish,
    UILanguageFrench,
    ..................
} UILanguage;

@interface AMLocalization : NSObject
+ (AMLocalization *)sharedLocalization;
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment;
- (BOOL) setLanguage:(UILanguage) lang;
- (UILanguage) language;
@end

// AMLocalization.m

#import "AMLocalization.h"

static AMLocalization *_s_sharedLocalization = nil;

@implementation AMLocalization {

    NSBundle *_bundle;
    NSDictionary *_languageSet;
}

+ (AMLocalization *)sharedLocalization
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _s_sharedLocalization = [AMLocalization new];
    });
    return _s_sharedLocalization;
}

- (id) init
{
    if (self = [super init]) {
        _bundle = [NSBundle mainBundle];
        _languageSet = @{
                     @(UILanguageEnglish) : @"en",
                     @(UILanguageFrench) : @"fr"
                    };
    }
    return self;
}

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment
{
return [_bundle localizedStringForKey:key value:comment table:nil];
}

- (BOOL) setLanguage:(UILanguage) lang
{
    UILanguage activeLang = [self language];
    if (lang == UILanguageUnknown) {
        _bundle = [NSBundle mainBundle];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:UDKeyAppLanguage];
    }
    else {
        NSString *path = [[NSBundle mainBundle] pathForResource:_languageSet[@(lang)] ofType:@"lproj"];

        _bundle = [NSBundle bundleWithPath:path];
        [[NSUserDefaults standardUserDefaults] setInteger:lang forKey:UDKeyAppLanguage];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];

    return activeLang != lang;
}

- (UILanguage) language
{
    UILanguage lang = [[NSUserDefaults standardUserDefaults] integerForKey:UDKeyAppLanguage];
    if (lang == UILanguageUnknown) {
        NSString* preferredLang = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
        for (NSNumber *key in _languageSet) {
            if ([_languageSet[key] isEqualToString:preferredLang]) {
                lang = [key integerValue];
                break;
            }
        }
    }
    return lang;
}

@end