我结束了这个解决方案。现在在你的整个项目
[I18NUtil localizedString:@"myLocalizedString"]
:我先定义一个静态类设置我的应用程式语言上:
@implementation I18NUtil
static NSBundle *_bundle;
static NSString *_language;
+ (id)alloc
{
[NSException raise:@"Cannot be instantiated!" format:@"Static class 'ClassName' cannot be instantiated!"];
return nil;
}
+ (void)setLanguage:(NSString *)languageCode
{
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:languageCode, nil] forKey:@"AppleLanguages"];
NSString *path = [[NSBundle mainBundle] pathForResource:languageCode ofType:@"lproj"];
_bundle = [NSBundle bundleWithPath:path];
_language = languageCode;
}
+ (NSString *)localizedString:(NSString *)key
{
return [_bundle localizedStringForKey:key value:@"" table:nil];
}
@end
你可以很容易地使用。
但我发现重写NSLocalizedString宏并使用像我习惯的i18n更容易。您必须将其包含到您的AppName-Prefix.pch中:
#ifdef __OBJC__
...
// Override native NSLocalizedString(key,message) to achieve in-app and on-the-fly i18n
// Needs to be defined AFTER importing Foundation.h
#import "I18NUtil.h"
#ifdef NSLocalizedString
#undef NSLocalizedString
#endif
#define NSLocalizedString(key, message) \
[I18NUtil localizedString:(key)]
#endif
玩得开心!