This is the 20th day of my participation in the August Text Challenge.More challenges in August

In order to make it easier for developers to release their applications to people in different languages around the world, Android suggests that developers do not hardcode the text content related to UI presentation during development, but write the content into strings.xml, which is more flexible and easier to translate into different languages. Here is a case to introduce the use of strings step by step.

Basic usage

For example, if I wanted to display a sentence on my app page called “I want to buy a Kindle”, I could add the following to strings.xml:

<string name="buy_kindle"> I want to buy a Kindle</string>Copy the code

Used after getting the string from getString where it is needed.

getString(R.string.buy_kindle)
Copy the code

Add parameters

Now you think this sentence kills the number of Kindles you bought, because you use this sentence in different places in your code, but the number of Kindles you bought is different. The Strings resource gives developers the freedom to add arguments anywhere in the string. To solve this problem, for example:

<string name="one"> </string> <string name="buy_kindle">Copy the code

If there are parameters, you can pass them in the following way

getString(R.string.buy_kindle, getString(R.string.one))
Copy the code

If you want to add multiple parameters to the sentence, for example, “I’d like to buy a Kindle for Xiao Ming”, and you can choose who you want to give it to.

<string name="buy_kindle"> I want to buy %1$s Kindle for %2$s</string> getString(r.sing.buy_kindle, getString(r.sing.one), getString(R.string.xiaoming))Copy the code

Note that for multiple arguments, you need to add location information to the argument, as shown in %1$s. The added parameters will correspond to the entry number based on the location information. With the formatter method in the particular way such as Java, please refer to: developer.android.com/reference/j…

internationalization

For example, to translate this sentence into English, you can create a new English Values resource file, as shown in the following image:

Translated as follows:

<string name="buy_kindle">I want to buy %1$s Kindle</string>
Copy the code

We can find that the word Kindle is not translated. For words that do not need to be translated in Android, we can use xliff: G tag to mark them, so that when we give resources to others or use Google Play automatic translation service, the other party will know that this part does not need to be translated. As follows:

支那

The < resources XMLNS: it = "urn: oasis: names: tc: it: document: 1.2" > < string name = "buy_kindle" > I want to buy % s < it: g id="Kindle">Kindle</xliff:g></string>Copy the code

Note that when using this tag, we add the namespace before the resource:

XMLNS: it = “urn: oasis: names: tc: it: document: 1.2”.

TIPS: Google Play Console provides APP TRANSLATION SERVICE TRANSLATION SERVICE

String with quantity determination

If I want to express that I bought multiple Kindles, but in English grammar I need to use the plural Kindles, how do I do that? Android provides Plurals methods for such situations.

<plurals name="buy_kindle"> <item quantity="one">I want to buy a Kindle</item> <item quantity="other">I want to buy some  Kindles</item> </plurals>Copy the code

The second argument is passed in quantity, and the system selects the corresponding display according to quantity. Parameters can also be added to this method:

getResources().getQuantityString(R.plurals.buy_kindle, 2)
Copy the code

The Chinese part can be changed to:

支那

<xliff:g id="Kindle">Kindle</xliff:g></item> <item <xliff:g id="Kindle">Kindle</xliff:g></item> </ Plurals >Copy the code

TIPS: For more details on Quantity String, please go to:Developer.android.com/guide/topic…

Add special characters

Some characters cannot be written directly in strings.xml, such as “<“, “>”, but can be displayed using the corresponding ASCII code instead. For example, to express “I want to buy a Kindle<$100>”, you can:

<xliff:g id="Kindle">Kindle<$100></xliff:g></item>Copy the code

Where “<” is ASCII < and “>” is ASCII #062. More special characters and ASCII corresponding tables can be seen: Common Characters and ASCII Decimal Corresponding Tables