Commit ba8c17fc authored by mlamouri@chromium.org's avatar mlamouri@chromium.org

Add getUILanguage() to chrome.i18n which returns the browser UI language.

For the moment, extension and internal code relies on navigator.language
to return the browser UI language but it should return the preferred
language for fingerprinting reasons, see http://crbug.com/101138.

BUG=348402

Review URL: https://codereview.chromium.org/185423003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255663 0039d316-1c4b-4281-b951-d872f2087c98
parent 9bf06c75
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
{ {
"name": "getAcceptLanguages", "name": "getAcceptLanguages",
"type": "function", "type": "function",
"description": "Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use <code>window.navigator.language</code>.", "description": "Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use $ref:i18n.getUILanguage.",
"parameters": [ "parameters": [
{ {
"type": "function", "type": "function",
...@@ -44,6 +44,17 @@ ...@@ -44,6 +44,17 @@
"type": "string", "type": "string",
"description": "Message localized for current locale." "description": "Message localized for current locale."
} }
},
{
"name": "getUILanguage",
"type": "function",
"nocompile": true,
"description": "Gets the browser UI language of the browser. This is different from $ref:i18n.getAcceptLanguages which returns the preferred user languages.",
"parameters": [],
"returns": {
"type": "string",
"description": "The browser UI language code such as en-US or fr-FR."
}
} }
], ],
"events": [] "events": []
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/message_bundle.h" #include "chrome/common/extensions/message_bundle.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h" #include "content/public/renderer/render_view.h"
#include "grit/renderer_resources.h" #include "grit/renderer_resources.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
...@@ -18,6 +19,9 @@ I18NCustomBindings::I18NCustomBindings(Dispatcher* dispatcher, ...@@ -18,6 +19,9 @@ I18NCustomBindings::I18NCustomBindings(Dispatcher* dispatcher,
: ChromeV8Extension(dispatcher, context) { : ChromeV8Extension(dispatcher, context) {
RouteFunction("GetL10nMessage", RouteFunction("GetL10nMessage",
base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this))); base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this)));
RouteFunction("GetL10nUILanguage",
base::Bind(&I18NCustomBindings::GetL10nUILanguage,
base::Unretained(this)));
} }
void I18NCustomBindings::GetL10nMessage( void I18NCustomBindings::GetL10nMessage(
...@@ -84,4 +88,10 @@ void I18NCustomBindings::GetL10nMessage( ...@@ -84,4 +88,10 @@ void I18NCustomBindings::GetL10nMessage(
message, substitutions, NULL).c_str())); message, substitutions, NULL).c_str()));
} }
void I18NCustomBindings::GetL10nUILanguage(
const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(v8::String::NewFromUtf8(
args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str()));
}
} // namespace extensions } // namespace extensions
...@@ -16,6 +16,7 @@ class I18NCustomBindings : public ChromeV8Extension { ...@@ -16,6 +16,7 @@ class I18NCustomBindings : public ChromeV8Extension {
private: private:
void GetL10nMessage(const v8::FunctionCallbackInfo<v8::Value>& args); void GetL10nMessage(const v8::FunctionCallbackInfo<v8::Value>& args);
void GetL10nUILanguage(const v8::FunctionCallbackInfo<v8::Value>& args);
}; };
} // namespace extensions } // namespace extensions
......
...@@ -8,6 +8,7 @@ var binding = require('binding').Binding.create('i18n'); ...@@ -8,6 +8,7 @@ var binding = require('binding').Binding.create('i18n');
var i18nNatives = requireNative('i18n'); var i18nNatives = requireNative('i18n');
var GetL10nMessage = i18nNatives.GetL10nMessage; var GetL10nMessage = i18nNatives.GetL10nMessage;
var GetL10nUILanguage = i18nNatives.GetL10nUILanguage;
binding.registerCustomHook(function(bindingsAPI, extensionId) { binding.registerCustomHook(function(bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions; var apiFunctions = bindingsAPI.apiFunctions;
...@@ -31,6 +32,10 @@ binding.registerCustomHook(function(bindingsAPI, extensionId) { ...@@ -31,6 +32,10 @@ binding.registerCustomHook(function(bindingsAPI, extensionId) {
function(messageName, substitutions) { function(messageName, substitutions) {
return GetL10nMessage(messageName, substitutions, extensionId); return GetL10nMessage(messageName, substitutions, extensionId);
}); });
apiFunctions.setHandleRequest('getUILanguage', function() {
return GetL10nUILanguage();
});
}); });
exports.binding = binding.generate(); exports.binding = binding.generate();
...@@ -54,6 +54,10 @@ chrome.test.getConfig(function(config) { ...@@ -54,6 +54,10 @@ chrome.test.getConfig(function(config) {
url: TEST_FILE_URL url: TEST_FILE_URL
}); });
chrome.test.succeed(); chrome.test.succeed();
},
function getUILanguage() {
chrome.test.assertEq('en-US', chrome.i18n.getUILanguage());
chrome.test.succeed();
} }
]); ]);
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment