2016-12-06 97 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");