UNPKG

1.79 kBJavaScriptView Raw
1function _defineProperty(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; }
2
3import lcid from "windows-locale";
4import iso from "iso639-codes";
5var locales = [];
6var isoKeys = Object.keys(iso);
7Object.keys(lcid).map(function (id) {
8 var locale = lcid[id];
9 var isoLanguage = isoKeys.find(function (name) {
10 return name.toLowerCase() === locale.language.toLowerCase();
11 });
12
13 if (locale.location && isoLanguage) {
14 var _locales$push;
15
16 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));
17 }
18});
19var defaultLocales = {
20 ar: "ar-SA",
21 ca: "ca-ES",
22 da: "da-DK",
23 en: "en-US",
24 ko: "ko-KR",
25 pa: "pa-IN",
26 pt: "pt-BR",
27 sv: "sv-SE"
28};
29/**
30 * Converts a 2-digit language into a full language-LOCATION locale.
31 * @param {String} locale
32 */
33
34export default function (locale) {
35 if (typeof locale !== "string" || locale.length === 5) return locale;
36 if (defaultLocales[locale]) return defaultLocales[locale];
37 var list = locales.filter(function (d) {
38 return d["iso639-1"] === locale;
39 });
40 if (!list.length) return locale;else if (list.length === 1) return list[0].tag;else if (list.find(function (d) {
41 return d.tag === "".concat(locale, "-").concat(locale.toUpperCase());
42 })) return "".concat(locale, "-").concat(locale.toUpperCase());else return list[0].tag;
43}
\No newline at end of file