const fetch = require('node-fetch')
const moment = require('moment/min/moment-with-locales')
exports.endpoint = async (req, res) => {
const momentLocalesCall = await fetch('https://api.github.com/repositories/1424470/contents/src/locale')
const momentLocalesJson = await momentLocalesCall.json()
const momentLocalesData = momentLocalesJson.map(l => l.name.split('.')[0])
const xummLocalesCall = await fetch('https://translate.xumm.dev/json/export-meta')
const xummLocalesJson = await xummLocalesCall.json()
if (res !== null) {
res.setHeader('Content-Type', 'application/json; Charset=UTF-8')
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', '*')
}
const xummLocaleToMomentJsLocale = Object.keys(xummLocalesJson.languages)
.reduce((a, b) => {
let resolvedLocale = 'en'
if (momentLocalesData.indexOf(b.toLowerCase()) > -1) {
resolvedLocale = b.toLowerCase()
} else if (momentLocalesData.indexOf(b.split('-')[0].toLowerCase()) > -1) {
resolvedLocale = b.split('-')[0].toLowerCase()
} else {
// Check alias
const aliasses = Object.keys(xummLocalesJson['language-code-alias']).filter(k => {
return xummLocalesJson['language-code-alias'][k].toLowerCase() === b.toLowerCase()
})
if (aliasses.length > 0) {
aliasses.forEach(a => {
const alias = a.toLowerCase().replace('_', '-')
if (momentLocalesData.indexOf(alias) > -1) {
resolvedLocale = alias
}
})
}
}
moment.locale(resolvedLocale)
const localeData = moment.localeData()
Object.assign(a, {
[b]: {
momentJsCode: resolvedLocale,
momentJsSource: Object.keys(localeData).reduce((c, d) => {
if (
['RegExp', 'Function'].indexOf(localeData[d].constructor.name) < 0
&& !d.match(/(Parse|Regex)$/)
&& d !== '_config'
) {
Object.assign(c, {
[d.replace(/^_/, '')]: localeData[d]
})
}
return c
}, {})
}
})
return a
}, {})
if (res !== null) {
return res.end(JSON.stringify(xummLocaleToMomentJsLocale))
} else {
// return console.log(xummLocaleToMomentJsLocale.nl.momentJsSource)
return console.log(xummLocaleToMomentJsLocale)
}
}
exports.endpoint(null, null)