如何根据语言以外的标准本地化文本

发布于 2024-12-01 12:28:40 字数 328 浏览 0 评论 0原文

我有一个应用程序将在不同的欧洲国家销售。我们已经完成了应用程序本地化的过程,以便其字符串保留在 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?

Thanks,

Peter Hornby

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-12-08 12:28:40

在单例上定义两个捆绑包、后备捆绑包和首选捆绑包...

#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");

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

Then call it from a macro function

#define countryString(key) [[Localization sharedInstance]countryStringForKey:key];

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

/* 
  country-ES.strings
*/

"hello" = "hello";

And just get the value for the key:

countryString(@"hello");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文