Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Fix for Safari v3 - locale.js #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/intl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,104 @@ export function getDirection(code) {
return ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(tag) >= 0 ?
'rtl' : 'ltr';
}

export function CanonicalizeLocaleList(locales) {
if (locales === undefined) {
return [];
}
let seen = [];
if (typeof locales === 'string') {
locales = [locales];
}
let O = locales;
let len = O.length;
let k = 0;
while (k < len) {
let tag = O[k];
tag = tag.toLowerCase();
if (seen.indexOf(tag) === -1) {
seen.push(tag);
}
k++;
}
return seen;
}

export function PrioritizeLocales(availableLocales,
requestedLocales,
defaultLocale) {

let array = new Array();
if (typeof availableLocales === 'object') {
let iter = availableLocales.values();
for (let z = 0; z < availableLocales.size; z++) {
array.push(iter.next().value);
}
} else {
array = availableLocales.slice();
}

availableLocales = CanonicalizeLocaleList(array);
requestedLocales = CanonicalizeLocaleList(requestedLocales);

let result = LookupAvailableLocales(availableLocales, requestedLocales);
if (defaultLocale) {
// if default locale is not present in result,
// add it to the end of fallback chain
defaultLocale = defaultLocale.toLowerCase();
if (result.indexOf(defaultLocale) === -1) {
result.push(defaultLocale);
}
}

for(let i = 0; i < result.length; i++) {
array = result[i].split('-');
if (array.length === 2) {
result[i] = `${array[0]} - ${array[1].toUpperCase()}`;
}
}

return result;
}

export function LookupAvailableLocales(availableLocales, requestedLocales) {
// Steps 1-2.
let len = requestedLocales.length;
let subset = [];

// Steps 3-4.
let k = 0;
while (k < len) {
// Steps 4.a-b.
let locale = requestedLocales[k];

// Step 4.c-d.
let availableLocale = BestAvailableLocale(availableLocales, locale);
if (availableLocale !== undefined) {
// in LookupSupportedLocales it pushes locale here
subset.push(availableLocale);
}
// Step 4.e.
k++;
}

// Steps 5-6.
return subset.slice(0);
}

export function BestAvailableLocale(availableLocales, locale) {
let candidate = locale;
while (true) {
if (availableLocales.indexOf(candidate) !== -1) {
return candidate;
}
let pos = candidate.lastIndexOf('-');
if (pos === -1) {
return undefined;
}
if (pos >= 2 && candidate[pos - 2] === '-') {
pos -= 2;
}
candidate = candidate.substring(0, pos);
}
}