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.

Set locale dynamically at runtime

See original GitHub issue

Hello,

It unclear to me from the docs if using setLocaleMessage() is how you get dynamic locales at runtime to work. There are many articles about how to do that at build time, but none that I could see about runtime changes.

To give an example, here is what I mean in the form of a timeline:

  1. App loads, reads locale requirement.
  2. Makes AJAX call to fetch the appropriate JSON file (e.g. en.json).
  3. Calls setLocaleMessage() as shown below:
const i18n = new VueI18n({...});
...
i18n.setLocaleMessage('en', {...pathsHere});
...
//Later on:
//After another AJAX call to fetch fr.json:
i18n.setLocaleMessage('fr', {...pathsHere});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:13

github_iconTop GitHub Comments

55reactions
ghostcommented, May 2, 2018

There should be some easy way to change the global language/locale at run-time / dynamically and it should be documented very well and at a central point because this IMHO is an essential feature of this type of library.

44reactions
BenoitRanquecommented, Mar 11, 2018

Here is my solution:

  app.i18n = new VueI18n({
    locale: 'es',
    fallbackLocale: 'es',
    messages
  })

  Object.defineProperty(Vue.prototype, '$locale', {
    get: function () {
      return app.i18n.locale
    },
    set: function (locale) {
      app.i18n.locale = locale
    }
  })

  // this part happens later
 
 new Vue(app)

Usage:


this.$locale // root Vuei18n locale

this.$locale = 'en' // set root Vuei18n locale

Edit: this works too:


this.$root.i18n.locale // root Vuei18n locale

this.$root.i18n.locale  = 'en' // set root Vuei18n locale

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically change the locale in Angular 7 at runtime without ...
I am trying to change Locale_Id in my angular app at the runtime but must use window.location.reload() to fire changes at locale.
Read more >
The All-In-One Guide for Changing App Locale Dynamically in ...
xml file specifying the locale. To do so, we will have to follow the steps: File > New > Android Resouce File.
Read more >
Android Change Locale Dynamically - Java Articles
In this article, we will show you how to switch between different languages within an app using androids multi-language support.
Read more >
Change app default locale dynamically - Laracasts
i'm using mcamara localization package https://github.com/mcamara/laravel-localization in app an with default language set to english as per default laravel ...
Read more >
Set the runtime locale manually - Angular
Set the runtime locale manuallylink · Search for the Unicode locale ID in the language-locale combination in the @angular/common/locales/ directory. · Set the ......
Read more >

github_iconTop Related Medium Post

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