📜 ⬆️ ⬇️

Android string.xml - a few things to remember

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


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 .


image

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.


image

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.


image

res / values ​​/ strings.xml


image

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 .


Divide


Separate lines that refer to the same screen with prefixes and comments.

image

res / values ​​/ strings.xml


  1. Add a prefix in the form of a screen name to each line to make it easier to understand which screen this resource belongs to.
  2. A clean string.xml file will help you to easily support and translate various languages ​​- screen by screen.

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.


Formatting


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.


image

res / values ​​/ strings.xml


image

java code


The correct way is to use Resources # getString (int id, Object ... formatArgs) .


image

res / values ​​/ strings.xml


image

res / values-UA / strings.xml


image

java code


Plural


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


image

res / values ​​/ strings.xml


image

java code


The correct way is to use Resources # getQuantityString (int id, int quantity)


image

res / values ​​/ strings.xml


image

java code


Word Illumination


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.


image

res / values ​​/ strings.xml


image

java code


')

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


All Articles