Skip to content

Instantly share code, notes, and snippets.

@jscheid
Created November 13, 2017 08:54
Show Gist options
  • Save jscheid/2ed15199f41b85fc6e75b5cf25f23763 to your computer and use it in GitHub Desktop.
Save jscheid/2ed15199f41b85fc6e75b5cf25f23763 to your computer and use it in GitHub Desktop.
const moment = require('moment');
const CountryLanguage = require('country-language');
const glob = require('glob-promise');
const path = require('path');
const Promise = require('bluebird');
console.log('resolved=', require.resolve('moment'));
const data = {};
const loadLocale = l => {
const locale = path.parse(l).name;
matchData = locale.match(/^([a-z]{2})(?:-([a-z]{2}))?$/);
if (!matchData) {
return Promise.resolve();
}
const lang = matchData[1];
return new Promise((resolve, reject) => {
const withCountry = (country, priority) => {
// console.log('l=', locale);
moment.locale(locale);
const localeData = moment.localeData()
//console.log('localeData.longDateFormat()=', localeData.longDateFormat());
if (!data[country] || data[country].priority > priority) {
data[country] = {
dateFormat: localeData.longDateFormat('L'),
priority,
};
}
};
if (matchData[2]) {
withCountry(matchData[2].toUpperCase(), -1);
resolve();
} else {
CountryLanguage.getLanguageCountries(lang, (err, countries) => {
if (err) {
console.error(err);
} else {
Promise.map(countries, (country, index) => withCountry(country.code_2, index));
}
resolve();
});
}
});
};
glob.promise(path.join(path.dirname(require.resolve('moment')), 'locale/*.js'))
.then(allLocaleFiles => Promise.map(allLocaleFiles, loadLocale))
.then(() => {
const allCountries = Object.keys(data);
allCountries.sort();
console.log('export default {')
allCountries.forEach(country => {
console.log(` ${country}: ${JSON.stringify(data[country].dateFormat)},`);
});
console.log('};');
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment