How to load locale dynamically

See original GitHub issue

I am developing an ionic 2 app. To localize angular2-moment I just included lang module in app.module.ts after including angular2-moment like this and that automatically pulled spanish language:

import { MomentModule} from 'angular2-moment';
import 'moment/locale/es';

My app use cordova-plugin-globalization to dinamically set app language so, how can I load the lang locale dynamically?

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
D4rkMindzcommented, May 24, 2018

I made a pipe and it worked to actualize the date on a language change:

@Pipe({
  name: 'dynamicMoment'
})
export class MomentPipe implements PipeTransform {
  /**
   * MomentPipe constructor
   * @param {TranslateService} translate
   */
  constructor(private translate: TranslateService) {
  }

  /**
   * Make moment dynamic
   * @param {string} value
   * @param {string} format
   * @returns {any}
   */
  transform(value: string, format?: string): any {
    // make the moment format configurable
    format = format ? format : 'MMMM Do YYYY';
    // get the initial value
    const initVal = moment(value).locale(moment.locale()).format(format);
    // insert the value into a new behaviour subject. If the language changes, the behaviour subject will be 
    // updated
    const momentObs = new BehaviorSubject<string>(initVal);
    this.translate.onLangChange.subscribe(() => {
      // format the new date according to the new locale
      const val = moment(value).locale(moment.locale()).format(format);
      momentObs.next(val);
    });
    return momentObs; // needs to be piped into the async pipe
  }

}
1reaction
beaussancommented, Mar 26, 2017

Hi, I managed to do so by ` moment. locale (‘es’); when a language change is detected, however, I haven’t found a way yet to update the currently printed ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I dynamically import locales in Angular 9?
The solution is to use Webpack's "magic comments". The following is enough to get everything loading properly: const base = import( /* ...
Read more >
Dynamic Import of Locales in Angular | by Michael Karén
The Goal. Remove import statements for locales; Replace switch statement with dynamic import; Lazy loading. We want to remove the imports of locales:...
Read more >
Angular Dynamic Locale - StackBlitz
* Open terminal and go to project folder. * Run `npm` or `yarn` in terminal to install.
Read more >
Code-splitting react-i18n locales using dynamic imports
Hence, a better solution would be to load only the required locale files or load them on demand. And this can be achieved...
Read more >
Lazy Loading Locales with Angular - ANGULARarchitects
However, we can divert it from its intended use to load the meta data dynamically on demand. This article shows how to leverage...
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