是否可以在 strings.xml 中为运行时值提供占位符?
string.xml
中的字符串值中是否可以包含可以在运行时赋值的占位符?
例子:
一些字符串PLACEHOLDER1一些更多的字符串
Is it possible to have placeholders in string values in string.xml
that can be assigned values at run time?
Example:
some string PLACEHOLDER1 some more string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
格式和样式
是的,请参阅字符串资源:格式和样式中的以下内容
基本用法
请注意,
getString
有一个重载,该重载使用该字符串作为格式字符串:复数
如果您需要处理复数,请使用:
第一个
mailCount
参数用于决定使用哪种格式(单数或复数),其他参数是您的替换:请参阅字符串资源:复数 了解更多详细信息。
Formatting and Styling
Yes, see the following from String Resources: Formatting and Styling
Basic Usage
Note that
getString
has an overload that uses the string as a format string:Plurals
If you need to handle plurals, use this:
The first
mailCount
param is used to decide which format to use (single or plural), the other params are your substitutions:See String Resources: Plurals for more details.
补充答案
当我第一次在接受的答案中看到
%1$s
和%2$d
时,它毫无意义。这里有更多的解释。它们称为格式说明符。在 xml 字符串中,它们的形式为
%:百分号标记格式说明符的开头。
参数索引:这是一个数字,后跟一个美元符号。如果您想要插入到字符串中的三个参数,那么它们将被称为
1$
、2$
和3$
。您将它们放置在资源字符串中的顺序并不重要,重要的是您提供参数的顺序。格式类型:有很多方式可以格式化内容(请参阅文档)。以下是一些常见的:
s
stringd
十进制整数f
浮点数示例
我们将创建以下格式化字符串,其中以编程方式插入灰色部分。
string.xml
MyActivity.java
注释
getString
因为我处于 Activity 中。如果不可用,您可以使用context.getResources().getString(...)
。String.format()
还将格式化字符串。1$
和2$
术语不需要按该顺序使用。也就是说,2$
可以出现在1$
之前。当针对使用不同词序的语言对应用程序进行国际化时,这非常有用。%1$s
的格式说明符。%%
获取实际的% 字符。
Supplemental Answer
When I first saw
%1$s
and%2$d
in the accepted answer, it made no sense. Here is a little more explanation.They are called format specifiers. In the xml string they are in the form of
%: The percent sign marks the beginning of the format specifier.
parameter index: This is a number followed by a dollar sign. If you had three parameters that you wanted to insert into the string, then they would be called
1$
,2$
, and3$
. The order you place them in the resource string doesn't matter, only the order that you supply the parameters.format type: There are a lot of ways that you can format things (see the documentation). Here are some common ones:
s
stringd
decimal integerf
floating point numberExample
We will create the following formatted string where the gray parts are inserted programmatically.
string.xml
MyActivity.java
Notes
getString
because I was in an Activity. You can usecontext.getResources().getString(...)
if it is not available.String.format()
will also format a String.1$
and2$
terms don't need to be used in that order. That is,2$
can come before1$
. This is useful when internationalizing an app for languages that use a different word order.%1$s
multiple times in the xml if you want to repeat it.%%
to get the actual%
character.当您想要使用实际 strings.xml 文件中的参数而不使用任何 Java 代码时:
这不适用于跨资源文件,即必须将变量复制到需要它们的每个 XML 文件中。
When you want to use a parameter from the actual strings.xml file without using any Java code:
This does not work across resource files, i.e. variables must be copied into each XML file that needs them.
一直在寻找相同的内容,最后找到了以下非常简单的解决方案。最好:开箱即用。
1. 更改字符串资源:
2. 使用字符串替换:
c.getString(R.string.welcome_messages,name,count);
其中 c 是上下文,name 是一个字符串变量和 count 你的 int 变量
你需要包含
在你的 res/strings.xml 中。
对我有用。 :)
Was looking for the same and finally found the following very simple solution. Best: it works out of the box.
1. alter your string ressource:
2. use string substitution:
c.getString(R.string.welcome_messages,name,count);
where c is the Context, name is a string variable and count your int variable
You'll need to include
in your res/strings.xml.
Works for me. :)
如果你想写百分比 (%),请重复它:
如果你简单地写
%1$d%
,你会得到错误:格式字符串 'percent' 不是有效的格式字符串因此不应将其传递给 String.format
。或者使用
formatted=false"
。If you want to write percent (%), duplicate it:
If you write simply
%1$d%
, you will get the error:Format string 'percent' is not a valid format string so it should not be passed to String.format
.Or use
formatted=false"
instead.在 Kotlin 中,您只需要像这样设置字符串值:
在布局上创建一个文本视图:
如果您使用 Anko,则在代码中执行此操作:
您可能需要从应用程序上下文中获取资源。
In Kotlin you just need to set your string value like this:
Create a text view on your layout:
Then do this in your code if you using Anko:
You might need to get your resources from the application context.
在您的字符串文件中使用它
并在您的代码中相应地使用
In your string file use this
And in your code use as accordingly
对于多语言项目
作为一个曾开发过具有多种不同语言和每个变体配置的主要白标解决方案的人,我可以说有很多事情需要考虑。
抛开文本方向不谈,光是语法就会让你头疼。
例如,项目的顺序是否可以改变,因此
是首选
,但是一旦您与经常不知道字符串或整数是什么的翻译人员一起工作,更不用说每种类型使用什么格式字符,或者一般人不知道参数在代码中应用的顺序,甚至您自己也忘记了,或者事情发生了变化,必须同时在多个位置进行更新,因此使用
MessageFormat
这样的方法是不可行的要么让非技术人员尝试找出
xlift
的想法甚至无法接受,那么到目前为止我所知道的最好的解决方案是使用显式的、命名的占位符作为这样..或其他任何与您的文本不冲突的内容。
虽然您可以像
这样使用 DOCTYPE,但它不适用于每种语言的单独文件。
因此,在您的
main/res/values/strings.xml
中提供这样的占位符和默认字符串,然后在您的变体
variant/res/values-de/strings.xml
中提供要使用它,请编写类似
使用一些辅助函数的内容,例如
简单地为翻译文件和开发提供参考。因此,每个构建风格不应该有一个默认文件。只有单个默认文件,然后是每个语言 x 变体的文件。
现在还有数字语法的问题。这可以通过
plurals
来解决,但是 xml 文件的复杂性再次增加。而且,正如所指出的,zero
并不像人们所期望的那样工作。但由于显示大小限制或 UI 的预渲染图像数量,您可能希望对应用计数进行限制,并且需要显示99+
而不是100
代码>.解决方案是使用辅助函数,例如覆盖和扩展复数解析器的行为。
如果每个变体都有可选功能,则添加一个
res/values/strings-beans.xml
,例如:而
variant-with-beans/res/value-en/strings- 中的变体beans.xml 只需要包含
特定于语言的覆盖,可以在每个文件的基础上提供。
要使用它,代码可以如下所示
,它解析为输出
,并且由于语言特定资源文件的简单性,然后可以使用电子表格或您公司自己的服务器端点等的部署工具生成它们。
我希望你喜欢我进入 Android 动态字符串资源世界的令人发狂的旅程,并希望你明白你不是那些必须获得相同功能才能在产品的 iOS 端工作的可怜的傻瓜,根据我的经验,需要 python 脚本来修改。 xcodeproj 文件并生成 swift 代码。
For Multilingual Projects
As someone who has worked on a major white label solution with many and varying languages and configurations per variant, I can say that there's a lot to consider.
Text direction aside, grammar alone can give you some headaches.
For instance can the order of items change thus
is to be preferred over
but once you work with translators who often don't know what a string or an integer is, let alone what formatting character to use for each type, or people in general who have no idea in what order the parameters are applied in your code, or even you yourself forget that, or things change which then must be updated at multiple places at once, so using
MessageFormat
likeisn't viable either and the idea of having non tech people try to figure out
xlift
can't even be entertained then the best solution I know so far is to use explicit, named placeholders as such..or whatever else doesn't conflict with your texts.
And while you could use DOCTYPE like
this will not work with separate files for each language.
Thus in your
main/res/values/strings.xml
provide the placeholders and default strings like thisand then in your variant
variant/res/values-de/strings.xml
and to use it, write something like
with the use of some helper functions like
for simply the reason of having a reference for the translation files and development. Hence there should not be a default file per build flavor. Only the single default file and then a file per language x variant.
Now there's also the issue of numeric grammar. This can be solved with
plurals
but here again the complexity of the xml file increases. And, as pointed out,zero
does not work as one would expect. But also you may want to have a limit to which your app counts due to display size limitations or number of pre-rendered images for your UI and need to display99+
instead of100
. A solution would be to use a helper function liketo override and extend the behavior of the plurals resolver.
And if you have optional features per variant then add a
res/values/strings-beans.xml
like:while the variant in
variant-with-beans/res/value-en/strings-beans.xml
only needs to containand language specific overrides can be provided on a per file basis.
To use this, the code can look like this
which resolves to the outputs
and due to the resulting simplicity of the language specific resource files, they then can be generated with deployment tools from spread sheets or your company's own server endpoints, etc.
I hope you enjoyed my maddening ride into the world of dynamic string resources for Android and hopefully appreciate that you're not the poor fools having to get the same functionality to work on the iOS side of the product which from my experience required python scripts to modify .xcodeproj files and generate swift code.
但是,您还应该阅读 Elias Mårtenson 在 Android 对“零”的复数处理。对某些值(例如“零”)的解释存在问题。
However, you should also read Elias Mårtenson's answer on Android plurals treatment of “zero”. There is a problem with the interpretation of certain values such as "zero".
在 res/values/string.xml
在 java 代码中
in res/values/string.xml
in java code
您可以使用
MessageFormat
:在 Java 代码中:
API 级别 1:
https://developer.android.com/reference/java/text/MessageFormat.html
You can use
MessageFormat
:In Java code :
API level 1:
https://developer.android.com/reference/java/text/MessageFormat.html
问题的直接 Kotlin 解决方案:
strings.xml
kotlinActivityORFragmentFile.kt:
A Direct Kotlin Solution to the problem:
strings.xml
kotlinActivityORFragmentFile.kt:
是的!您可以在不编写任何 Java/Kotlin 代码的情况下执行此操作,只需使用我创建的这个小型库(在构建时执行此操作)编写 XML,因此您的应用程序不会受到它的影响: com/LikeTheSalad/android-stem" rel="nofollow noreferrer">https://github.com/LikeTheSalad/android-stem
用法
你的字符串:
构建后生成的字符串:
Yes! you can do so without writing any Java/Kotlin code, only XML by using this small library I created, which does so at buildtime, so your app won't be affected by it: https://github.com/LikeTheSalad/android-stem
Usage
Your strings:
The generated string after building:
已接受答案的 Kotlin 版本...
Kotlin version of the accepted answer...