感谢我得到的答案,我正在编写 2 个基于 Android 的解决方案。我使用的第一个是复数形式。乍一看检查复数文档/示例,您可能会认为有一个数量 =“few”(用于 2-4 个复数),在 sources 处检查仅适用于语言环境“cs”。对于其他语言环境,只有“一个”和“其他”有效。
所以在你的 strings.xml 文件中:
<plurals name ="years">
<item quantity="one">1 year</item>
<item quantity="other"><xliff:g id="number">%d</xliff:g> years</item>
</plurals>
所以对于波兰语,我会:
<plurals name ="years">
<item quantity="one">1 rok</item>
<item quantity="other"><xliff:g id="number">%d</xliff:g> lat</item>
</plurals>
然后我会在我的代码上:
int n = getYears(...);
if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl") && n >= 2 && n <= 4) {
return getString(R.string.years_pl, n);
} else {
return getResources().getQuantityString(R.plurals.years, n, n);
}
在波兰语语言环境的 strings.xml 文件中,我将添加缺少的字符串:
<string name="years_pl"><xliff:g id="number">%d</xliff:g> lata</string>
我的第二个解决方案有英语、西班牙语和其他没有太多复数变化的语言的复数元素。然后对于其他有这种变化的语言,我会使用 ChoiceFormat。所以在我的代码中:
...
private static final int LANG_PL = 0;
// add more languages here if needed
...
String formats[] = {
"{0,number} {0,choice,1#" + getString(R.string.year_1) + "|2#" + getString(R.string.years_2_4) + "|4<" + getString(R.string.years_lots) +"}", // polish
// more to come
};
...
// Here I would add more for certain languages
if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl")) {
return MessageFormat.format(formats[LANG_PL], n);
} else {
return getResources().getQuantityString(R.plurals.years, n, n);
}
我不知道这些方法是否是最好的方法,但就目前而言,或者在 Google 做出更好的东西之前,这对我有用。