是否可以在字符串值中包含占位符string.xml,可以在运行时分配值?
例:
一些字符串PLACEHOLDER1更多字符串
我正在使用Plurals来简化我的代码.例如,我曾经有过
<string name="cat">Cat</string>
<string name="cats">Cats</string>
Run Code Online (Sandbox Code Playgroud)
我现在使用Plurals而不是多个字符串
<plurals name="cats">
<item quantity="one">Cat</item>
<item quantity="other">Cats</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
但是,我曾经检索过要在我的XML中用作标题或摘要的字符串.例如,
android:title="@string/cats"
Run Code Online (Sandbox Code Playgroud)
删除了该字符串以支持Plural,我现在不确定如何从XML中检索我的字符串.我做了一个天真的尝试
android:title="@plurals/cats"
Run Code Online (Sandbox Code Playgroud)
但这只是给了我@1234567890而不是Cats(或Cat).
有人知道是否可以从XML中检索Plural中的字符串?
我使用复数来编译Android应用程序的数量字符串.我完全按照教程中的内容进行操作:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)
这是复数的定义:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="number_of_comments">
<item quantity="zero">No comments</item>
<item quantity="one">One comment</item>
<item quantity="other">%d comments</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
有趣的是,输出字符串与我所定义的一样奇怪:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
Run Code Online (Sandbox Code Playgroud)
我想这是因为文档说明When the language requires special treatment of the number 0 (as in Arabic).了zero数量.有没有办法强迫我的定义?
Android允许翻译人员定义Plurals.以下示例适用于我的语言环境'en':
<plurals name="numberOfSongsAvailable">
<item quantity="one">One song found.</item>
<item quantity="other">%d songs found.</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
但是添加一个特殊值two不起作用,仍然采用该other版本.使用two依赖于语言环境?那么,two如果语言环境明确指定应该有two版本,Android是否只采用该版本?
SO问题Android复数处理"零"在使用zero英语时也出现同样的错误,也不支持.除了避免我想要避免的Android复数之外,这个问题没有解决方案.
什么是为不同语言制作字符串的最佳方法?我有这个问题,我试图显示诸如'月','月','年','年'之类的字符串.目前我正在研究我所熟知的3种语言:西班牙语,英语和波兰语.对于英语和西班牙语,这是直截了当的.但是,例如,在波兰'年'可以成为'lata'(在数字2 - 4之后)或'lat'(在5之后的数字之后).我正在考虑为此添加一个额外的字符串,并在其他语言中将其设置为空.然而,这让我想到了我不知道的其他语言,这可能会有更多的差异.如果我考虑将来添加更多语言,那么在这种情况下哪个应该是最好的方法?
我开始学习iOS Stringsdict 文件,并在使用以下语法的项目中找到了一些现有代码:
<key>zero</key>
<string>You no message.</string>
Run Code Online (Sandbox Code Playgroud)
根据 CLDR,zero在英语中是无效的复数形式,我们希望使用明确的复数规则(=0使用 ICU MessageFormat 时)
我试图找到如何在 iOS Stringsdict 文件中使用显式复数规则,但找不到任何方法来实现这一点。有人可以确认这是否受支持吗?
解决方案示例(我无法测试它们,但也许有人可以?)
<key>0</key>
<string>You no message.</string>
Run Code Online (Sandbox Code Playgroud)
或者
<key>=0</key>
<string>You no message.</string>
Run Code Online (Sandbox Code Playgroud)
关于 ICU MessageFormat 的 CLDR 实现的显式复数规则部分的额外参考:
https://formatjs.io/guides/message-syntax/#plural-format
=value 这用于匹配特定值,而不管当前语言环境的多个类别。
我开始遇到 android 复数形式,并且我对这个功能的可用性感到满意。我声明plurals.xml并尝试从我的代码中读取它们,但我得到了不正确的结果。
复数:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numbers">
<item quantity="zero">No comments</item>
<item quantity="one">%1$d comment.</item>
<item quantity="two">%1$d comments.</item>
<item quantity="few">%1$d comments.</item>
<item quantity="many">%1$d comments.</item>
<item quantity="other">%1$d comments.</item>
</plurals>
<plurals name="numberOfSongsAvailable">
<item quantity="zero">No song found.</item>
<item quantity="one">One song found.</item>
<item quantity="two">Two song found.</item>
<item quantity="other">Other songs found.</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
活动 :
public class AndroidPlurals extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.plurals);
((ListView) findViewById(R.id.listView)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPluralsList(10)));
Toast.makeText(this, getClass().getSimpleName(), Toast.LENGTH_LONG).show();
}
private String[] getPluralsList(int k) …Run Code Online (Sandbox Code Playgroud) android ×6
android-xml ×2
plural ×2
cldr ×1
ios ×1
java ×1
localization ×1
multilingual ×1
string ×1
translation ×1