Last active
March 6, 2022 17:18
-
-
Save andrejsharapov/0b982a25b0e7a6ca69c0d735032b5e28 to your computer and use it in GitHub Desktop.
Declensions years [склонения лет: js]. Use i18n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
author: { | |
name: 'User name', | |
}, | |
pages: { | |
index: { | |
title: 'Home', | |
sections: { | |
about: { | |
message: | |
'My name is {author}. Already {years} am ... | |
}, | |
}, | |
}, | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template lang="pug"> | |
p {{ $t("pages.index.sections.about.message", { author: $t("author.name"), years: declensions(years, localeWords) }) }} | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
years: 5, | |
} | |
}, | |
computed: { | |
localeWords() { | |
if (this.$i18n.locale === 'ru') { | |
return ['год', 'года', 'лет'] | |
} else if (this.$i18n.locale === 'en') { | |
return ['year', 'years', 'years'] | |
} | |
return [] | |
}, | |
}, | |
methods: { | |
declensions(number, words) { | |
return ( | |
this.years + | |
' ' + | |
words[ | |
number % 100 > 4 && number % 100 < 20 | |
? 2 | |
: [2, 0, 1, 1, 1, 2][number % 10 < 5 ? Math.abs(number) % 10 : 5] | |
] | |
) | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment