How can I use localized placeholder (`formatArgs`)...
# compose
l
How can I use localized placeholder (
formatArgs
) in
stringResource()
? E.g.
Copy code
<string name="generic_input">Please insert %s</string>
<string name="length_input">length</string>
Would nested `stringResource`s work?
Copy code
Text(
    text = stringResource(R.string.generic_input, stringResource(R.string.length_input))
)
d
Technically it would. But it is hard to tell if all languages would follow the pattern, it might true for English but not necessarily other languages. It could be that "Please insert X" might be written differently depending on the word X. So generally I'd advice against this type of building up strings with other localized strings. It also becomes hard for translators to follow since everything is references.
👍 1
l
That's a very good point, thanks!
👍 1
d
Np, your welcome.
v
Copy code
But it is hard to tell if all languages would follow the pattern
Not sure how exactly this related to the topic.
<string name="generic_input">%s insertio in someOtherLanguage </string>
will still work.
Copy code
It also becomes hard for translators to follow since everything is references.
Is there another way?
d
@Vlad It works in that specific scenario but if the input of
%s
is used in different places it might suppose to have different format in different languages depending where in a sentence the phrase/word is used. The alternative is to make them more flat. Use
%s
&
%d
for cases where there is strict dynamic input from code. E.g a value, day of week, name of something etc.
v
English:
<string name="generic_input">Please insert %s</string>
Italiano:
<string name="generic_input">%s insertio in someOtherLanguage </string>
d
Of course you can move around the string substitution that is not the problem. Given the surrounding sentence
%s
may be different in different languages, and if you reuse
%s
across multiple sentences it won't work in all places.
v
And how do make them more flat?
d
In the case above the example was
Please insert length
You can imagine length would be substituted with
height
,
weight
or something else: It can be 3 different strings:
Please insert height
Please insert weight
Please insert length
But if your scenario is
Your score is %d points
then it it obviously you cant have separate string for that. My point is, overly nesting strings, without a clear reason too is not good. A good warning flag is when you start inserting longer sentences or phrases into other phrases.
Language rules are necessarily not strict or logical, there is plenty special cases. 😄