将 iOS 应用程序翻译成不受支持/非标准的语言 [英] Translating iOS app to unsupported/non-standard languages

查看:20
本文介绍了将 iOS 应用程序翻译成不受支持/非标准的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展现有的 iPhone 应用程序(4.x 及更高版本)以支持更多语言:爱沙尼亚语、拉脱维亚语和立陶宛语.

I am expanding an existing iPhone app (4.x and up) with support for more languages: Estonian, Latvian and Lithuanian.

在我的 iPhone 和模拟器中,不支持这些语言,我很确定在这些地区也没有专门的固件可供它们使用.

In my iPhone and in the simulator there is no support for these languages, and I am pretty sure that no special firmware exists for them either for use in those territories.

如何才能最好地制作支持它们的应用?

我想出了两个我不太喜欢的解决方案.它们都不允许我在应用程序中使用一种以上的语言,因为用户无法从 Settings.app 列表中选择捆绑的语言.这意味着必须为每种语言提交一个版本.

I have come up with two solutions which I don't really like. Neither of them allows me to have more than one language in the app, since the user cannot choose the bundled languages from the Settings.app list. This means that one version must be submitted for each language.

对于每种目标语言(lt、lv、et),我将该语言的字符串文件放入 en.lproj 目录中.

For each targeted language (lt, lv, et) I put the strings files for that language into an en.lproj directory.

优点:使用众所周知的机制.该应用程序只是认为它正在运行英语.

Pros: Uses a well-known mechanism. The app just thinks it is running English.

缺点:严重破坏了我的本地化工具.它让未来的维护者感到困惑,因此容易出错.需要一个奇怪的构建设置.

Cons: Wreaks havoc on my localization tools. Its confusing for future maintainers and therefore error prone. Requires a weird build setup.

NSUserDefaults 中的 AppleLanguages 对象包含应用程序要使用的语言列表.通过这样设置,我可以让应用程序从 lt.lproj 目录加载例如立陶宛语:

The AppleLanguages object in NSUserDefaults contains a list of languages for the app to use. By setting it like this I can get the app to load for example Lithuanian from an lt.lproj directory:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"lt", nil] forKey:@"AppleLanguages"];

(由于历史原因,我已经在做一个稍微复杂的版本,以删除应用程序的某些版本中已弃用的翻译.否则即使我不再将它与应用程序捆绑在一起,较旧的安装也会选择 lproj 目录.)

(For historical reasons I am already doing a slightly more involved version of this to remove a deprecated translation in some versions of the app. Otherwise older installations would pick up the lproj dir even though I no longer bundle it with the app.)

优点:使用正确命名的 lproj 目录.与本地化工具很好地集成.简单的设置.只需要main.m中的一行即可实现.

Pros: Uses properly named lproj directories. Integrates well with localization tools. Simple setup. Only requires one line in main.m to implement.

缺点:尽管很多人都在使用 AppleLanguages 键,但这个解决方案使用它来加载其他不受支持的语言,所以我担心我可能会滑到薄冰.

Cons: Even though the AppleLanguages key is being used by a lot of people this solution uses it to load otherwise unsupported languages, so I fear I might be skating on thin ice.

  • 其他应用程序通常是如何支持这些不支持"的?语言?
  • 有没有办法支持它们以及通常支持的语言?
  • 您如何看待 AppleLanguages hack?
  • How do other apps normally support these "unsupported" languages?
  • Is there a way to support them along with the normally supported languages?
  • How do you feel about the AppleLanguages hack?

推荐答案

为什么不在您的应用程序中添加语言设置然后使用此代码(我在一个项目中使用它,用户可以根据我的要求在应用程序中切换语言客户).

Why not adding language setting within your app then use this code (i use it in a project where the user can switch languages within the app after a requirement from my client).

它基本上覆盖了 NSLocalizedString 并使用相同的文件结构(en.lproj 等)来保持您在使用apple-way"时使用的相同语言文件.

It basically overwrites NSLocalizedString and uses the same file structure (en.lproj, etc.) for keeping the same language files you use when you use the "apple-way".

试一试!

.h 文件

#import <Foundation/Foundation.h>

//#undef NSLocalizedString

#define ___(key) 
[[I7I18N sharedInstance] localizedStringForKey:(key)]

#undef NSLocalizedString
#define NSLocalizedString(key,value) 
[[I7I18N sharedInstance] localizedStringForKey:(key)]

@interface I7I18N : NSObject

@property (nonatomic, retain) NSMutableDictionary *i18nTable;
+ (I7I18N *)sharedInstance;
- (NSString *)localizedStringForKey:(NSString *)key;
- (void)setLocale:(NSString *)lProjFile;

@end

.m 文件

#import "I7I18N.h"

static I7I18N *sharedInstance;

@implementation I7I18N
@synthesize i18nTable=_i18nTable;

+ (I7I18N *)sharedInstance {
    if(!sharedInstance) {
        sharedInstance = [[I7I18N alloc] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.i18nTable = [NSMutableDictionary dictionary];

        NSArray *validLocalizations = [[NSBundle mainBundle] localizations];
        [self setLocale:[validLocalizations objectAtIndex:0]];
    }
    return self;
}

- (void)setLocale:(NSString *)lProjFile {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable.strings" ofType:@"" inDirectory:[NSString stringWithFormat:@"%@.lproj",lProjFile]];
    self.i18nTable = [NSDictionary dictionaryWithContentsOfFile:path];
}

- (NSString *)localizedStringForKey:(NSString *)key {
    NSString *possibleI18NString = [self.i18nTable objectForKey:key];
    if(!possibleI18NString) {
        return key;
    }
    return possibleI18NString;

}

@end

更新 1:不要忘记在使用 [[I7I18N sharedInstance] setLocale:@"yourlang.lproj"] 切换语言时构建所有视图(所有 NSLocalizedString 依赖项.

Update 1: Do not forget to build all your view (all NSLocalizedString dependencies when switching the language with [[I7I18N sharedInstance] setLocale:@"yourlang.lproj"].

这篇关于将 iOS 应用程序翻译成不受支持/非标准的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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