Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization : feature? #107

Closed
Alx101 opened this issue Oct 26, 2016 · 11 comments
Closed

Localization : feature? #107

Alx101 opened this issue Oct 26, 2016 · 11 comments

Comments

@Alx101
Copy link

Alx101 commented Oct 26, 2016

Description of the Issue and Steps to Reproduce:

Did you search for duplicate issue? [Yes / No]

Please describe the issue and steps to reproduce, preferably with a code sample / plunker:
I guess this is more of a question than an issue, but I haven't found any info on this anywhere. Terribly sorry if this is the wrong place to post. But here goes:

Is there any way to localize the output of moment using this module? I know moment has the .locale() function, but I haven't gotten that to work, and I can't see any functions in this library where I could set the locale either. My goal is to allow for swedish translations as well as english, but I've had no luck. Any help would be very much appreciated.

Thank you

Ensure your issue is isolated to angular2-moment. Issues involving third party tools will be closed unless submitted by the tool's author/maintainer.

Environment:

Please answer the following questions:

  • Angular version? final ^2.0.0
  • TypeScript version? ^2.0.3
  • moment version? ^2.15.2
  • Are you using moment-timezone? no
  • Are you using the angular-cli? Not sure? Probably
  • Using Rollup/Webpack/System.js/Ionic/similar? Ionic 2, with rollup.
@PitPanda1
Copy link

I have the same problem. I have a little workarround

Add this to your component to set the locale:

const moment = require('moment');
moment.locale('de-de');

Not the best solution but it works for me.

@Alx101
Copy link
Author

Alx101 commented Oct 27, 2016

I solved this now by using the moment package directly and setting locale there. I will post a snippet in case anyone else needs it. But it would still be nice to have locales included in this module and functions for switching them.
All credit goes to the user reedrichards over at the ionic forums (github is @peterpeterparker, again thank you) , who posted this excellent solution:

  1. Install momentis
    sudo npm install moment --save
  2. Install typescript definition for moments
    sudo npm install --save-dev @types/moment
  3. Import moment in one of your typescript class like following
    import moment from 'moment';
  4. If you want to define swedish to use as locales for moment (not 100% sure it's 'se' but most probably) <-- it was sv, but close enough ;)
    moment().locale('sv');

4.a Remember to import the locale as described in the P.S
import 'moment/src/locale/sv';

  1. If you want to display a date like '26 oktober 2016 kl. 21:39', do following (you find all pattern at the end of following page http://momentjs.com)
    moment(new Date()).format('LLL');
  2. If you want to add 5 days to today
    moment(new Date()).add(5, 'd').toDate()

P.S.: If the locales doesn't work, you may need to include explicitly the locales you want to use. For me it's the case. It works when I debug but when I test on my real phone it doesn't work (just tested right now). Therefore import it like following (I'm not sure if its needed or not...):

import 'moment/src/locale/sv';

@urish urish closed this as completed Oct 28, 2016
@davorpeic
Copy link

davorpeic commented Jan 5, 2017

For someone trying to use locales in ionic 2 app, I just included lang module as instructed by @Alx101 in app.module.ts after including angular2-moment like this and that automatically pulled spanish language

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

dp

@ramoncarreras
Copy link

Thanks @davorpeic it woks in ionic2 but, how do I set the language dynamically?

@andreas-aeschlimann
Copy link

Thank you @Alx101 for your post. I want to add that this does not seem to work for me:

moment().locale('de');

I ended up having multiple Moment instances (?) and the language was not properly set... This works for me:

moment.locale('de');

I hope this helps some other people.

@urish it would be nice if there were some info about localization in the ReadMe.

@jwhijazi
Copy link

@davorpeic your solution worked for me in angular2 app....
many thanks

@donbenci13
Copy link

donbenci13 commented Jun 27, 2017

I'm also curious how to set the language dynamically?

niether moment().locale('de'); or moment.locale('de'); worked for me but

import 'moment/locale/es'; worked

so now, how do I change back to en if I want to?

@ramoncarreras
Copy link

First, I import all available languages on my application:

import * as moment from 'moment';
import 'moment/locale/en-gb';
import 'moment/locale/es';
import 'moment/locale/fr';
import 'moment/locale/de';
import 'moment/locale/ca';

Then, on my onLanguageChange() method:

moment.locale((this.newLanguage.id == "en") ? 'en-gb' : this.newLanguage.id);

@donbenci13
Copy link

@ramoncarreras awesome, but how would I make this work inside ngFor specific data with angular2-moment?

@ramoncarreras
Copy link

Sorry @donking13, I can't help you with this. :(

@nickshoe
Copy link

nickshoe commented Apr 7, 2020

This worked for me:

import { locale } from "moment";
locale('it');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants