如何根据语言以外的标准本地化文本 [英] How to localize text based on criterion other than language

查看:99
本文介绍了如何根据语言以外的标准本地化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请将在不同的欧洲国家销售。我们已经完成了本地化应用程序的过程,以便其字符串在Settings.bundle中的特定于语言的.lproj文件中进行维护。一切正常。问题是有一些字符串不能关闭语言,但是在应用程序运行的国家/地区之外。例如,奥地利版本的应用程序和应用程序的德语版本之间存在不同的字符串,即使这两个国家都说德语。当它第一次运行时,应用程序会询问用户它正在运行的国家/地区。

I have an application which will be marketed in different European countries. We've gone through the process of localizing the application so that its strings are maintained in the language-specific .lproj files in the Settings.bundle. This all works fine. The problem is that there are some strings which don't key off language, but off the country where the app is run. For example, there are strings which differ between the Austrian version of the app and the German version of the app, even though both these countries speak German. When it's run for the first time, the app asks the user which country it's running in.

有没有办法可以将这些特定国家/地区的字符串保存在资源文件,并在运行时使用的资源文件由用户设置决定,在这种情况下,应用程序运行的国家/地区,而不是设备语言?

Is there a way in which I can maintain these country-specific strings in a resource file, and have the resource file used at run time be decided by a user setting, in this case the country where the app is running, rather than the device language?

谢谢,

Peter Hornby

Peter Hornby

推荐答案

定义两个捆绑包单身,后备和首选......

Define two bundles on a singleton, fallback and preferred...

#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编写默认文件,并为每种支持的语言编写一个文件。例如:

Write a default file for ES, and one file per supported language. eg:

/* 
  country-ES.strings
*/

"hello" = "hello";

只需获取密钥的值:

countryString(@"hello");

这篇关于如何根据语言以外的标准本地化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆