我的申请将在不同的欧洲国家销售。我们已经完成了本地化应用程序的过程,以便其字符串在Settings.bundle中的特定于语言的.lproj文件中进行维护。一切正常。问题是有一些字符串不能关闭语言,但是在运行应用程序的国家/地区之外。例如,奥地利版本的应用程序和德语版本的应用程序之间存在不同的字符串,即使这两个国家都说德语。当它第一次运行时,应用程序会询问用户正在运行的国家/地区。
是否有一种方法可以在资源文件中维护这些特定于国家/地区的字符串,并且在运行时使用的资源文件由用户设置决定,在这种情况下是应用程序运行的国家/地区,而不是比设备语言?
谢谢,
彼得霍恩比答案 0 :(得分:1)
在单身,后备和首选上定义两个包...
#import <Foundation/Foundation.h>
@interface Localization : NSObject
@property (nonatomic, retain) NSString* fallbackCountry;
@property (nonatomic, retain) NSString* preferredCountry;
@property (nonatomic, retain) NSDictionary* fallbackCountryBundle;
@property (nonatomic, retain) NSDictionary* preferredCountryBundle;
+(Localization *)sharedInstance;
- (NSString*) countryStringForKey:(NSString*)key;
@end
#import "Localization.h"
@implementation Localization
@synthesize fallbackCountryBundle, preferredCountryBundle;
@synthesize fallbackCountry, preferredCountry;
+(Localization *)sharedInstance
{
static dispatch_once_t pred;
static Localization *shared = nil;
dispatch_once(&pred, ^{
shared = [[Localization alloc] init];
[shared setFallbackCountry:@"country-ES"];
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
[shared setPreferredCountry:[NSString stringWithFormat:@"country-%@",countryCode]];
});
return shared;
}
-(void) setFallbackCountry:(NSString*)country
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:@"strings"];
self.fallbackCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
trace(@"Fallback: %@ %@",[bundlePath lastPathComponent], self.fallbackCountryBundle);
}
-(void) setPreferredCountry:(NSString*)country
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:@"strings"];
self.preferredCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:nil];
if (!exists) warn(@"%@.strings %@", country, exists ? @"FOUND" : @"NOT FOUND");
trace(@"Preferred: %@ %@",[bundlePath lastPathComponent], self.preferredCountryBundle);
}
- (NSString*) countryStringForKey:(NSString*)key
{
NSString* result = nil;
if (preferredCountryBundle!=nil) result = [preferredCountryBundle objectForKey:key];
if (result == nil) result = [fallbackCountryBundle objectForKey:key];
if (result == nil) result = key;
return result;
}
@end
然后从宏功能
中调用它#define countryString(key) [[Localization sharedInstance]countryStringForKey:key];
为ES编写默认文件,并为每种支持的语言编写一个文件。例如:
/*
country-ES.strings
*/
"hello" = "hello";
只需获取密钥的值:
countryString(@"hello");