How to format string for different languages ​​with different arguments in Android?

I have two string resources. The first in the main values ​​is in English and does not have a string argument:

<string name="example">Have a good day!</string> 

But in French, with-fr values, we have another phrase with one argument, for example:

 <string name="example">Bonne journée M. %1$s!</string> 

How to use resource string formatter? This works, but it does not seem correct (and it generates a line warning):

 textView.setText(getString(R.string.example, name)); 

The lint warning is "StringFormatInvalid", and the description of the "Format string" example 'is not a valid format string, so it cannot be passed to String.format "

+5
source share
3 answers

There is nothing wrong with your approach. Formatting works fine even if there are too many arguments. The remaining arguments are simply discarded.

Lint warnings indicate possible unintended errors in the code. But if you know what you are doing, you can simply suppress the warning by adding the @SuppressLint("StringFormatMatches") annotation to the attached method or class.

+3
source

You can use the String.format method (String args, Object ... args). More information. at http://developer.android.com/reference/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29

0
source

he explained here! http://programmerguru.com/android-tutorial/android-localization-at-runtime/ you must create a custom locale and set a new config for getResource

0
source

Source: https://habr.com/ru/post/1241076/


All Articles