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

Code before:
                    readBrowserLocales().success(function (langs) {
                        // Find the first language in the list provided by the user's browser
                        // that is a prefix of a language we have available. That is, "en"
                        // sent by the browser will match "en" or "en-US", while "zh-TW" will
                        // match only "zh-TW" and not "zh-CN".

                        var i,
                            lang,
                            matching,
                            pattern = /-.*$/,
                            locale = _defaultLocale;

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

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

                            matching = _availableLocales.filter(function (possibleLang) {
                                // 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 = possibleLang.toLowerCase();
                                // Match "en" with "en-US" but not "fi" with "fil",
                                // and match "en-US" with "en" but not "fil" with "fi".
                                if (possibleLang !== lang) {
                                    lang = lang.replace(pattern, '');
                                    possibleLang = possibleLang.replace(pattern, '');
                                }
                                return lang === possibleLang;
                            });

                            if (matching) {
                                locale = matching[0];
                                break;
                            }
                        }
                        // Fallback if nothing matched
                        useLocale(locale);
                    });
                }
            }

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

Review comment (suggestion):
```suggestion
                                // Match "en" with "en-US" but not "fi" with "fil", and vice versa
```

Code after:
                    readBrowserLocales().success(function (langs) {
                        // Find the first language in the list provided by the user's browser
                        // that is a prefix of a language we have available. That is, "en"
                        // sent by the browser will match "en" or "en-US", while "zh-TW" will
                        // match only "zh-TW" and not "zh" or "zh-CN".

                        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;