2016-12-06 48 views
0

我正在为使用本地化按钮按下时更改标签语言创建一个示例演示。如何更改我的标签语言不更改我的系统语言按钮按下时?

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

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

我已经创建Localizable.string文件但不能更改语言。

注意:不要更改系统(模拟器)语言。

+0

你的意思是你想改变你的应用程序中的语言,而不是改变设备的语言配置? – NSNoob

+0

雅正好,我想按钮的行动 – user7018875

+0

可能重复的[iOS:如何更改应用程序语言以编程方式无需重新启动应用程序?](http://stackoverflow.com/questions/9416923/ios-how-to-change-app -language-programmatically-without-restarting-the-app) – NSNoob

回答

0

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

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");