Greetings Tell me how to properly use Plurals in relation to seconds, minutes, hours, days to display the time that "left" and "passed"

<plurals name="second_plurals"> <item quantity="zero">секунд</item> <item quantity="one">%1$d секунда</item> <item quantity="few">%1$d секунды</item> <item quantity="many">%1$d секунд</item> <item quantity="other">%1$d секунд</item> </plurals> 

Zero, one, two is understandable, but for example, few are, as far as I understand, from 2 to 5, and 5 will no longer be "seconds" and "seconds." And further - with many, for example 101 seconds, 102 seconds, ... i.e. must act rules for one, two. How to account for this difference?

  • Keep, in my opinion, a complete and at the same time brief article about plurals dimasokol.ru/plurals-in-android - Vladyslav Matviienko
  • one
    Maybe it will be cheaper to reduce to “sec.” And not to bathe? - Arik
  • Metalurgus, mastered for this article. The question remained unclosed - Ruben
  • @Ruben, it means poorly mastered, there are answers to all your questions. It is only necessary to read not through the word, but in full - Vladyslav Matviienko

5 answers 5

Article

zero - a string for zero, the absence of something; in some languages, also for numbers ending in zero; one is a string for numbers ending in one; in some languages, only for a unit;

two - for numbers ending in two, or only for two;

few — here, under the word “several,” the specifics are no longer hidden, the processing is completely dependent on the language rules; for example, for the Russian language, this refers to numbers ending in 2, 3, and 4 (this is the case, despite the existence of the “two” rule);

many - similarly, a “non-specific” category, it can be understood as “something more than a few”; in Russian - from the top five and above;

other - everything else.

In short, the unique rules are formulated for each language separately.
At the end of the article there is an example that should dispel all misunderstandings:

 <plurals name="plurals_2"> <item quantity="zero">нет андроидов</item> <item quantity="one">%1$d андроид</item> <item quantity="two">%1$d андроида</item> <item quantity="few">%1$d андроида</item> <item quantity="many">%1$d андроидов</item> <item quantity="other">%1$d андроидов</item> </plurals> // Выведем данные в формате: 8 = many : 8 андроидов for (int i = 0; i < 30; i++) { String pluralTest = this.getResources().getQuantityString(R.plurals.plurals_1, i, i); String pluralAndroid = this.getResources().getQuantityString(R.plurals.plurals_2, i, i); android.util.Log.i("Plurals", pluralTest + " : " + pluralAndroid); } 

Result:

 0 = many : 0 андроидов 1 = one : 1 андроид 2 = few : 2 андроида 3 = few : 3 андроида 4 = few : 4 андроида 5 = many : 5 андроидов 6 = many : 6 андроидов 7 = many : 7 андроидов 8 = many : 8 андроидов 9 = many : 9 андроидов 10 = many : 10 андроидов 11 = many : 11 андроидов 12 = many : 12 андроидов 13 = many : 13 андроидов 14 = many : 14 андроидов 15 = many : 15 андроидов 16 = many : 16 андроидов 17 = many : 17 андроидов 18 = many : 18 андроидов 19 = many : 19 андроидов 20 = many : 20 андроидов 21 = one : 21 андроид 22 = few : 22 андроида 23 = few : 23 андроида 24 = few : 24 андроида 25 = many : 25 андроидов 26 = many : 26 андроидов 27 = many : 27 андроидов 28 = many : 28 андроидов 29 = many : 29 андроидов 

Total for the Russian language:
Zero is used for zero.
For numbers ending in 1 (except numbers ending in 11 ), one is used.
For numbers ending in 2 (except numbers ending in 12 ) two is used (only in Russian, few are used for these numbers)
For numbers ending in 3, 4 (except numbers ending in 13 , 14 ) few are used.
For all others, many are used.
Other is used for other cases that are not in Russian.
Despite this, one and the other are a mandatory minimum that should always be defined.

The same article describes a problem with plurals on API <3.0. There all plurals work according to the rules of the English language. The solution to this problem is also described there - use the android-i18n-plurals library,

P. S. I took all of the above from the above article, and if

mastered it for this article. The question remained unclosed

then this means only one thing - poorly mastered.

  • This works for devices with Russian locale, so do not forget to write your function for the general case. - CoolMind

The problem was solved ... I forgot to rename values ​​in values-ru, otherwise almost all the numbers fell into OTHER. Thank you all, problem solved

    For the Russian language, one is all numbers ending in 1, except 11. few - ending in 2 - 4, except 12 - 14. That is, there will be no problems.

    To format relative dates in order not to mess around yourself, you can use DateUtils # getRelativeTimeSpanString () and other methods of this class. So you will have the text not only for one language.

      A bit of "scooping" if English is selected in your android OS, the application will search for the values-en folder, if it is not found, then the resources from the values ​​folder will be taken. Similarly, for the Russian language, if the OS language is selected Russian, then the resources will first be searched in values-ru, and if there is no such one, then they will be taken from the values.

      Change OS language - Settings - Language and input

      To fully understand the issue, you need to read about the localization of applications. http://developer.alexanderklimov.ru/android/locale.php

      • I use the emulator with the English interface (by default). strings.xml is only one. Wrong values ​​are taken (other for almost all cases). - CoolMind

      If the phone has English, then when using plurals, the value for case 2 will be replaced with the value for zero. This is because in English the grammatical form for 0 is much the same. And if it is used in an application where the language is Russian, then there will be 2 Tickets and not 2 Tickets