Generator-jhipster: Update dayjs locale when changing app language

Created on 28 Oct 2020  路  19Comments  路  Source: jhipster/generator-jhipster

Overview of the feature request

Currently when changing the language from application, the locale is not updated. it will keep the default locale of days. that need to change.

Motivation for or Use Case

Locale dayjs need to be updated according to the use language selection.

Related issues or PR

https://github.com/jhipster/generator-jhipster/issues/12575

  • [x] Checking this box is mandatory (this is just to show you read everything)
$$ bug-bounty $$ $100 area angular front react vue

Most helpful comment

Never mind i upgraded the node and it works now
image

All 19 comments

@FabienEssid and myself started looking on how to add the improvement.

  • [ ] Check that locale are imported properly.
  • [ ] Code to be used for changing dayjs locale dayjs.locale(LOCALE_VALUE)
  • [ ] Find the method called when the application language is changed.
  • [ ] Add the changements for React, Angular and VueJs
  • [ ] Test that everything is working

A confirm the issue for angular. ~It works for vue.~ I was wrong, it also doesn't work for vue.

I think for vue it can be added to translation.service.ts in refreshTranslation. Need to check for angular/react

It works for vue by extending translation.service.ts as follows:

import axios from 'axios';
import dayjs from 'dayjs';
import VueI18n from 'vue-i18n';
import { Store } from 'vuex';
import { BUILD_TIMESTAMP } from '@/constants';

export default class TranslationService {
  private store: Store<{}>;
  private i18n: VueI18n;

  constructor(store: Store<{}>, i18n: VueI18n) {
    this.store = store;
    this.i18n = i18n;
  }

  public refreshTranslation(newLanguage: string) {
    let currentLanguage = this.store.getters.currentLanguage;
    currentLanguage = newLanguage ? newLanguage : 'en';
    if (this.i18n && !this.i18n.messages[currentLanguage]) {
      this.i18n.setLocaleMessage(currentLanguage, {});
      dayjs.locale(currentLanguage);
      axios.get(`i18n/${currentLanguage}.json?buildTimestamp=${BUILD_TIMESTAMP}`).then(res => {
        if (res.data) {
          this.i18n.setLocaleMessage(currentLanguage, res.data);
          this.i18n.locale = currentLanguage;
          this.store.commit('currentLanguage', currentLanguage);
        }
      });
    } else if (this.i18n) {
      this.i18n.locale = currentLanguage;
      dayjs.locale(currentLanguage);
      this.store.commit('currentLanguage', currentLanguage);
    }
  }
}

@medamineziraoui Would you like to do a PR? If you need more help don't hesitate to reach out here.

Hello @atomfrede yes i would love to, this would be my first contribution if done successfully so definitely would not hesitate to ask if I need help.

@medamineziraoui Awesome! I think for angular you need to look here and for react it could be done in the locale reducer/store

@atomfrede Thank you

@medamineziraoui is currently on holidays. We should work on this issue when he'll be back next week :slightly_smiling_face:

Hello im starting working on this issue today, i will keep you updated with my progress :slightly_smiling_face:

  • [ ] Vuejs: we tried the fixed offered by @atomfrede, we confirmed that it change update the dayjs with the current app locale.
  • [ ] React: I located this reducer that update locale in the hipster TranslatorContext
export type LocaleState = Readonly<typeof initialState>;

export default (state: LocaleState = initialState, action): LocaleState => {
  switch (action.type) {
    case ACTION_TYPES.SET_LOCALE: {
      const currentLocale = action.locale;
      if (state.currentLocale !== currentLocale) {
        dayjs.locale(currentLocale); // <====== updated this dayjs locale before setting state in the TranslatorContext
        TranslatorContext.setLocale(currentLocale);

What do think about updating React this way to fixe the dayjs locale?

  • [ ] Angular: i tried starting the project but i got an error
    image
    I will look it up tomorrow
  • When we tried starting spring section of a generated jhipster project 3 weeks ago, we got this error
    image
    On multiple machines.
    Today we tried starting again the same process ( ofc after pulling changes from jhipster main project , we got the changes ), and we still got the exactly the same error.
    Did you experience the same issue before ?
    We only could work with the generator when we changed
<jhipster-dependencies.version>7.0.0-SNAPSHOT</jhipster-dependencies.version> 

jhipster-dependencies.version to 0.1,0
This ofc broke multiple parts in the project but it was enough for us to test what we need for Vuejs and Reactjs.

@medamineziraoui You need to checkout jhipster-bom project and build it locally.
There is no breaking change between released version 0.1.0 and current master (7.0.0-SNAPSHOT) of jhipster-bom project until now... But better to always compile it locally.

Thank you for your answer @DanielFran, are you talking about this repo ? https://github.com/jhipster/jhipster-bom
Is there anything else I need to do ( any change in configuration for generated project ?)

Yes, it is this repo. No changes required on generator side...
Please follow the instructions how to build jhipster-bom project in the readme.

Ok thank you, i will test this tomorrow :slightly_smiling_face:

  • Angular: i tried starting the project but i got an error

Try to delete node_modules folder and package-lock.json file and run npm install again, this can solve this error.

Thank you @kaidohallik i will try this later this day and i will keep you updated :slightly_smiling_face:

I tried both solution for jhipster-bom and node_modules and works fine ( needed to upgrade node version too for angular ), i got another issue when trying to link the generator-jhipster
image
i will check it out and continue tomorrow

Never mind i upgraded the node and it works now
image

@medamineziraoui Do you still have issues with your setup?

Hello no i don't have setups issues thx to your advice guys so thank you, i wasn't able to work fixe the issue those pass days but i will this weekend.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaidohallik picture kaidohallik  路  3Comments

tomj0101 picture tomj0101  路  3Comments

marcelinobadin picture marcelinobadin  路  3Comments

chegola picture chegola  路  4Comments

DanielFran picture DanielFran  路  3Comments