UNPKG

d3plus-common

Version:

Common functions and methods used across D3plus modules.

44 lines (43 loc) 2.69 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import lcid from "./locales/lookup.js"; import iso from "./locales/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 _locales$push; locales.push((_locales$push = {}, _defineProperty(_locales$push, "name", locale.language), _defineProperty(_locales$push, "location", locale.location), _defineProperty(_locales$push, "tag", locale.tag), _defineProperty(_locales$push, "lcid", locale.id), _defineProperty(_locales$push, "iso639-2", iso[isoLanguage]["iso639-2"]), _defineProperty(_locales$push, "iso639-1", iso[isoLanguage]["iso639-1"]), _locales$push)); } }); 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" }; /** * Converts a 2-digit language into a full language-LOCATION locale. * @param {String} locale */ 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; }