我为我的iOS应用进行了本地化。根据客户的要求,我需要进行修改,即应用程序语言应该通过按钮操作更改。我的先前本地化是基于设备语言工作的。现在我必须修改应用程序,以便在不重新启动设备的情况下更改语言。
我为我的iOS应用进行了本地化。根据客户的要求,我需要进行修改,即应用程序语言应该通过按钮操作更改。我的先前本地化是基于设备语言工作的。现在我必须修改应用程序,以便在不重新启动设备的情况下更改语言。
#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)]
@interface LocalizeHelper : NSObject
// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;
// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;
//set a new language:
- (void) setLanguage:(NSString*) lang;
@end
// LocalizeHelper.m
#import "LocalizeHelper.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];
}
}
}
@end
设置语言使用
LocalizationSetLanguage(@"ar");
获取数值请使用
self.Mylabel.text = LocalizedString(@"rent");
对于您想支持的每种语言,您需要一个名为Localizable.strings的文件。这与Apple文档中描述的本地化完全相同。
// TABS
"buy" = "شراء";
"rent" = "إيجار";
"addListing" = "إضافة إعلان" ;
"calculator" = "دلالي" ;
"news" = "أخبار" ;
在您的应用中无法更改设备的语言,但是您可以通过更改 NSUserDefaults
中的 AppleLanguages
属性来仅更改您的应用程序的语言。请注意,我认为这仍然需要重新启动应用本身,但不需要重新启动设备。
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];
这里有另一个类似的问题,可能也能帮到您。