2

Angular 4.x - ngx-translate - angular2-moment

How to use "amTimeAgo" and "amLocale" with dynamic locale? I mean, i.e this works

<p> {{ note.createdAt | amLocale: 'es' | amTimeAgo  }} </p>

but I want to convert 'es' into a dynamic variable. What is the best approach? Using a Custom Pipe with ngx-translate service or... call from inside the component like this:

//myComponent.ts
ngOnInit() {
        this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
            this.momentLang = event.lang;
        });
    }

// myComponent.html
<p> {{ note.createdAt | amLocale: momentLang  | amTimeAgo  }} </p>

1 Answers1

1

In your interceptor component

import * as moment from 'moment';

const language = localStorage.getItem('language'); (language setted before)
moment.locale(language);
Taazar
  • 1,545
  • 18
  • 27