Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: navigator.languages to use OS preferred languages #15532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions atom/app/atom_main_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AtomMainDelegate : public content::ContentMainDelegate {
#if defined(OS_MACOSX)
bool ShouldSendMachPort(const std::string& process_type) override;
bool DelaySandboxInitialization(const std::string& process_type) override;
std::string GetSystemPreferredLanguage();
#endif
bool ShouldLockSchemeRegistry() override;
bool ShouldCreateFeatureList() override;
Expand Down
5 changes: 5 additions & 0 deletions atom/app/atom_main_delegate_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
base::mac::SetBaseBundleID(base_bundle_id.c_str());
}

std::string AtomMainDelegate::GetSystemPreferredLanguage() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this get used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think it got lost somewhere in the rebase. I'll update.

return base::SysNSStringToUTF8(
[[NSLocale preferredLanguages] objectAtIndex:0]);
}

void RegisterAtomCrApp() {
// Force the NSApplication subclass to be used.
[AtomApplication sharedApplication];
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void WebContents::InitWithSessionAndOptions(
managed_web_contents()->GetView()->SetDelegate(this);

auto* prefs = web_contents()->GetMutableRendererPrefs();
prefs->accept_languages = g_browser_process->GetApplicationLocale();
SetAcceptLanguages(prefs);

#if defined(OS_LINUX) || defined(OS_WIN)
// Update font settings.
Expand Down
6 changes: 6 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace blink {
struct WebDeviceEmulationParams;
}

namespace content {
struct RendererPreferences;
}

namespace mate {
class Arguments;
class Dictionary;
Expand Down Expand Up @@ -514,6 +518,8 @@ class WebContents : public mate::TrackableObject<WebContents>,

void InitZoomController(content::WebContents* web_contents,
const mate::Dictionary& options);
// Update the accept_languages pref
void SetAcceptLanguages(content::RendererPreferences* prefs);

v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_;
Expand Down
20 changes: 20 additions & 0 deletions atom/browser/api/atom_api_web_contents_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/api/atom_api_web_contents.h"

#include "chrome/browser/browser_process.h"
#include "content/public/common/renderer_preferences.h"

namespace atom {

namespace api {

void WebContents::SetAcceptLanguages(content::RendererPreferences* prefs) {
prefs->accept_languages = g_browser_process->GetApplicationLocale();
}

} // namespace api

} // namespace atom
7 changes: 7 additions & 0 deletions atom/browser/api/atom_api_web_contents_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// found in the LICENSE file.

#include "atom/browser/api/atom_api_web_contents.h"
#include "base/strings/sys_string_conversions.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/renderer_preferences.h"

#import <Cocoa/Cocoa.h>

Expand All @@ -27,6 +29,11 @@
return view->HasFocus();
}

void WebContents::SetAcceptLanguages(content::RendererPreferences* prefs) {
prefs->accept_languages = base::SysNSStringToUTF8(
[[NSLocale preferredLanguages] componentsJoinedByString:@","]);
}

} // namespace api

} // namespace atom
28 changes: 28 additions & 0 deletions atom/browser/api/atom_api_web_contents_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/api/atom_api_web_contents.h"

#include "base/strings/sys_string_conversions.h"
#include "base/win/i18n.h"
#include "content/public/common/renderer_preferences.h"

namespace atom {

namespace api {

void WebContents::SetAcceptLanguages(content::RendererPreferences* prefs) {
std::vector<base::string16> languages;
base::win::i18n::GetThreadPreferredUILanguageList(&languages);
std::string accept_langs;
for (const auto& s16 : languages) {
accept_langs += base::SysWideToUTF8(s16) + ',';
}
accept_langs.pop_back();
prefs->accept_languages = accept_langs;
}

} // namespace api

} // namespace atom
2 changes: 2 additions & 0 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ filenames = {
"atom/browser/api/atom_api_web_contents.h",
"atom/browser/api/atom_api_web_contents_impl.cc",
"atom/browser/api/atom_api_web_contents_mac.mm",
"atom/browser/api/atom_api_web_contents_linux.cc",
"atom/browser/api/atom_api_web_contents_win.cc",
"atom/browser/api/atom_api_web_contents_view.cc",
"atom/browser/api/atom_api_web_contents_view.h",
"atom/browser/api/atom_api_web_request.cc",
Expand Down
7 changes: 4 additions & 3 deletions spec/chromium-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,11 @@ describe('chromium feature', () => {
})

describe('navigator.languages', (done) => {
it('should return the system locale only', () => {
it('should return the system locales', () => {
const appLocale = app.getLocale()
assert.strictEqual(navigator.languages.length, 1)
assert.strictEqual(navigator.languages[0], appLocale)
expect(navigator.languages.length).greaterThan(0)
// compare only the language not the region as appLocale provides that
expect(navigator.languages[0].split('-')[0]).equal(appLocale.split('-')[0])
})
})

Expand Down
24 changes: 12 additions & 12 deletions spec/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.