OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/i18n_custom_bindings.h" | 5 #include "extensions/renderer/i18n_custom_bindings.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "extensions/common/extension_messages.h" | 10 #include "extensions/common/extension_messages.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 const v8::FunctionCallbackInfo<v8::Value>& args) { | 27 const v8::FunctionCallbackInfo<v8::Value>& args) { |
28 if (args.Length() != 3 || !args[0]->IsString()) { | 28 if (args.Length() != 3 || !args[0]->IsString()) { |
29 NOTREACHED() << "Bad arguments"; | 29 NOTREACHED() << "Bad arguments"; |
30 return; | 30 return; |
31 } | 31 } |
32 | 32 |
33 std::string extension_id; | 33 std::string extension_id; |
34 if (args[2]->IsNull() || !args[2]->IsString()) { | 34 if (args[2]->IsNull() || !args[2]->IsString()) { |
35 return; | 35 return; |
36 } else { | 36 } else { |
37 extension_id = *v8::String::Utf8Value(args[2]->ToString()); | 37 extension_id = *v8::String::Utf8Value(args[2]); |
38 if (extension_id.empty()) | 38 if (extension_id.empty()) |
39 return; | 39 return; |
40 } | 40 } |
41 | 41 |
42 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); | 42 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); |
43 if (!l10n_messages) { | 43 if (!l10n_messages) { |
44 // Get the current RenderView so that we can send a routed IPC message | 44 // Get the current RenderView so that we can send a routed IPC message |
45 // from the correct source. | 45 // from the correct source. |
46 content::RenderView* renderview = context()->GetRenderView(); | 46 content::RenderView* renderview = context()->GetRenderView(); |
47 if (!renderview) | 47 if (!renderview) |
(...skipping 18 matching lines...) Expand all Loading... |
66 | 66 |
67 v8::Isolate* isolate = args.GetIsolate(); | 67 v8::Isolate* isolate = args.GetIsolate(); |
68 std::vector<std::string> substitutions; | 68 std::vector<std::string> substitutions; |
69 if (args[1]->IsArray()) { | 69 if (args[1]->IsArray()) { |
70 // chrome.i18n.getMessage("message_name", ["more", "params"]); | 70 // chrome.i18n.getMessage("message_name", ["more", "params"]); |
71 v8::Local<v8::Array> placeholders = v8::Local<v8::Array>::Cast(args[1]); | 71 v8::Local<v8::Array> placeholders = v8::Local<v8::Array>::Cast(args[1]); |
72 uint32_t count = placeholders->Length(); | 72 uint32_t count = placeholders->Length(); |
73 if (count > 9) | 73 if (count > 9) |
74 return; | 74 return; |
75 for (uint32_t i = 0; i < count; ++i) { | 75 for (uint32_t i = 0; i < count; ++i) { |
76 substitutions.push_back( | 76 substitutions.push_back(*v8::String::Utf8Value(placeholders->Get( |
77 *v8::String::Utf8Value( | 77 v8::Integer::New(isolate, i)))); |
78 placeholders->Get(v8::Integer::New(isolate, i))->ToString())); | |
79 } | 78 } |
80 } else if (args[1]->IsString()) { | 79 } else if (args[1]->IsString()) { |
81 // chrome.i18n.getMessage("message_name", "one param"); | 80 // chrome.i18n.getMessage("message_name", "one param"); |
82 substitutions.push_back(*v8::String::Utf8Value(args[1]->ToString())); | 81 substitutions.push_back(*v8::String::Utf8Value(args[1])); |
83 } | 82 } |
84 | 83 |
85 args.GetReturnValue().Set(v8::String::NewFromUtf8( | 84 args.GetReturnValue().Set(v8::String::NewFromUtf8( |
86 isolate, | 85 isolate, |
87 ReplaceStringPlaceholders(message, substitutions, NULL).c_str())); | 86 ReplaceStringPlaceholders(message, substitutions, NULL).c_str())); |
88 } | 87 } |
89 | 88 |
90 void I18NCustomBindings::GetL10nUILanguage( | 89 void I18NCustomBindings::GetL10nUILanguage( |
91 const v8::FunctionCallbackInfo<v8::Value>& args) { | 90 const v8::FunctionCallbackInfo<v8::Value>& args) { |
92 args.GetReturnValue().Set(v8::String::NewFromUtf8( | 91 args.GetReturnValue().Set(v8::String::NewFromUtf8( |
93 args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str())); | 92 args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str())); |
94 } | 93 } |
95 | 94 |
96 } // namespace extensions | 95 } // namespace extensions |
OLD | NEW |