Vue-i18n: List of available languages

Created on 8 Jul 2017  路  9Comments  路  Source: kazupon/vue-i18n

I'd like to have an feature which allows me to output all available languages with one method. That would allow to make a list of all available translations.

Most helpful comment

In the meantime until this comes to fruition, I did find a stop gap:

computed: {
  languages() {
    return Object.keys(this.$i18n.messages);
  }
}

All 9 comments

Thank you for your feedback.

However, I could not understand the reason of your feedback.
Could you talk us more explanation please?

I started using https://github.com/bdswiss/country-language, which has methods to retrieve ISO standard names for languages, as well as country specific mapping.

When I'm adding the 'messages' to your plugin, I'm creating different Objects like en, de, ja. I'd think it would be useful if there is an built-in function to get all 'Objects' which are basically languages-codes in my case.

In the meantime until this comes to fruition, I did find a stop gap:

computed: {
  languages() {
    return Object.keys(this.$i18n.messages);
  }
}

@kazupon It could be nice to provide a getLocales() method which returns an array of the available locales: _['en', 'fr', 'pt']_.

PS: With the same result of the @sethmurphy18 snippet.

Could someone provide use-case for this feature?

I imagine this may be useful to create language selector UI which shows available languages based on messages objects.
But not sure.

Example:
If you want to redirect a user to a path (with locale):

routes: [
    {
      path: '/', redirect: (to) => {
        const browserLocale = getBrowserLocale();
        if (Object.keys(VueI18n.messages).includes(browserLocale)) {
          return `/${browserLocale}`;
        }

        return `/${VueI18n.fallbackLocale}`;
      },
    },
]

@rimiti
That use-case totally makes sense 馃憤

It's a good job @exoego, you rock 馃殌

Was this page helpful?
0 / 5 - 0 ratings

Related issues

forddyce picture forddyce  路  5Comments

koslo picture koslo  路  5Comments

jarikmarwede picture jarikmarwede  路  4Comments

JakubKoralewski picture JakubKoralewski  路  5Comments

zhangruinian picture zhangruinian  路  4Comments