Answer the question
In order to leave comments, you need to log in
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
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];
}
numToStr(42, ['яблоко', 'яблока', 'яблок']) // "яблока"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question