目前,我有三个单独的 .strings
文件 - 分别用于英语、法语和德语。
我在首选项部分有一个 UISegmentedControl
,我想根据选择的段更改默认使用的 .strings
文件。
当我测试它时,本地化已经在应用程序外部工作,我什至设法根据选择的段来本地化单个对象(如果选择法国,下面的标签文本从“South”变为“Sud” ,如果重新选择了英语,则再次返回)使用此:
if(German segment) {
NSString *path= [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
// Returns german version of localised string as label text.
self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil];
// I would like to set the default .strings file to 'de' here.
}
else if(French segment) {
NSString *path= [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
// Returns french version of localised string as label text
self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil];
// I would like to set the default .strings file to 'fr' here
}
else if(British segment) {
NSString *path= [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
// Returns english version of localised string as label text
self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil];
// I would like to set the default .strings file to 'en' here.
}
现在,因为我已经在单击按钮时本地化了各个字符串,所以我想知道我是否可以本地化每个 .strings
文件中的 ALL 字符串一口气(这样我就可以根据选择的段更改默认的.strings)或至少以可接受的代码量定位尽可能多的目标。
显然,目前我可以一个一个地键入它们,但由于应用程序中使用的本地化字符串的数量,这并不是一个真正的选择。
如有任何帮助,我们将不胜感激。
最佳答案
您可以将这种本地化方法包装在 LocalizationSystem 类中:
@interface PLLocalizationSystem : NSObject
@property (strong, nonatomic) NSString *language;
+ (PLLocalizationSystem *) sharedLocalizationSystem;
- (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)comment;
@end
@implementation PLLocalizationSystem {
NSBundle *_bundle;
}
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment {
return [_bundle localizedStringForKey:key value:comment table:nil];
}
- (void)setLanguage:(NSString *)newLanguage {
...
NSString *path = [[NSBundle mainBundle] pathForResource:newLanguage ofType:@"lproj"];
if (path) {
_bundle = [NSBundle bundleWithPath:path];
_language = newLanguage;
} else {
[self resetLocalization];
}
...
[[NSNotificationCenter defaultCenter] postNotificationName:kLocalizationChangedNotification object:nil];
}
并为更简单的访问做一些定义(在 LocalizationSystem 头文件中定义):
#define PLLocalizedString(key, comment) \
[[PLLocalizationSystem sharedLocalizationSystem] localizedStringForKey:(key) value:(comment)]
#define PLLocalizationSetLanguage(language) \
[[PLLocalizationSystem sharedLocalizationSystem] setLanguage:(language)]
#define PLLocalizationGetLanguage \
[[PLLocalizationSystem sharedLocalizationSystem] language]
您还可以从您的 AppName-Prefix.pch 文件中导入此文件,以便从任何地方访问。
接下来,在您的 Controller 中,您可以观察本地化变化并调用适当的方法。
所以现在,您可以像使用 NSLocalizedString 一样使用它。
附言我没有在这里写下所有的实现,但如果有人想要,我可能稍后会在 githab 上分享它
更新。使用示例。
例如,您有一个要动态本地化的 UIViewController:
@implementation MyCustomViewController
- (void)viewDidLoad {
[super viewDidLoad];
//first of all - register for localization changes
//i made for this purposes category for UIViewController(localizable)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localize)
name:kLocalizationChangedNotification
object:nil];
}
//here you should apply all localization
- (void)localize {
[self setTitle:PLLocalizedString(@"myTitle", nil);
[label setText:PLLocalizedString(@"labelTitle", nil)];
....
}
@end
并且在您的偏好设置中,当您更改语言时调用 PLLocalizationSetLanguage(language),
所有注册此事件的 Controller 都将自动本地化。当然,这种方法需要所有的本地化应用在localize
方法中。
并且不要忘记在 Controller 释放或卸载时移除观察者
关于iphone - 动态本地化应用程序内部的 iOS 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892143/