If you are developing an application that covers multiple countries, when dealing with the number of problems, you will be bound to use different strings depending on the number.
Different languages do not deal with the same amount of treatment, which is very common, give a simple example to illustrate the difference in the number of modifications in Chinese and English:
In Chinese, 1 books, 2 books, ... n book,
In English, 1 book, 2 books, ... n books. However, even in the case of 0, use books, or 0 books.
Chinese has not made a distinction in the treatment of singular and plural cases, and English has made a difference. In addition to English, many languages have been made with similar differences. In some cases, we also have to use different words according to different numbers, like 0 or one, two, very few, many, and then other ways of modification. Even at this point, different countries are used differently.
So when it comes to multi-lingual processing of quantitative problems, it's a tricky one. You cannot use the IF statement in the code according to the language of different countries to make various judgments, matching the corresponding strings, it is conceivable that these work is extremely boring. The Android system provides the Getquantitystring () method to handle this problem by selecting your pre-defined string resources based on the language of the different countries. You can define the string in the corresponding Values-xx/strings.xml file according to the different national languages, of course you have to follow the rules of Andorid
The following is an example of a resource definition:
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <plurals name= "Plural_name" > <item quantity=["Zero" | "One" | "Both" | "Few" | "Many" | "Other"] >text_string</item> </plurals></resources></span>
The rules can be used in the corresponding syntax from the definitions above.
<resources>
This is the root where the resource is defined.
<plurals>
It is a collection of strings containing strings that you can use to decorate different numbers. It can contain one or more <item> elements.
name refers to the resource, and name is referenced in the code as a Reousrce ID.
<item>
Represents a single or multiple number of strings. The value can be a reference to another string.
Property:
Quanitty:
Keywords. The value of the quantity representation, which determines which string to refer to. The following is a reference to the different values
The following example shows:
Files in Res/values/strings.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <plurals name= "Numberofsongsavailable" > <!-- as a developer, you should all supply "one" and "other" strings. Your translators would know which strings is actually needed for their language. Always include%d in "one" because translators would need to use%d for languages where "one" doesn ' t mean 1 (as ex plained above). --<item quantity= "one" >%d song found.</item> <item quantity= "Other" >%d songs found. </item> </plurals></resources></span>
files in Res/values-pl/strings.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <plurals name= "Numberofsongsavailable" > <item quantity= "One" >znaleziono%d Piosenk?. </item> <item quantity= "Few" >znaleziono%d piosenki.</item> <item quantity= "other" >znaleziono%d piosenek.</item> </plurals></resources></span>
Code Reference:
<span style= "FONT-SIZE:18PX;" >int count = getnumberofsongsavailable (); Resources res = getresources (); String Songsfound = res.getquantitystring (r.plurals.numberofsongsavailable, Count, Count);</span>
Note: When you use the Getquantitystring () method, if you reference a string that contains%d string format, you need to transfer the count to the method two times. Using the above example, the first time you pass in the parameter count is for plural to provide the number of quantity to judge, the second time you pass in count is inserted into the placeholder%d.
Complex processing methods for strings in Android