electron/shell/common/language_util_linux.cc
Raymond Zhao 5fc3ed936e
feat: add app.getPreferredSystemLanguages() API (#36035)
* feat: add app.getSystemLanguage() API

* Change the API to getPreferredSystemLanguages

* Fix test

* Clarify docs and add Linux impl

* Remove USE_GLIB

* Don't add C to list

* Remove examples since there's a lot of edge cases

* Fix lint

* Add examples

* Fix compile error

* Apply PR feedback

* Update the example
2022-11-09 10:50:43 -05:00

34 lines
1 KiB
C++

// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/language_util.h"
#include <glib.h>
#include "base/check.h"
#include "base/i18n/rtl.h"
namespace electron {
std::vector<std::string> GetPreferredLanguages() {
std::vector<std::string> preferredLanguages;
// Based on
// https://source.chromium.org/chromium/chromium/src/+/refs/tags/108.0.5329.0:ui/base/l10n/l10n_util.cc;l=543-554
// GLib implements correct environment variable parsing with
// the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
const char* const* languages = g_get_language_names();
DCHECK(languages); // A valid pointer is guaranteed.
DCHECK(*languages); // At least one entry, "C", is guaranteed.
for (; *languages; ++languages) {
if (strcmp(*languages, "C") != 0) {
preferredLanguages.push_back(base::i18n::GetCanonicalLocale(*languages));
}
}
return preferredLanguages;
}
} // namespace electron