I am currently developing an application using Typescript that requires features from both Moment.js and moment-timezone. To localize the date and timestamps within the application, I have set moment's locale in the main app.ts
file to match the language of the device.
UPDATE: For a detailed look at the sample files with additional comments, check out this gist https://gist.github.com/spstratis/fa853f9750a095d4acd0d1196a285be9
app.ts
import * as moment from 'moment/min/moment-with-locales';
let language = appUtil.getPhoneLanguage();
moment.locale(language);
// Check if the expected locale is printed
console.log("Moment Locale = " + moment.locale());
The problem arises in the utilities module when importing moment-timezone; it defaults to the 'en' locale despite setting moment's locale globally in the main app.ts
file.
Below are two utility methods where I'm looking to localize relative date strings and months, even when moment-timezone defaults them to 'en'.
I attempted to use .locale(locale) on the moment methods but saw no change. Switching to import moment instead of moment-timezone worked for some methods but failed for those requiring timezone utilities.
date-util.ts
import * as moment from 'moment-timezone';
export function dateRelativeTime(value): string {
let timezoneId = appCache.getTimezoneId();
let localTime = _getLocalUtcDateString(value, timezoneId);
let dateMoment = moment(localTime, "MM/DD/YYYY hh:mm:ss A");
let formatedDate = dateMoment.tz(timezoneId).fromNow();
return formatedDate;
};
export function localizedMonths(): ValueList {
let m = moment("2016");
let months = new ValueList([]);
for (var i = 0; i < 12; i++) {
months.push({ ValueMember: [i + 1], DisplayMember: m.month(i).format('MMMM') });
}
return months;
};