-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
932 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import coreLikelySubtags from "../core/likely_subtags"; | ||
import coreRemoveLikelySubtags from "../core/remove_likely_subtags"; | ||
import coreSubtags from "../core/subtags"; | ||
/** | ||
* bundleLookup( minLanguageId ) | ||
* | ||
* @Cldr [Cldr class] | ||
* | ||
* @cldr [Cldr instance] | ||
* | ||
* @minLanguageId [String] requested languageId after applied remove likely subtags. | ||
*/ | ||
|
||
export default function (Cldr, cldr, minLanguageId) { | ||
var availableBundleMap = Cldr._availableBundleMap, | ||
availableBundleMapQueue = Cldr._availableBundleMapQueue; | ||
|
||
if (availableBundleMapQueue.length) { | ||
while (availableBundleMapQueue.length > 0) { | ||
var bundle = availableBundleMapQueue.shift(); | ||
|
||
if (!bundle) { | ||
break; | ||
} | ||
|
||
var existing, maxBundle, minBundle, subtags; | ||
subtags = coreSubtags(bundle); | ||
maxBundle = coreLikelySubtags(Cldr, cldr, subtags); | ||
|
||
if (typeof maxBundle === "undefined") { | ||
throw new Error("Could not find likelySubtags for ".concat(bundle)); | ||
} | ||
|
||
minBundle = coreRemoveLikelySubtags(Cldr, cldr, maxBundle); | ||
minBundle = minBundle.join(Cldr.localeSep); | ||
existing = availableBundleMap[minBundle]; | ||
|
||
if (existing && existing.length < bundle.length) { | ||
return; | ||
} | ||
|
||
availableBundleMap[minBundle] = bundle; | ||
} | ||
} | ||
|
||
return availableBundleMap[minLanguageId] || null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import resourceGet from "../resource/get"; | ||
import pathNormalize from "../path/normalize"; | ||
export default function (Cldr, locale) { | ||
var normalizedPath, parent; | ||
|
||
if (locale === "root") { | ||
return; | ||
} // First, try to find parent on supplemental data. | ||
|
||
|
||
normalizedPath = pathNormalize(["supplemental/parentLocales/parentLocale", locale]); | ||
parent = resourceGet(Cldr._resolved, normalizedPath) || resourceGet(Cldr._raw, normalizedPath); | ||
|
||
if (parent) { | ||
return parent; | ||
} // Or truncate locale. | ||
|
||
|
||
parent = locale.substr(0, locale.lastIndexOf(Cldr.localeSep)); | ||
|
||
if (!parent) { | ||
return "root"; | ||
} | ||
|
||
return parent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import arrayForEach from "../util/array/for_each"; | ||
import objectKeys from "../util/object/keys"; | ||
export default function (code, attributes) { | ||
var error, message; | ||
message = code + (attributes && JSON ? ": " + JSON.stringify(attributes) : ""); | ||
error = new Error(message); | ||
error.code = code; // extend( error, attributes ); | ||
|
||
arrayForEach(objectKeys(attributes), function (attribute) { | ||
error[attribute] = attributes[attribute]; | ||
}); | ||
return error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import createError from "./create_error"; | ||
export default function (code, check, attributes) { | ||
if (!check) { | ||
throw createError(code, attributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import validate from "../validate"; | ||
export default function (value, name) { | ||
process.env.NODE_ENV !== "production" ? validate("E_MISSING_PARAMETER", typeof value !== "undefined", { | ||
name: name | ||
}) : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import validate from "../validate"; | ||
export default function (value, name, check, expected) { | ||
process.env.NODE_ENV !== "production" ? validate("E_INVALID_PAR_TYPE", check, { | ||
expected: expected, | ||
name: name, | ||
value: value | ||
}) : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import validateType from "../type"; | ||
export default function (value, name) { | ||
process.env.NODE_ENV !== "production" ? validateType(value, name, typeof value === "undefined" || typeof value === "function", "Function") : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import validateType from "../type"; | ||
import arrayIsArray from "../../../util/array/is_array"; | ||
export default function (value, name) { | ||
process.env.NODE_ENV !== "production" ? validateType(value, name, typeof value === "string" || arrayIsArray(value), "String or Array") : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import validateType from "../type"; | ||
import isPlainObject from "../../../util/is_plain_object"; | ||
export default function (value, name) { | ||
process.env.NODE_ENV !== "production" ? validateType(value, name, typeof value === "undefined" || isPlainObject(value), "Plain Object") : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import validateType from "../type"; | ||
export default function (value, name) { | ||
process.env.NODE_ENV !== "production" ? validateType(value, name, typeof value === "string", "a string") : void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import bundleLookup from "./bundle/lookup"; | ||
import createError from "./common/create_error"; | ||
import validate from "./common/validate"; | ||
import validatePresence from "./common/validate/presence"; | ||
import validateType from "./common/validate/type"; | ||
import validateTypePath from "./common/validate/type/path"; | ||
import validateTypePlainObject from "./common/validate/type/plain_object"; | ||
import validateTypeString from "./common/validate/type/string"; | ||
import coreLikelySubtags from "./core/likely_subtags"; | ||
import coreLoad from "./core/load"; | ||
import coreRemoveLikelySubtags from "./core/remove_likely_subtags"; | ||
import coreSubtags from "./core/subtags"; | ||
import itemGetResolved from "./item/get_resolved"; | ||
import pathNormalize from "./path/normalize"; | ||
import resourceGet from "./resource/get"; | ||
import alwaysArray from "./util/always_array"; | ||
import jsonMerge from "./util/json/merge"; | ||
/** | ||
* new Cldr() | ||
*/ | ||
|
||
var Cldr = function Cldr(locale) { | ||
this.init(locale); | ||
}; // Build optimization hack to avoid duplicating functions across modules. | ||
|
||
|
||
Cldr._alwaysArray = alwaysArray; | ||
Cldr._coreLoad = coreLoad; | ||
Cldr._createError = createError; | ||
Cldr._itemGetResolved = itemGetResolved; | ||
Cldr._jsonMerge = jsonMerge; | ||
Cldr._pathNormalize = pathNormalize; | ||
Cldr._resourceGet = resourceGet; | ||
Cldr._validatePresence = validatePresence; | ||
Cldr._validateType = validateType; | ||
Cldr._validateTypePath = validateTypePath; | ||
Cldr._validateTypePlainObject = validateTypePlainObject; | ||
Cldr._availableBundleMap = {}; | ||
Cldr._availableBundleMapQueue = []; | ||
Cldr._resolved = {}; // Allow user to override locale separator "-" (default) | "_". According to http://www.unicode.org/reports/tr35/#Unicode_language_identifier, both "-" and "_" are valid locale separators (eg. "en_GB", "en-GB"). According to http://unicode.org/cldr/trac/ticket/6786 its usage must be consistent throughout the data set. | ||
|
||
Cldr.localeSep = "-"; | ||
/** | ||
* Cldr.load( json [, json, ...] ) | ||
* | ||
* @json [JSON] CLDR data or [Array] Array of @json's. | ||
* | ||
* Load resolved cldr data. | ||
*/ | ||
|
||
Cldr.load = function () { | ||
Cldr._resolved = coreLoad(Cldr, Cldr._resolved, arguments); | ||
}; | ||
/** | ||
* .init() automatically run on instantiation/construction. | ||
*/ | ||
|
||
|
||
Cldr.prototype.init = function (locale) { | ||
var attributes, | ||
language, | ||
maxLanguageId, | ||
minLanguageId, | ||
script, | ||
subtags, | ||
territory, | ||
unicodeLocaleExtensions, | ||
variant, | ||
sep = Cldr.localeSep, | ||
unicodeLocaleExtensionsRaw = ""; | ||
process.env.NODE_ENV !== "production" ? validatePresence(locale, "locale") : void 0; | ||
process.env.NODE_ENV !== "production" ? validateTypeString(locale, "locale") : void 0; | ||
subtags = coreSubtags(locale); | ||
|
||
if (subtags.length === 5) { | ||
unicodeLocaleExtensions = subtags.pop(); | ||
unicodeLocaleExtensionsRaw = sep + "u" + sep + unicodeLocaleExtensions; // Remove trailing null when there is unicodeLocaleExtensions but no variants. | ||
|
||
if (!subtags[3]) { | ||
subtags.pop(); | ||
} | ||
} | ||
|
||
variant = subtags[3]; // Normalize locale code. | ||
// Get (or deduce) the "triple subtags": language, territory (also aliased as region), and script subtags. | ||
// Get the variant subtags (calendar, collation, currency, etc). | ||
// refs: | ||
// - http://www.unicode.org/reports/tr35/#Field_Definitions | ||
// - http://www.unicode.org/reports/tr35/#Language_and_Locale_IDs | ||
// - http://www.unicode.org/reports/tr35/#Unicode_locale_identifier | ||
// When a locale id does not specify a language, or territory (region), or script, they are obtained by Likely Subtags. | ||
|
||
maxLanguageId = coreLikelySubtags(Cldr, this, subtags, { | ||
force: true | ||
}) || subtags; | ||
language = maxLanguageId[0]; | ||
script = maxLanguageId[1]; | ||
territory = maxLanguageId[2]; | ||
minLanguageId = coreRemoveLikelySubtags(Cldr, this, maxLanguageId).join(sep); // Set attributes | ||
|
||
this.attributes = attributes = { | ||
bundle: bundleLookup(Cldr, this, minLanguageId), | ||
// Unicode Language Id | ||
minLanguageId: minLanguageId + unicodeLocaleExtensionsRaw, | ||
maxLanguageId: maxLanguageId.join(sep) + unicodeLocaleExtensionsRaw, | ||
// Unicode Language Id Subtabs | ||
language: language, | ||
script: script, | ||
territory: territory, | ||
region: territory | ||
/* alias */ | ||
, | ||
variant: variant | ||
}; // Unicode locale extensions. | ||
|
||
unicodeLocaleExtensions && ("-" + unicodeLocaleExtensions).replace(/-[a-z]{3,8}|(-[a-z]{2})-([a-z]{3,8})/g, function (attribute, key, type) { | ||
if (key) { | ||
// Extension is in the `keyword` form. | ||
attributes["u" + key] = type; | ||
} else { | ||
// Extension is in the `attribute` form. | ||
attributes["u" + attribute] = true; | ||
} | ||
}); | ||
this.locale = locale; | ||
}; | ||
/** | ||
* .get() | ||
*/ | ||
|
||
|
||
Cldr.prototype.get = function (path) { | ||
process.env.NODE_ENV !== "production" ? validatePresence(path, "path") : void 0; | ||
process.env.NODE_ENV !== "production" ? validateTypePath(path, "path") : void 0; | ||
return itemGetResolved(Cldr, path, this.attributes); | ||
}; | ||
/** | ||
* .main() | ||
*/ | ||
|
||
|
||
Cldr.prototype.main = function (path) { | ||
process.env.NODE_ENV !== "production" ? validatePresence(path, "path") : void 0; | ||
process.env.NODE_ENV !== "production" ? validateTypePath(path, "path") : void 0; | ||
process.env.NODE_ENV !== "production" ? validate("E_MISSING_BUNDLE", this.attributes.bundle !== null, { | ||
locale: this.locale | ||
}) : void 0; | ||
path = alwaysArray(path); | ||
return this.get(["main/{bundle}"].concat(path)); | ||
}; | ||
|
||
export default Cldr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import arraySome from "../util/array/some"; | ||
/** | ||
* Return the maximized language id as defined in | ||
* http://www.unicode.org/reports/tr35/#Likely_Subtags | ||
* 1. Canonicalize. | ||
* 1.1 Make sure the input locale is in canonical form: uses the right | ||
* separator, and has the right casing. | ||
* TODO Right casing? What df? It seems languages are lowercase, scripts are | ||
* Capitalized, territory is uppercase. I am leaving this as an exercise to | ||
* the user. | ||
* | ||
* 1.2 Replace any deprecated subtags with their canonical values using the | ||
* <alias> data in supplemental metadata. Use the first value in the | ||
* replacement list, if it exists. Language tag replacements may have multiple | ||
* parts, such as "sh" ➞ "sr_Latn" or mo" ➞ "ro_MD". In such a case, the | ||
* original script and/or region are retained if there is one. Thus | ||
* "sh_Arab_AQ" ➞ "sr_Arab_AQ", not "sr_Latn_AQ". | ||
* TODO What <alias> data? | ||
* | ||
* 1.3 If the tag is grandfathered (see <variable id="$grandfathered" | ||
* type="choice"> in the supplemental data), then return it. | ||
* TODO grandfathered? | ||
* | ||
* 1.4 Remove the script code 'Zzzz' and the region code 'ZZ' if they occur. | ||
* 1.5 Get the components of the cleaned-up source tag (languages, scripts, | ||
* and regions), plus any variants and extensions. | ||
* 2. Lookup. Lookup each of the following in order, and stop on the first | ||
* match: | ||
* 2.1 languages_scripts_regions | ||
* 2.2 languages_regions | ||
* 2.3 languages_scripts | ||
* 2.4 languages | ||
* 2.5 und_scripts | ||
* 3. Return | ||
* 3.1 If there is no match, either return an error value, or the match for | ||
* "und" (in APIs where a valid language tag is required). | ||
* 3.2 Otherwise there is a match = languagem_scriptm_regionm | ||
* 3.3 Let xr = xs if xs is not empty, and xm otherwise. | ||
* 3.4 Return the language tag composed of languager _ scriptr _ regionr + | ||
* variants + extensions. | ||
* | ||
* @subtags [Array] normalized language id subtags tuple (see init.js). | ||
*/ | ||
|
||
export default function (Cldr, cldr, subtags, options) { | ||
var match, | ||
matchFound, | ||
language = subtags[0], | ||
script = subtags[1], | ||
sep = Cldr.localeSep, | ||
territory = subtags[2], | ||
variants = subtags.slice(3, 4); | ||
options = options || {}; // Skip if (language, script, territory) is not empty [3.3] | ||
|
||
if (language !== "und" && script !== "Zzzz" && territory !== "ZZ") { | ||
return [language, script, territory].concat(variants); | ||
} // Skip if no supplemental likelySubtags data is present | ||
|
||
|
||
if (typeof cldr.get("supplemental/likelySubtags") === "undefined") { | ||
return; | ||
} // [2] | ||
|
||
|
||
matchFound = arraySome([[language, script, territory], [language, territory], [language, script], [language], ["und", script]], function (test) { | ||
return match = !/\b(Zzzz|ZZ)\b/.test(test.join(sep)) | ||
/* [1.4] */ | ||
&& cldr.get(["supplemental/likelySubtags", test.join(sep)]); | ||
}); // [3] | ||
|
||
if (matchFound) { | ||
// [3.2 .. 3.4] | ||
match = match.split(sep); | ||
return [language !== "und" ? language : match[0], script !== "Zzzz" ? script : match[1], territory !== "ZZ" ? territory : match[2]].concat(variants); | ||
} else if (options.force) { | ||
// [3.1.2] | ||
return cldr.get("supplemental/likelySubtags/und").split(sep); | ||
} else { | ||
// [3.1.1] | ||
return; | ||
} | ||
} |
Oops, something went wrong.