Skip to content

Instantly share code, notes, and snippets.

@andrejsharapov
Last active March 6, 2022 17:18
Show Gist options
  • Save andrejsharapov/0b982a25b0e7a6ca69c0d735032b5e28 to your computer and use it in GitHub Desktop.
Save andrejsharapov/0b982a25b0e7a6ca69c0d735032b5e28 to your computer and use it in GitHub Desktop.
Declensions years [склонения лет: js]. Use i18n
export default {
author: {
name: 'User name',
},
pages: {
index: {
title: 'Home',
sections: {
about: {
message:
'My name is {author}. Already {years} am ...
},
},
},
},
}
<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