1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/i18n_custom_bindings.h"
8 #include "chrome/common/extensions/message_bundle.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "content/public/renderer/render_view.h"
11 #include "extensions/common/extension_messages.h"
12 #include "grit/renderer_resources.h"
13 #include "v8/include/v8.h"
15 namespace extensions
{
17 I18NCustomBindings::I18NCustomBindings(Dispatcher
* dispatcher
,
18 ChromeV8Context
* context
)
19 : ChromeV8Extension(dispatcher
, context
) {
20 RouteFunction("GetL10nMessage",
21 base::Bind(&I18NCustomBindings::GetL10nMessage
, base::Unretained(this)));
22 RouteFunction("GetL10nUILanguage",
23 base::Bind(&I18NCustomBindings::GetL10nUILanguage
,
24 base::Unretained(this)));
27 void I18NCustomBindings::GetL10nMessage(
28 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
29 if (args
.Length() != 3 || !args
[0]->IsString()) {
30 NOTREACHED() << "Bad arguments";
34 std::string extension_id
;
35 if (args
[2]->IsNull() || !args
[2]->IsString()) {
38 extension_id
= *v8::String::Utf8Value(args
[2]->ToString());
39 if (extension_id
.empty())
43 L10nMessagesMap
* l10n_messages
= GetL10nMessagesMap(extension_id
);
45 // Get the current RenderView so that we can send a routed IPC message
46 // from the correct source.
47 content::RenderView
* renderview
= GetRenderView();
51 L10nMessagesMap messages
;
52 // A sync call to load message catalogs for current extension.
53 renderview
->Send(new ExtensionHostMsg_GetMessageBundle(
54 extension_id
, &messages
));
56 // Save messages we got.
57 ExtensionToL10nMessagesMap
& l10n_messages_map
=
58 *GetExtensionToL10nMessagesMap();
59 l10n_messages_map
[extension_id
] = messages
;
61 l10n_messages
= GetL10nMessagesMap(extension_id
);
64 std::string message_name
= *v8::String::Utf8Value(args
[0]);
66 MessageBundle::GetL10nMessage(message_name
, *l10n_messages
);
68 v8::Isolate
* isolate
= args
.GetIsolate();
69 std::vector
<std::string
> substitutions
;
70 if (args
[1]->IsArray()) {
71 // chrome.i18n.getMessage("message_name", ["more", "params"]);
72 v8::Local
<v8::Array
> placeholders
= v8::Local
<v8::Array
>::Cast(args
[1]);
73 uint32_t count
= placeholders
->Length();
76 for (uint32_t i
= 0; i
< count
; ++i
) {
77 substitutions
.push_back(
78 *v8::String::Utf8Value(
79 placeholders
->Get(v8::Integer::New(isolate
, i
))->ToString()));
81 } else if (args
[1]->IsString()) {
82 // chrome.i18n.getMessage("message_name", "one param");
83 substitutions
.push_back(*v8::String::Utf8Value(args
[1]->ToString()));
86 args
.GetReturnValue().Set(
87 v8::String::NewFromUtf8(isolate
, ReplaceStringPlaceholders(
88 message
, substitutions
, NULL
).c_str()));
91 void I18NCustomBindings::GetL10nUILanguage(
92 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
93 args
.GetReturnValue().Set(v8::String::NewFromUtf8(
94 args
.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str()));
97 } // namespace extensions