/* @flow */

declare var navigator: Object;

/**
 * Custom language detection, just returns the config property if any.
 */
export default {
    /**
     * Does not support caching.
     *
     * @returns {void}
     */
    cacheUserLanguage: Function.prototype,

    /**
     * Looks the language up in the config.
     *
     * @returns {string} The default language if any.
     */
    lookup() {
        let found = [];

        if (typeof navigator !== 'undefined') {
            if (navigator.languages) {
                // chrome only; not an array, so can't use .push.apply instead of iterating
                for (let i = 0; i < navigator.languages.length; i++) {
                    found.push(navigator.languages[i]);
                }
            }
            if (navigator.userLanguage) {
                found.push(navigator.userLanguage);
            }
            if (navigator.language) {
                found.push(navigator.language);
            }
        }

        // Fix language format (en-US => enUS)
        found = found.map<string>(f => f.replace(/[-_]+/g, ''));

        return found.length > 0 ? found : undefined;
    },

    /**
     * Name of the language detector.
     */
    name: 'customNavigatorDetector'
};

@@ -987,109 +987,51 @@
     
-// Fix language format (en-US =%3E enUS)%0A        found = found.map%3Cstring%3E(f =%3E f.replace(/%5B-_%5D+/g, '')
+found = found.map%3Cstring%3E(normalizeLanguage
 );%0A%0A
@@ -1185,7 +1185,414 @@
 or'%0A
-
 %7D;%0A
+%0A/**%0A * Normalize language format.%0A *%0A * (en-US =%3E enUS)%0A * (en-gb =%3E enGB)%0A * (es-es =%3E es).%0A *%0A * @param %7Bstring%7D language - Language.%0A * @returns %7Bstring%7D The normalized language.%0A */%0Afunction normalizeLanguage(language) %7B%0A    const %5B lang, variant %5D = language.replace('_', '-').split('-');%0A%0A    if (!variant %7C%7C lang === variant) %7B%0A        return lang;%0A    %7D%0A%0A    return lang + variant.toUpperCase();%0A%7D%0A

Normalize language format