Moment.js Languages

You must Login before you can answer or comment on any questions.

HI.

I have an app that will be in 2 languages.

I have made the i18n folder and i got all the strings to work for the internationalization and so on.

My problem is with Moment.js

I have moment.js in my common folder. In my common folder i have a 'lang' folder and inside that i have my sv.js file.

I call it like this

var moment = require('common/moment');
 
moment.lang(Ti.Locale.getString('date_lang'));
 
var date = moment(eventToDisplay.start_date).format('MMMM Do ');
var time = moment(eventToDisplay.start_date).format('h:mm a');
And in my i18n folder i have a 'sv' folder with
// moment.js language configuration
// language : swedish (sv)
// author : Jens Alm : https://github.com/ulmus
(function () {
    var lang = {
            months : "januari_februari_mars_april_maj_juni_juli_augusti_september_oktober_november_december".split("_"),
            monthsShort : "jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec".split("_"),
            weekdays : "söndag_måndag_tisdag_onsdag_torsdag_fredag_lördag".split("_"),
            weekdaysShort : "sön_mån_tis_ons_tor_fre_lör".split("_"),
            weekdaysMin : "sö_må_ti_on_to_fr_lö".split("_"),
            longDateFormat : {
                LT : "HH:mm",
                L : "YYYY-MM-DD",
                LL : "D MMMM YYYY",
                LLL : "D MMMM YYYY LT",
                LLLL : "dddd D MMMM YYYY LT"
            },
            calendar : {
                sameDay: '[Idag klockan] LT',
                nextDay: '[Imorgon klockan] LT',
                lastDay: '[Igår klockan] LT',
                nextWeek: 'dddd [klockan] LT',
                lastWeek: '[Förra] dddd[en klockan] LT',
                sameElse: 'L'
            },
            relativeTime : {
                future : "om %s",
                past : "för %s sen",
                s : "några sekunder",
                m : "en minut",
                mm : "%d minuter",
                h : "en timme",
                hh : "%d timmar",
                d : "en dag",
                dd : "%d dagar",
                M : "en månad",
                MM : "%d månader",
                y : "ett år",
                yy : "%d år"
            },
            ordinal : function (number) {
                var b = number % 10;
                return (~~ (number % 100 / 10) === 1) ? 'e' :
                    (b === 1) ? 'a' :
                    (b === 2) ? 'a' :
                    (b === 3) ? 'e' : 'e';
            }
        };
 
    // Node
    if (typeof module !== 'undefined' && module.exports) {
        module.exports = lang;
    }
    // Browser
    if (typeof window !== 'undefined' && this.moment && this.moment.lang) {
        this.moment.lang('sv', lang);
    }
}());
The string in my 'sv' strings.xml is
<?xml version="1.0" encoding="UTF-8"?>
<resources>
    ......Lots of strings........
    <string name="date_lang">sv</string>
</resources>
I guess i call the
moment.lang(Ti.Locale.getString('date_lang'));
wrong because i get
[WARN] Exception in event callback. Couldn't find module: ./lang/sv
when i run it and then it stops.

So i guess that i wont need a en.js for the english because moment.js is en by default. Right?

But how can i get it to work with the right language?

Is the folder structure wrong?

Pls help

Dates are hard, both in JS as in real life.... :)

— asked 8 months ago by Richard Harrysson
6 Comments
  • yeah,

    moment.lang(Ti.Locale.getString('date_lang')); 
    // ^ will return moment.lang('sv')); in which will call your moment.js:-
    //  (function () {
    //    var sv = {}
    //   });
    // which currently you don't have

    — commented 8 months ago by kim huat

  • Ok.

    So where will i put that?

    Thanx

    — commented 8 months ago by Richard Harrysson

  • im not sure what exactly you need actually..but here some example for you :-

    //sv.js
    var moment = require('test/moment').moment();
     
    var getLang = Ti.Locale.getString('date_lang'); // will return 'sv'
    // assume language that you have decided = sv
     
    if(getLang === 'sv')
    alert(moment.lang.hi);
     
     
    //moment.js
    exports.moment = function() {
        return {
                lang: { 
                hi:'hiiii'
                },  
        };
    };

    — commented 8 months ago by kim huat

  • Show 3 more comments

1 Answer

Your Answer

Think you can help? Login to answer this question!