use of com.github.anba.es6draft.runtime.objects.intl.LanguageTagParser.LanguageTag in project es6draft by anba.
the class IntlAbstractOperations method ResolveLocale.
/**
* 9.2.6 ResolveLocale (availableLocales, requestedLocales, options, relevantExtensionKeys, localeData)
*
* @param realm
* the realm instance
* @param availableLocales
* the set of available locales
* @param requestedLocales
* the set of requested locales
* @param options
* the options record
* @param relevantExtensionKeys
* the list of relevant extension keys
* @param localeData
* the locale data
* @return the resolved locale
*/
public static ResolvedLocale ResolveLocale(Realm realm, Lazy<Set<String>> availableLocales, Set<String> requestedLocales, OptionsRecord options, List<ExtensionKey> relevantExtensionKeys, LocaleData localeData) {
/* steps 1-4 */
OptionsRecord.MatcherType matcher = options.getLocaleMatcher();
LocaleMatch r;
if (matcher == OptionsRecord.MatcherType.Lookup) {
r = LookupMatcher(realm, availableLocales, requestedLocales);
} else {
r = BestFitMatcher(realm, availableLocales, requestedLocales);
}
// fast path for steps 5-16
String extension = r.getExtension();
if (extension.isEmpty() && options.size() == 0) {
return ResolveLocaleNoExtension(r, relevantExtensionKeys, localeData);
}
/* step 5 */
String foundLocale = r.getLocale();
LocaleDataInfo foundLocaleData = localeData.info(ULocale.forLanguageTag(foundLocale));
/* step 6 */
EnumMap<ExtensionKey, String> extensionSubtags = null;
if (!extension.isEmpty()) {
extensionSubtags = UnicodeExtensionSubtags(extension);
}
/* steps 7-8 (not applicable) */
/* step 9 */
StringBuilder supportedExtension = new StringBuilder("-u");
/* steps 10-13 */
EnumMap<ExtensionKey, String> values = new EnumMap<>(ExtensionKey.class);
for (ExtensionKey key : relevantExtensionKeys) {
/* steps 13.a-b (not applicable) */
/* step 13.c */
List<String> keyLocaleData = foundLocaleData.entries(key);
/* step 13.d */
String value = keyLocaleData.get(0);
/* step 13.e */
String supportedExtensionAddition = "";
/* steps 13.f, 13.f.i-ii */
if (extensionSubtags != null && extensionSubtags.containsKey(key)) {
/* steps 13.f.ii.1-2 */
if (!extensionSubtags.get(key).isEmpty()) {
/* step 13.f.ii.1 */
String requestedValue = extensionSubtags.get(key);
if (keyLocaleData.contains(requestedValue)) {
value = requestedValue;
supportedExtensionAddition = "-" + key.name() + "-" + value;
}
} else if (keyLocaleData.contains("true")) {
/* step 13.f.ii.2 */
value = "true";
}
}
/* step 13.g */
if (options.contains(key)) {
/* step 13.g.i */
String optionsValue = options.get(key);
/* step 13.g.ii */
if (keyLocaleData.contains(optionsValue) && !optionsValue.equals(value)) {
value = optionsValue;
supportedExtensionAddition = "";
}
}
/* step 13.h */
values.put(key, value);
/* step 13.i */
supportedExtension.append(supportedExtensionAddition);
/* step 13.j (not applicable) */
}
/* step 14 */
if (supportedExtension.length() > 2) {
// FIXME: spec bug - https://bugs.ecmascript.org/show_bug.cgi?id=1456
/* step 14.a */
int privateIndex = foundLocale.indexOf("-x-");
/* steps 14.b-c */
if (privateIndex == -1) {
/* step 14.b */
foundLocale += supportedExtension;
} else {
/* step 14.c */
/* step 14.c.i */
String preExtension = foundLocale.substring(0, privateIndex);
/* step 14.c.ii */
String postExtension = foundLocale.substring(privateIndex);
/* step 14.c.iii */
foundLocale = preExtension + supportedExtension + postExtension;
}
/* step 14.d */
LanguageTag languageTag = IsStructurallyValidLanguageTag(foundLocale);
assert languageTag != null : foundLocale;
/* step 14.e */
foundLocale = CanonicalizeLanguageTag(languageTag);
}
/* steps 15-16 */
return new ResolvedLocale(r.getLocale(), foundLocale, values);
}
use of com.github.anba.es6draft.runtime.objects.intl.LanguageTagParser.LanguageTag in project es6draft by anba.
the class IntlAbstractOperations method sanitizeLanguageTag.
private static String sanitizeLanguageTag(String languageTag) {
LanguageTag tag = IsStructurallyValidLanguageTag(languageTag);
if (tag == null) {
return DEFAULT_LOCALE;
}
String locale = RemoveUnicodeLocaleExtension(tag.canonicalize());
locale = BestLocale(GetAvailableLocales(LanguageData.getAvailableCollatorLocales()), locale);
if (locale == null) {
return DEFAULT_LOCALE;
}
locale = BestLocale(GetAvailableLocales(LanguageData.getAvailableDateFormatLocales()), locale);
if (locale == null) {
return DEFAULT_LOCALE;
}
locale = BestLocale(GetAvailableLocales(LanguageData.getAvailableNumberFormatLocales()), locale);
if (locale == null) {
return DEFAULT_LOCALE;
}
return locale;
}
use of com.github.anba.es6draft.runtime.objects.intl.LanguageTagParser.LanguageTag in project es6draft by anba.
the class IntlAbstractOperations method CanonicalizeLocaleList.
/**
* 9.2.1 CanonicalizeLocaleList (locales)
*
* @param cx
* the execution context
* @param locales
* the locales array
* @return the set of canonicalized locales
*/
public static Set<String> CanonicalizeLocaleList(ExecutionContext cx, Object locales) {
/* step 1 */
if (Type.isUndefined(locales)) {
return emptySet();
}
/* steps 2-8 (string only) */
if (Type.isString(locales)) {
// handle the string-only case directly
String tag = ToFlatString(cx, locales);
LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
if (langTag == null) {
throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
}
tag = CanonicalizeLanguageTag(langTag);
return singleton(tag);
}
/* step 2 */
LinkedHashSet<String> seen = new LinkedHashSet<>();
/* step 3 (not applicable) */
/* step 4 */
ScriptObject o = ToObject(cx, locales);
/* step 5 */
long len = ToLength(cx, Get(cx, o, "length"));
/* steps 6-7 */
for (long k = 0; k < len; ++k) {
/* step 7.a */
long pk = k;
/* step 7.b */
boolean kPresent = HasProperty(cx, o, pk);
/* step 7.c */
if (kPresent) {
/* step 7.c.i */
Object kValue = Get(cx, o, pk);
/* step 7.c.ii */
if (!(Type.isString(kValue) || Type.isObject(kValue))) {
throw newTypeError(cx, Messages.Key.IntlInvalidLanguageTagType, Type.of(kValue).toString());
}
/* step 7.c.iii */
String tag = ToFlatString(cx, kValue);
/* step 7.c.iv */
LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
if (langTag == null) {
throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
}
/* step 7.c.v */
tag = CanonicalizeLanguageTag(langTag);
/* step 7.c.vi */
seen.add(tag);
}
}
/* step 8 */
return seen;
}
Aggregations