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.

Locale not working as expected when with i18n routes in generated mode

See original GitHub issue

When using both nuxt-i18n and nuxtjs/moment I ran into an interesting issue that I think deserves the attention of this module. If this is the wrong place for this issue then I apologize.

Essentially, I wanted to globally set the locale as the following plugin:

@/plugins/moment.js

export default ({ app }) => {
  const { $moment } = app;
  $moment.locale(app.i18n.locale);

  app.i18n.onLanguageSwitched = (_oldLocale, newLocale) => {
    $moment.locale(newLocale);
  };
};

When working in universal mode, this works as expected. But, when working in generated mode the dates would flicker with the last locale of the last generated route because moment does not get reinitialized between routes.

Here’s an example: https://github.com/Burtonium/moment-test/tree/bug-example https://moment-i18n-bug-example.netlify.com/

You’ll see the date flicker in Spanish. If not, throttle your internet in the browser to replicate.

The fix was to only locally use the locale and overriding the moment instance completely and use local instances of moment:

@/plugins/moment.js

export default ({ app }, inject) => {
  const { $moment } = app;
  inject('moment', (...args) => {
    const localMoment = $moment(...args);
    localMoment.locale(app.i18n.locale);
    return localMoment;
  });
};

This fixed everything for me. Not sure if this is the right way to go about it but would like to know your thoughts. Maybe a warning in the documentation with a proposed solution would go a long way and save people some time. Let me know.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sonnh58commented, Nov 16, 2021

Thanks for your fix

1reaction
farnabazcommented, Feb 3, 2021

This should fix using latest version of Nuxt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Internationalized Routing - Next.js
Next.js has built-in support for internationalized (i18n) routing since v10.0.0 . You can provide a list of locales, the default locale, and domain-specific ......
Read more >
next.js - NextJS Localisation Routing Not Working As Expected
I've got a NextJS app that I'm trying to add localisation to but I'm noticing some odd behaviour in regards to the routing...
Read more >
Globalization and localization in ASP.NET Core
You can develop an app targeted for localization and not need to create ... and was originally intended to specify the user's language....
Read more >
The Ultimate Vue Localization Guide | Phrase
One problem with our current localized routing solution is that we would need to inject the :locale route parameter manually every time we ......
Read more >
Using Angular routes in a single-page application
From your terminal, navigate to the angular-router-sample directory. Create a component, crisis-list. content_copy ng generate component crisis ...
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