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
This commit is contained in:
Raymond Zhao 2022-11-09 07:50:43 -08:00 committed by GitHub
parent 8f5959aad2
commit 5fc3ed936e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 9 deletions

View file

@ -4,14 +4,31 @@
#include "shell/common/language_util.h"
#include "ui/base/l10n/l10n_util.h"
#include <glib.h>
#include "base/check.h"
#include "base/i18n/rtl.h"
namespace electron {
std::vector<std::string> GetPreferredLanguages() {
// Return empty as there's no API to use. You may be able to use
// GetApplicationLocale() of a browser process.
return std::vector<std::string>{};
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