Good day! I present to your attention a free translation of an article from the GDE (Google developer expert) Dmytro Danylyk . Actually, here is the original . This article describes the right approaches for working with strings.xml, and it will be especially useful for developers who are developing multilingual applications. I ask under the cat.
This article is about such a trivial android thing as string.xml.
Do not reuse lines for different screens.
Let's say that you have a loading dialog on the Sign In or Sign Up screen. Since both screens have a loading dialog, you decide to use the same strings - R.string.loading .
res / values / strings.xml.
Later, if you decide to use different ones, you will need to create 2 different lines and modify them directly in the .java classes. If you use different strings from the beginning, you would have to modify only the strings.xml file.
res / values / strings.xml
You will never know in advance the support and translation of which language you have to add. It's all about the context: in one language you can use the same word in one context, but in the other - this word will not fit the meaning.
res / values / strings.xml
res / values-UA / strings.xml
Please note that in this case the English version of strings.xml uses the same word - “Yes” for both cases R.string.download_file_yes and R.string.terms_of_use_yes strings .
But the Ukrainian version of strings.xml uses 2 different words - “Garazd” for R.string.download_file_yes and “So” for R.string.terms_of_use_yes .
Separate lines that refer to the same screen with prefixes and comments.
res / values / strings.xml
Create different strings.xml for each screen
If you want, you can create a string.xml file for each screen - settings-strings.xml, profile-strings.xml . But usually the application has about 10-20 screens, respectively, you need to have 10-20 string.xml files in each language folder. I think it will be a mess.
Use Resources # getString (int id, Object ... formatArgs) to format strings
Never do concatenation through the + operator, since in other languages the word order may vary.
res / values / strings.xml
java code
The correct way is to use Resources # getString (int id, Object ... formatArgs) .
res / values / strings.xml
res / values-UA / strings.xml
java code
Use Resources # getQuantityString (int id, int quantity) for quantity strings
Do not solve plural problems in java code, since different languages have different rules for matching the plural
res / values / strings.xml
java code
The correct way is to use Resources # getQuantityString (int id, int quantity)
res / values / strings.xml
java code
Use html text to highlight static words.
If you want to change the color of some words in TextView - ForegroundColorSpan is not always the best option, because the backlight is done through the index and it is not safe in a multilanguage application. It is better to use html font color tags inside your strings.xml file.
Imagine you have the text “Discover and play games.” And you want to highlight the “Discover” word and the “play” word in blue.
res / values / strings.xml
java code
Source: https://habr.com/ru/post/307798/
All Articles