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

Code before:

                return $http.get(urlbase + "/svc/lang");
            }

            function autoConfigLocale() {
                var params = $location.search();
                var savedLang;
                if (_localStorage) {
                    savedLang = _localStorage[_SYNLANG];
                }

                if (params.lang) {
                    useLocale(params.lang, true);
                } else if (savedLang) {
                    useLocale(savedLang);
                } else {
                    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;

                        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 /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();
                                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;
                            });


Review comment (suggestion):
```suggestion
                            locale = _defaultLocale;  // Fallback if nothing matched
```
This was deliberately moved up here, because it makes no sense at the bottom  where the comment stands right now.

Code after:

                return $http.get(urlbase + "/svc/lang");
            }

            function autoConfigLocale() {
                var params = $location.search();
                var savedLang;
                if (_localStorage) {
                    savedLang = _localStorage[_SYNLANG];
                }

                if (params.lang) {
                    useLocale(params.lang, true);
                } else if (savedLang) {
                    useLocale(savedLang);
                } else {
                    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;
                            });