与此同时,我确实为我自己的问题找到了解决方案:
我创建了一个新类"LocalizeHelper":
Header LocalizeHelper.h个
//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)]
@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
iMplementation LocalizeHelper.m
// 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
对于您想要支持的每种语言,您都需要一个名为Localizable.strings
的文件.这与Apple本地化文档中描述的完全一样.唯一的区别是:现在你甚至可以使用苹果不支持的印地语或世界语等语言.
举个例子,下面是我的英语和德语版本的Localizable.string的前几行:
English个
/* English - English */
/* for debugging */
"languageOfBundle" = "English - English";
/* Header-Title of the Table displaying all lists and projects */
"summary" = "Summary";
/* Section-Titles in table "summary" */
"help" = "Help";
"lists" = "Lists";
"projects" = "Projects";
"listTemplates" = "List Templates";
"projectTemplates" = "Project Templates";
German
/* German - Deutsch */
/* for debugging */
"languageOfBundle" = "German - Deutsch";
/* Header-Title of the Table displaying all lists and projects */
"summary" = "Überblick";
/* Section-Titles in table "summary" */
"help" = "Hilfe";
"lists" = "Listen";
"projects" = "Projekte";
"listTemplates" = "Vorlagen für Listen";
"projectTemplates" = "Vorlagen für Projekte";
要使用本地化,必须在应用程序中设置一些 routine ,并在语言 Select 中调用宏:
LocalizationSetLanguage(selectedLanguage);
在此之后,您必须确保所有用旧语言显示的内容现在都会用新语言重新绘制(隐藏文本必须在再次可见时立即重新绘制).
要使本地化文本适用于各种情况,您永远不必将修复文本写入对象标题.始终使用宏LocalizedString(keyword)
.
don't:个
cell.textLabel.text = @"nice title";
do:个
cell.textLabel.text = LocalizedString(@"nice title");
在每个版本的Localizable中都有一个"好标题"条目.串!