Repository: syncthing/syncthing
File: gui/default/syncthing/core/localeService.js (JavaScript)

Code before:
                        var i,
                            // We need to reverse the order, so that "zh" matches with "zh-CN"
                            // and not zh-TW". This is because if no exact matches are found,
                            // the last match wins. The order needs to be reversed here and not
                            // later in the matching loop, as the browser may request multiple
                            // languages, and all of them should match in the reversed order.
                            guiLangs = _availableLocales.reverse(),
                            browserLang,
                            possibleLang,
                            matchingLang,
                            pattern = /-.*$/,
                            locale = _defaultLocale;

                        for (i = 0; i < langs.length; i++) {
                            browserLang = langs[i];
                            if (browserLang.length < 2) {
                                continue;
                            }
                            guiLangs.filter(function (guiLang) {
                                // The langs returned by the /rest/langs call will be in lower
                                // case. We compare to the lowercase version of the language
                                // code we have as well.
                                possibleLang = guiLang.toLowerCase();
                                if (possibleLang === browserLang) {
                                    // Skip further checking if exact match found.
                                    return matchingLang = guiLang;
                                } else if (possibleLang.length > browserLang.length) {
                                    // Match "en" with "en-GB" but not "en-GB" with "en". Keep
                                    // checking even if found already, as exact match may still
                                    // be found later. Otherwise, the last match will be used.
                                    possibleLang = possibleLang.replace(pattern, '');
                                    if (possibleLang === browserLang) {
                                        matchingLang = guiLang;
                                    }
                                }
                            });

                            if (matchingLang) {
                                locale = matchingLang;
                                break;
                            }
                        }
                        // Fallback if nothing matched
                        useLocale(locale);
                    });
                }
            }

            function useLocale(language, save2Storage) {
                if (language) {
                    $translate.use(language).then(function () {

Review comment (suggestion):
I think you're misunderstanding how the `Array.filter()` function works here, or misusing it.  You're not using its return value, but it doesn't mutate the array either.  The return value of the inner function indicates whether the entry should be contained in the result.  If you just want to loop over the array to set a variable from outside the loop, then a simple `for` loop will suffice.

Code after:
                        var i,
                            browserLang,
                            matching,
                            locale = _defaultLocale; // Fallback if nothing matched

                        for (i = 0; i < langs.length; i++) {
                            browserLang = langs[i];

                            if (browserLang.length < 2) {
                                continue;
                            }

                            matching = _availableLocales.filter(function (possibleLang) {
                                // The langs returned by the /svc/langs call will be in lower
                                // case. We compare to the lowercase version of the language
                                // code we have as well.
                                possibleLang = possibleLang.toLowerCase();
                                if (possibleLang.indexOf(browserLang) !== 0) {
                                    // Prefix does not match
                                    return false;
                                }
                                if (possibleLang.length > browserLang.length) {
                                    // Must match up to the next hyphen separator
                                    return possibleLang[browserLang.length] === '-';
                                }
                                // Same length, exact match
                                return true;
                            });

                            if (matching.length >= 1) {
                                locale = matching[0];
                                break;
                            }
                        }
                        useLocale(locale);
                    });
                }
            }

            function useLocale(language, save2Storage) {
                if (language) {
                    $translate.use(language).then(function () {
                        document.documentElement.setAttribute("lang", language);
                        if (save2Storage && _localStorage)
                            _localStorage[_SYNLANG] = language;
                    });
                }
            }

            return {
                autoConfigLocale: autoConfigLocale,