Objective-C的国际多元化Lib?
我正在国际化我的应用程序,并正在寻找如何处理复数形式的解决方案。我遇到了 Mozilla的PluralForm 项目,该项目基本上将复数问题抽象为国际化。以下是它的工作原理:
I am internationalizing my app and am looking for a solution to how to deal with plural forms. I ran across Mozilla's PluralForm project, which essentially abstracts away the issue of plurals for internationalization. Here's how it works:
-
语言可以遵循许多复数规则。每种语言都适用于众多复数规则中的一种,而且只有一种(Mozilla的文档有15种可能的复数规则)。例如,在具有规则0的语言(例如中文)中,没有复数形式,因此只有一个所需的单词形式。在具有规则1的语言(例如德语)中,每个单词具有两个不同的复数形式。依此类推,取决于规则如何匹配。
There are a number of "plural rules" that languages can follow. Each language fits into one and only one of the many plural rules (Mozilla's documentation has 15 potential plural rules). For example, in languages with rule 0 (such as Chinese), there are no plural forms, and so there is only one needed word form. In languages with rule 1 (such as German), each word has two distinct plural forms. And so on, depending on how the rules match up.
当您编写国际化字符串时,您不仅要传递字符串以进行国际化,而且您想要使名词复数化的数字:
When you're programming an internationalized string, you not only pass the string to be internationalized but the number that you want to pluralize the noun with:
print(你有+ num ++ PluralForm.get(num,downloads )+。);
在Objective-C中,人们的做法会有所不同:
In Objective-C, one would do it rather differently:
NSString * str = [NSString stringWithFormat:NSLocalizedStringFromTable(@%d Items),myNumber];
当然,上面的Objective-C示例并没有解决复数形式的问题。
Of course, this above Objective-C example does not solve the issue of plural forms.
当PluralForm.get函数接到一个调用时,根据当前使用的语言,它应用复数形式,并从字符串文件中提取适当的国际化字符串,具体取决于它应遵循的特定子规则。
When the PluralForm.get function gets a call, depending on the language currently in use, it applies the plural form, and pulls the proper internationalized string from the strings file depending on which specific sub-rule that it should follow.
PluralForm的优势很明显(至少对我而言)。它从编码器中抽象出实际的语言规则,因此在您必须处理复数形式的所有位置都不需要复制它们。
The advantage to PluralForm is clear (at least to me). It abstracts away the actual language rules from the coder, so that they do not need to be duplicated in all locations where you have to deal with a plural form.
我想使用像编写iPhone应用程序时那样的系统。是否有可用的项目或代码将Mozilla的PluralForm移植到objective-c?
Smartling(翻译管理) Platform)发布了一个开源库,用于管理iOS中的复数形式。在处理了许多具有iOS复数问题的客户并且找不到能够满足我们和客户需求的解决方案后,我们决定建立自己的。
Smartling (a Translation Management Platform) has released an open source library for managing plurals in iOS. After having dealt with numerous clients having iOS plurals problems and not finding a solution that would work how we and our clients wanted, we decided to build our own.
库需要多个字符串的键,并将它们扩展为包含基于CLDR复数规则的复数形式。该库为NSLocalizedString提供了另一个名为SLPluralizedString的函数来进行查找。
The library takes the keys for the plural strings and extends them to contain the plural form based on the CLDR plural rules. The library provides an alternative function to NSLocalizedString called SLPluralizedString to do the look up.
英文源文件如下所示:
"%d Items Processed##{one}" = "1 Item Processed";
"%d Items Processed##{other}" = "%d Items Processed";
您可以使用SLPluralizedString函数查找字符串:
And you would use the SLPluralizedString function to look up the string:
SLPluralizedString(@"%d Items Processed", numItems, @"Number of items processed");
翻译的俄语文件将具有适当数量的语言键/值:
A translated Russian file would have the appropriate number of keys/values for the language:
"%d Items Processed##{one}" = "%d элемент обработан";
"%d Items Processed##{few}" = "%d элемента обработано";
"%d Items Processed##{many}" = "%d элементов обработано";
"%d Items Processed##{other}" = "%d элемента обработано";
实际代码不需要根据语言而改变。一个函数适用于所有语言并返回相应的翻译字符串。
The actual code wouldn't need to change depending on the language. One function would work across all languages and return the appropriate translated string.
随意分享评论,改进等。
Feel free to share comments, improvements, etc.