G
G
Gumper2021-08-19 00:09:46
Internationalization and localization
Gumper, 2021-08-19 00:09:46

How to localize time?

I have a task to translate words depending on the number:
For example:
1 Minute
2 Minutes
30 Minutes ...
Or
1 Day
3 Days
8 Days ...

I have vue-i18n on my project, but the example for solving this problem is very poor:
https://kazupon.github.io/vue-i18n/guide/pluraliza...
And I don't really understand what to do if you need to write a different algorithm for different words

. What are the solutions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-08-19
@Gumper

For a long time there was a post on Habré with a solution in different J.P .: “War with robots: declension of nouns after numerals”
On the Internet, there was a more concise solution in JS:

var numToStr = function(num, arrText) {
  if (num % 10 === 1 && num % 100 !== 11) { 
    return arrText[0];
  } else if (num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20)) {
    return arrText[1];            
  }
  return arrText[2];
}

Arguments: a number and an array of srv for 1, 2, and 5.
For example:
numToStr(42, ['яблоко', 'яблока', 'яблок']) // "яблока"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question