Question

How can moment.js used within a meteor.js app be told to use another language than English? moment.js (installed as an mrt package) works fine with the default English language.

Using the demo calls from the moment.js docs at http://momentjs.com/docs/#/i18n/changing-language/ always produces 'en'. I noticed there is a German language file for moment in meteor_project/packages/moment/lib/moment/lang/de.js that doesn't seem to be used though?

To specify: within a template helper I tried: moment.lang('de'); return moment.lang() //will result to 'en'

and the other options mentioned here: Format a date from inside a Handlebars Template in Meteor

Was it helpful?

Solution 5

If you installed moment as

mrt add moment

Then you already got the languages, but you have to include them manually. Find them at packages\moment\lib\lang. To include them, go to the package.js file and add the language of your choice after all the other files.

api.add_files('lib/moment/lang/de.js', 'client')

modify package.js

And there you go!

proof

OTHER TIPS

You can use rzymek's packages.

These packages use the offical momentjs:moment Meteor package.

Locales:

To add a specific locale user

meteor add rzymek:moment-locale-pl

The complete list of locales can be obtained by

meteor search rzymek:moment-locale-.*

To add all locales use:

meteor add rzymek:moment-locales

As for today (July 2015) I've done this:

Download locales.min.js from Moment.js github repository and put it somewhere inside client folder of your app.

I've choose client/lib/moment/locales.min.js
Then you can set any locale you like (that moment js have) and change it according to user account settings.

moment.locale('en')
// or
moment.locale('ru')

What I don't like is that all languages are combined in one file. Even though it's not too large there are still some work to do (ajax calls or something like that to grab only locale file you need)


By the way. If you don't need all supported languages you can grab locale files.
I believe meteor.js will automatically minify this files when you go to production

Just don't use the Meteorite package. Include Moment.js client-side along with whatever language files you need.

  1. mrt remove moment
  2. Create a folder client/compatibility/moment in your project.
  3. Save http://momentjs.com/downloads/moment-with-locales.min.js into client/compatibility/moment.

That's it. You don't need to add any <script> tags, nothing. Because Moment.js is in the client/compatibility subfolder, any client-side code that's loaded from client or any other subfolder of client will run after Moment.js has been loaded.

Seems that their is still just the momentum.js installed without translations if you do the official "meteor add momentjs:moment" until today. My workaround: Just open moment-with-locales.js and Str-F your desired language (in my case german) and copy and past the:

// moment.js locale configuration
// locale : german (de)
// author : lluchs : 
// author: Menelion Elensúle: 

(function (factory) {
    ...
}`

in a momentum.js file in e.g. client/lib folder. Reload and their you go with moment.locale('de') ... since moment.lang('de') is deprecated .

I did the following for having translation.

You have to install "momentjs:moment".

After that you have to use the following lines for each language you want to add. I put it on my main.js file.

// French translation for moment JS
moment.locale('fr', {
    months : "janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre".split("_"),
    monthsShort : "janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.".split("_"),
    weekdays : "dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi".split("_"),
    weekdaysShort : "dim._lun._mar._mer._jeu._ven._sam.".split("_"),
    weekdaysMin : "Di_Lu_Ma_Me_Je_Ve_Sa".split("_"),
    longDateFormat : {
        LT : "HH:mm",
        LTS : "HH:mm:ss",
        L : "DD/MM/YYYY",
        LL : "D MMMM YYYY",
        LLL : "D MMMM YYYY LT",
        LLLL : "dddd D MMMM YYYY LT"
    },
    calendar : {
        sameDay: "[Aujourd'hui à] LT",
        nextDay: '[Demain à] LT',
        nextWeek: 'dddd [à] LT',
        lastDay: '[Hier à] LT',
        lastWeek: 'dddd [dernier à] LT',
        sameElse: 'L'
    },
    relativeTime : {
        future : "dans %s",
        past : "il y a %s",
        s : "quelques secondes",
        m : "une minute",
        mm : "%d minutes",
        h : "une heure",
        hh : "%d heures",
        d : "un jour",
        dd : "%d jours",
        M : "un mois",
        MM : "%d mois",
        y : "une année",
        yy : "%d années"
    },
    ordinalParse : /\d{1,2}(er|ème)/,
    ordinal : function (number) {
        return number + (number === 1 ? 'er' : 'ème');
    },
    meridiemParse: /PD|MD/,
    isPM: function (input) {
        return input.charAt(0) === 'M';
    },
    // in case the meridiem units are not separated around 12, then implement
    // this function (look at locale/id.js for an example)
    // meridiemHour : function (hour, meridiem) {
    //     return /* 0-23 hour, given meridiem token and hour 1-12 */
    // },
    meridiem : function (hours, minutes, isLower) {
        return hours < 12 ? 'PD' : 'MD';
    },
    week : {
        dow : 1, // Monday is the first day of the week.
        doy : 4  // The week that contains Jan 4th is the first week of the year.
    }
});

And then no more "ReferenceError: module is not defined". For info I use momentjs:moment@2.11.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top