add app.setLocale() (#11469)
* infrastructure for setLocale via klang * add documentation for setLocale * add test for setLocale * fix spec * add spec and update docs * fix carriage feeds on windows * SetLocale() sets LC_ALL on Linux * in SetLocale() on Linux, use g_setenv() * fix tyop: '#ifdef OSX_POSIX' * make the linter happy * improvements from review
This commit is contained in:
parent
e3f1d90854
commit
ca34978e73
9 changed files with 92 additions and 16 deletions
|
@ -154,9 +154,12 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int AtomBrowserMainParts::PreCreateThreads() {
|
int AtomBrowserMainParts::PreCreateThreads() {
|
||||||
|
const int result = brightray::BrowserMainParts::PreCreateThreads();
|
||||||
|
if (!result) {
|
||||||
fake_browser_process_->SetApplicationLocale(
|
fake_browser_process_->SetApplicationLocale(
|
||||||
l10n_util::GetApplicationLocale(""));
|
brightray::BrowserClient::Get()->GetApplicationLocale());
|
||||||
return brightray::BrowserMainParts::PreCreateThreads();
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomBrowserMainParts::PreMainMessageLoopRun() {
|
void AtomBrowserMainParts::PreMainMessageLoopRun() {
|
||||||
|
|
|
@ -4,13 +4,17 @@
|
||||||
|
|
||||||
#include "brightray/browser/browser_main_parts.h"
|
#include "brightray/browser/browser_main_parts.h"
|
||||||
|
|
||||||
#if defined(OSX_POSIX)
|
#if defined(OS_POSIX)
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#if defined(OS_LINUX)
|
||||||
|
#include <glib.h> // for g_setenv()
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/feature_list.h"
|
#include "base/feature_list.h"
|
||||||
#include "base/message_loop/message_loop.h"
|
#include "base/message_loop/message_loop.h"
|
||||||
|
@ -29,6 +33,8 @@
|
||||||
#include "net/proxy/proxy_resolver_v8.h"
|
#include "net/proxy/proxy_resolver_v8.h"
|
||||||
#include "ui/base/l10n/l10n_util.h"
|
#include "ui/base/l10n/l10n_util.h"
|
||||||
#include "ui/base/material_design/material_design_controller.h"
|
#include "ui/base/material_design/material_design_controller.h"
|
||||||
|
#include "ui/base/resource/resource_bundle.h"
|
||||||
|
#include "ui/base/ui_base_switches.h"
|
||||||
|
|
||||||
#if defined(USE_AURA)
|
#if defined(USE_AURA)
|
||||||
#include "ui/display/display.h"
|
#include "ui/display/display.h"
|
||||||
|
@ -220,10 +226,29 @@ void BrowserMainParts::ToolkitInitialized() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BrowserMainParts::PreMainMessageLoopStart() {
|
void BrowserMainParts::PreMainMessageLoopStart() {
|
||||||
|
// Initialize ui::ResourceBundle.
|
||||||
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
||||||
|
"", nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
|
||||||
|
auto cmd_line = base::CommandLine::ForCurrentProcess();
|
||||||
|
if (cmd_line->HasSwitch(switches::kLang)) {
|
||||||
|
const std::string locale = cmd_line->GetSwitchValueASCII(switches::kLang);
|
||||||
|
const base::FilePath locale_file_path =
|
||||||
|
ui::ResourceBundle::GetSharedInstance().GetLocaleFilePath(locale, true);
|
||||||
|
if (!locale_file_path.empty()) {
|
||||||
|
custom_locale_ = locale;
|
||||||
|
#if defined(OS_LINUX)
|
||||||
|
/* When built with USE_GLIB, libcc's GetApplicationLocaleInternal() uses
|
||||||
|
* glib's g_get_language_names(), which keys off of getenv("LC_ALL") */
|
||||||
|
g_setenv("LC_ALL", custom_locale_.c_str(), TRUE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
#if defined(OS_MACOSX)
|
||||||
|
if (custom_locale_.empty())
|
||||||
l10n_util::OverrideLocaleWithCocoaLocale();
|
l10n_util::OverrideLocaleWithCocoaLocale();
|
||||||
#endif
|
#endif
|
||||||
InitializeResourceBundle("");
|
LoadResourceBundle(custom_locale_);
|
||||||
#if defined(OS_MACOSX)
|
#if defined(OS_MACOSX)
|
||||||
InitializeMainNib();
|
InitializeMainNib();
|
||||||
#endif
|
#endif
|
||||||
|
@ -277,7 +302,8 @@ int BrowserMainParts::PreCreateThreads() {
|
||||||
layout_provider_.reset(new views::LayoutProvider());
|
layout_provider_.reset(new views::LayoutProvider());
|
||||||
|
|
||||||
// Initialize the app locale.
|
// Initialize the app locale.
|
||||||
BrowserClient::SetApplicationLocale(l10n_util::GetApplicationLocale(""));
|
BrowserClient::SetApplicationLocale(
|
||||||
|
l10n_util::GetApplicationLocale(custom_locale_));
|
||||||
|
|
||||||
// Manage global state of net and other IO thread related.
|
// Manage global state of net and other IO thread related.
|
||||||
io_thread_ = base::MakeUnique<IOThread>();
|
io_thread_ = base::MakeUnique<IOThread>();
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define BRIGHTRAY_BROWSER_BROWSER_MAIN_PARTS_H_
|
#define BRIGHTRAY_BROWSER_BROWSER_MAIN_PARTS_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
|
@ -62,6 +63,7 @@ class BrowserMainParts : public content::BrowserMainParts {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::unique_ptr<views::LayoutProvider> layout_provider_;
|
std::unique_ptr<views::LayoutProvider> layout_provider_;
|
||||||
|
std::string custom_locale_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(BrowserMainParts);
|
DISALLOW_COPY_AND_ASSIGN(BrowserMainParts);
|
||||||
};
|
};
|
||||||
|
|
|
@ -42,17 +42,22 @@ bool SubprocessNeedsResourceBundle(const std::string& process_type) {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void InitializeResourceBundle(const std::string& locale) {
|
void LoadResourceBundle(const std::string& locale) {
|
||||||
// Load locales.
|
const bool initialized = ui::ResourceBundle::HasSharedInstance();
|
||||||
|
if (initialized)
|
||||||
|
ui::ResourceBundle::CleanupSharedInstance();
|
||||||
|
|
||||||
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
||||||
locale, nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
|
locale, nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
|
||||||
|
|
||||||
|
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
|
||||||
|
bundle.ReloadLocaleResources(locale);
|
||||||
|
|
||||||
// Load other resource files.
|
// Load other resource files.
|
||||||
#if defined(OS_MACOSX)
|
#if defined(OS_MACOSX)
|
||||||
LoadCommonResources();
|
LoadCommonResources();
|
||||||
#else
|
#else
|
||||||
base::FilePath pak_dir;
|
base::FilePath pak_dir;
|
||||||
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
|
|
||||||
PathService::Get(base::DIR_MODULE, &pak_dir);
|
PathService::Get(base::DIR_MODULE, &pak_dir);
|
||||||
bundle.AddDataPackFromPath(
|
bundle.AddDataPackFromPath(
|
||||||
pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")),
|
pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")),
|
||||||
|
@ -104,7 +109,7 @@ void MainDelegate::PreSandboxStartup() {
|
||||||
// browser process as a command line flag.
|
// browser process as a command line flag.
|
||||||
if (SubprocessNeedsResourceBundle(process_type)) {
|
if (SubprocessNeedsResourceBundle(process_type)) {
|
||||||
std::string locale = cmd.GetSwitchValueASCII(switches::kLang);
|
std::string locale = cmd.GetSwitchValueASCII(switches::kLang);
|
||||||
InitializeResourceBundle(locale);
|
LoadResourceBundle(locale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace brightray {
|
||||||
class BrowserClient;
|
class BrowserClient;
|
||||||
class ContentClient;
|
class ContentClient;
|
||||||
|
|
||||||
void InitializeResourceBundle(const std::string& locale);
|
void LoadResourceBundle(const std::string& locale);
|
||||||
void LoadCommonResources();
|
void LoadCommonResources();
|
||||||
|
|
||||||
class MainDelegate : public content::ContentMainDelegate {
|
class MainDelegate : public content::ContentMainDelegate {
|
||||||
|
|
|
@ -530,6 +530,12 @@ Returns `String` - The current application locale. Possible return values are do
|
||||||
|
|
||||||
**Note:** On Windows you have to call it after the `ready` events gets emitted.
|
**Note:** On Windows you have to call it after the `ready` events gets emitted.
|
||||||
|
|
||||||
|
### `app.setLocale(locale)`
|
||||||
|
|
||||||
|
* `locale` String
|
||||||
|
|
||||||
|
Set the locale of the app (must be called before the `ready` event).
|
||||||
|
|
||||||
### `app.addRecentDocument(path)` _macOS_ _Windows_
|
### `app.addRecentDocument(path)` _macOS_ _Windows_
|
||||||
|
|
||||||
* `path` String
|
* `path` String
|
||||||
|
|
|
@ -112,6 +112,25 @@ describe('app module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('app.setLocale()', () => {
|
||||||
|
const testLocale = (locale, result, done) => {
|
||||||
|
const appPath = path.join(__dirname, 'fixtures', 'api', 'locale-check')
|
||||||
|
const electronPath = remote.getGlobal('process').execPath
|
||||||
|
let output = ''
|
||||||
|
let appProcess = ChildProcess.spawn(electronPath, [appPath, `--lang=${locale}`])
|
||||||
|
|
||||||
|
appProcess.stdout.on('data', (data) => { output += data })
|
||||||
|
appProcess.stdout.on('end', () => {
|
||||||
|
output = output.replace(/(\r\n|\n|\r)/gm, '')
|
||||||
|
assert.equal(output, result)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should set the locale', (done) => testLocale('fr', 'fr', done))
|
||||||
|
it('should not set an invalid locale', (done) => testLocale('asdfkl', 'en-US', done))
|
||||||
|
})
|
||||||
|
|
||||||
describe('app.isInApplicationsFolder()', () => {
|
describe('app.isInApplicationsFolder()', () => {
|
||||||
before(function () {
|
before(function () {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
|
|
10
spec/fixtures/api/locale-check/main.js
vendored
Normal file
10
spec/fixtures/api/locale-check/main.js
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
const {app} = require('electron')
|
||||||
|
|
||||||
|
app.on('ready', () => {
|
||||||
|
process.stdout.write(app.getLocale())
|
||||||
|
process.stdout.end()
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
app.quit()
|
||||||
|
})
|
||||||
|
})
|
5
spec/fixtures/api/locale-check/package.json
vendored
Normal file
5
spec/fixtures/api/locale-check/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"name": "locale-check",
|
||||||
|
"main": "main.js"
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue