@d3plus/locales
Version:
International localizations for number, date, and UI labels.
57 lines (56 loc) • 1.94 kB
JavaScript
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
import lcid from "./utils/lookup.js";
import iso from "./utils/iso-codes.js";
var locales = [];
var isoKeys = Object.keys(iso);
Object.keys(lcid).map(function(id) {
var locale = lcid[id];
var isoLanguage = isoKeys.find(function(name) {
return name.toLowerCase() === locale.language.toLowerCase();
});
if (locale.location && isoLanguage) {
var _obj;
locales.push((_obj = {}, _define_property(_obj, "name", locale.language), _define_property(_obj, "location", locale.location), _define_property(_obj, "tag", locale.tag), _define_property(_obj, "lcid", locale.id), _define_property(_obj, "iso639-2", iso[isoLanguage]["iso639-2"]), _define_property(_obj, "iso639-1", iso[isoLanguage]["iso639-1"]), _obj));
}
});
var defaultLocales = {
ar: "ar-SA",
ca: "ca-ES",
da: "da-DK",
en: "en-US",
ko: "ko-KR",
pa: "pa-IN",
pt: "pt-BR",
sv: "sv-SE",
zh: "zh-CN"
};
/**
@module inViewport
@desc Converts a 2-digit language into a full language-LOCATION locale.
@param {String} locale
@private
*/ export default function(locale) {
if (typeof locale !== "string" || locale.length === 5) return locale;
if (defaultLocales[locale]) return defaultLocales[locale];
var list = locales.filter(function(d) {
return d["iso639-1"] === locale;
});
if (!list.length) return locale;
else if (list.length === 1) return list[0].tag;
else if (list.find(function(d) {
return d.tag === "".concat(locale, "-").concat(locale.toUpperCase());
})) return "".concat(locale, "-").concat(locale.toUpperCase());
else return list[0].tag;
}