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

查看:90
本文介绍了将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.

NSLserDefaults中的> 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-w时使用的相同语言文件ay。

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:
当用 [[切换语言]时,不要忘记构建所有视图(所有 NSLocalizedString 依赖项) I7I18N sharedInstance] setLocale:@yourlang.lproj]

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天全站免登陆