question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] - Override of the getChoiceIndex function should be available via options object on initialization

See original GitHub issue

I use vue-i18n via another lib nuxt-i18n so I doesn’t have direct access to the library to override getChoiceIndex method in a clean way as it described in the documentation and my personal opinion is that it looks dirty to to this at all. Instead, what I like is to be able to do override this method via default vue-i18n config object. The syntax is pretty simple:

new VueI18n({
  locale: 'pl', // set locale
  getChoiceIndex(choice, choicesLength) {
     // Custom pluralization code goes here
  }
})

so when using nuxt-i18n module I can simply do:

modules: [ ['nuxt-i18n', {
locales: [...], 
vueI18n: {
   getChoiceIndex(choice, choicesLength) {
     // Custom pluralization code goes here
  }
}
}]]

I think such approach is much more cleaner to use!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
bludniccommented, Apr 22, 2019

No need to override VueI18n.prototype.getChoiceIndex . Update nuxt and vue-i18n to latest version, and use pluralizationRules option.

$ npm i --save nuxt@2.6.2
$ npm i --save vue-i18n@8.10.0

plugins/i18n.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)

function slavicPluralization (choice, choicesLength) {
  if (choice === 0) {
    return 0
  }

  const teen = choice > 10 && choice < 20
  const endsWithOne = choice % 10 === 1

  if (!teen && endsWithOne) {
    return 1
  }

  if (!teen && choice % 10 >= 2 && choice % 10 <= 4) {
    return 2
  }

  return (choicesLength < 4) ? 2 : 3
}

export default ({ app, store }) => {
  app.i18n = new VueI18n({
    locale: store.state.locale,
    fallbackLocale: 'en',
    messages: {
      'en': require('~/locales/en.json'),
      'ru': require('~/locales/ru.json')
    },
    pluralizationRules: {
      ru: slavicPluralization
    }
  })
}

nuxt.config.js

plugins: [
  '~/plugins/i18n.js'
]
2reactions
lukaVargacommented, Mar 3, 2019

Ok so upon some further digging, the problem is not in vue-i18n but rather in nuxt-i18n. As you can see here, nuxt-i18n takes the vueI18n options and stringifies them using JSON.stringify. That causes the functions to be removed and just returns an object pluralizationRules: {}.

So here’s a nice workaround. Instead of adding pluralizationRules within vueI18n options, you can add a plugin:

export default ({ app }) => {
  app.i18n.pluralizationRules = {
    sl: slovenePluralization
  };
}

function slovenePluralization(number, choicesLength) {
  let option = null;

  if (number === 0) {
    option = 3;
  }

  if (number % 100 === 1) {
    option = 0;
  }

  if (number % 100 === 2) {
    option = 1;
  }

  if (number % 100 === 3 || number % 100 === 4) {
    option = 2;
  }

  if (option === null) {
    option = 3;
  }

  return option > choicesLength ? choicesLength : option;
}

and then you just have to register the plugin with ssr: true:

// nuxt.config.js

module.exports = {
  ...
  plugins: [
    { src: '~plugins/pluralization.js', ssr: true }
    ],
  ...
};

That way it works for SSR as well 😃

Hope that helps. cc @fibigerg @AndrewBogdanovTSS

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Ultimate Guide to Vue Localization with Vue I18n - Medium
We'll use getChoiceIndex() to override the VueI18n instance's method by the same name. getChoiceIndex() does the work of determining which ...
Read more >
API references | Vue I18n
API references. # Extension of Vue. # Vue constructor options. # i18n. Type: I18nOptions. Component based localization option.
Read more >
Vue i18n: Building a multi-language app with locale switcher
Vue i18n is a key process needed to localize your Vue apps and websites. Learn how to set up a Vue app with...
Read more >
A B C D E F G H I J K L M N O P ...
return the object reference for an instance method to be called (we are still in the caller's frame). getCalleeThis(int) - Method in class...
Read more >
Index (OfficeFloor 3.33.0 API)
Abstract functionality for overriding the System.getenv(String) values in tests. ... array will provide all Content-Type parsing available for the Object .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found