如何更改我的标签语言而不更改我的系统语言按下按钮?

时间:2016-12-06 10:29:07

标签: ios objective-c

我正在创建一个示例演示,用于在使用本地化按下按钮时更改标签语言。

我已经尝试了很多次但无法显示不同的语言。

我的问题是我想将我的语言英语改为其他语言。

我已经创建了Localizable.string文件,但无法更改语言。

注意:请勿更改系统(模拟器)语言。

1 个答案:

答案 0 :(得分:0)

您可以创建一个Helper类。请参阅下面我正在使用的课程。

<强> LocalizeHelper.h

#import <Foundation/Foundation.h>

// some macros (optional, but makes life easy)

// Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)"
#define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]

// "language" can be (for american english): "en", "en-US", "english". Analogous for other languages.
#define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]

#define LocalizationGetLanguage() [[LocalizeHelper sharedLocalSystem] getLanguage]

@interface LocalizeHelper : NSObject

// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;

// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;

//set a new language:
- (void) setLanguage:(NSString*) lang;

//get current language
- (NSString *)getLanguage;

@end

<强> LocalizeHelper.m

#import "LocalizeHelper.h"
#import "Constants.h"

// Singleton
static LocalizeHelper* SingleLocalSystem = nil;

// my Bundle (not the main bundle!)
static NSBundle* myBundle = nil;


@implementation LocalizeHelper


//-------------------------------------------------------------
// allways return the same singleton
//-------------------------------------------------------------
+ (LocalizeHelper*) sharedLocalSystem {
    // lazy instantiation
    if (SingleLocalSystem == nil) {
        SingleLocalSystem = [[LocalizeHelper alloc] init];
    }
    return SingleLocalSystem;
}


//-------------------------------------------------------------
// initiating
//-------------------------------------------------------------
- (id) init {
    self = [super init];
    if (self) {
        // use systems main bundle as default bundle
        myBundle = [NSBundle mainBundle];
    }
    return self;
}


//-------------------------------------------------------------
// translate a string
//-------------------------------------------------------------
// you can use this macro:
// LocalizedString(@"Text");
- (NSString*) localizedStringForKey:(NSString*) key {
    // this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")
    // the difference is: here we do not use the systems main bundle, but a bundle
    // we selected manually before (see "setLanguage")
    return [myBundle localizedStringForKey:key value:@"" table:nil];
}


//-------------------------------------------------------------
// set a new language
//-------------------------------------------------------------
// you can use this macro:
// LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de");
- (void) setLanguage:(NSString*) lang {

    // path to this languages bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];
    if (path == nil) {
        // there is no bundle for that language
        // use main bundle instead
        myBundle = [NSBundle mainBundle];
    } else {

        // use this bundle as my bundle from now on:
        myBundle = [NSBundle bundleWithPath:path];

        // to be absolutely shure (this is probably unnecessary):
        if (myBundle == nil) {
            myBundle = [NSBundle mainBundle];
        }
    }

    [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"lang"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//-------------------------------------------------------------
// get current language
//-------------------------------------------------------------
- (NSString *)getLanguage {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"lang"];
}


@end

当您想要从应用更改语言时,请调用此函数LocalizationSetLanguage(@"fr");

要获取本地化字符串,请致电LocalizedString(@"string");