| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #include "i18n-extension.h" | |
| 29 | |
| 30 #include <algorithm> | |
| 31 #include <string> | |
| 32 | |
| 33 #include "unicode/locid.h" | |
| 34 #include "unicode/uloc.h" | |
| 35 | |
| 36 namespace v8 { | |
| 37 namespace internal { | |
| 38 | |
| 39 I18NExtension* I18NExtension::extension_ = NULL; | |
| 40 | |
| 41 // TODO(cira): maybe move JS code to a .js file and generata cc files from it? | |
| 42 const char* const I18NExtension::kSource = | |
| 43 "Locale = function(optLocale) {" | |
| 44 " native function NativeJSLocale();" | |
| 45 " var properties = NativeJSLocale(optLocale);" | |
| 46 " this.locale = properties.locale;" | |
| 47 " this.language = properties.language;" | |
| 48 " this.script = properties.script;" | |
| 49 " this.region = properties.region;" | |
| 50 "};" | |
| 51 "Locale.availableLocales = function() {" | |
| 52 " native function NativeJSAvailableLocales();" | |
| 53 " return NativeJSAvailableLocales();" | |
| 54 "};" | |
| 55 "Locale.prototype.maximizedLocale = function() {" | |
| 56 " native function NativeJSMaximizedLocale();" | |
| 57 " return new Locale(NativeJSMaximizedLocale(this.locale));" | |
| 58 "};" | |
| 59 "Locale.prototype.minimizedLocale = function() {" | |
| 60 " native function NativeJSMinimizedLocale();" | |
| 61 " return new Locale(NativeJSMinimizedLocale(this.locale));" | |
| 62 "};" | |
| 63 "Locale.prototype.displayLocale_ = function(displayLocale) {" | |
| 64 " var result = this.locale;" | |
| 65 " if (displayLocale !== undefined) {" | |
| 66 " result = displayLocale.locale;" | |
| 67 " }" | |
| 68 " return result;" | |
| 69 "};" | |
| 70 "Locale.prototype.displayLanguage = function(optDisplayLocale) {" | |
| 71 " var displayLocale = this.displayLocale_(optDisplayLocale);" | |
| 72 " native function NativeJSDisplayLanguage();" | |
| 73 " return NativeJSDisplayLanguage(this.locale, displayLocale);" | |
| 74 "};" | |
| 75 "Locale.prototype.displayScript = function(optDisplayLocale) {" | |
| 76 " var displayLocale = this.displayLocale_(optDisplayLocale);" | |
| 77 " native function NativeJSDisplayScript();" | |
| 78 " return NativeJSDisplayScript(this.locale, displayLocale);" | |
| 79 "};" | |
| 80 "Locale.prototype.displayRegion = function(optDisplayLocale) {" | |
| 81 " var displayLocale = this.displayLocale_(optDisplayLocale);" | |
| 82 " native function NativeJSDisplayRegion();" | |
| 83 " return NativeJSDisplayRegion(this.locale, displayLocale);" | |
| 84 "};" | |
| 85 "Locale.prototype.displayName = function(optDisplayLocale) {" | |
| 86 " var displayLocale = this.displayLocale_(optDisplayLocale);" | |
| 87 " native function NativeJSDisplayName();" | |
| 88 " return NativeJSDisplayName(this.locale, displayLocale);" | |
| 89 "};"; | |
| 90 | |
| 91 v8::Handle<v8::FunctionTemplate> I18NExtension::GetNativeFunction( | |
| 92 v8::Handle<v8::String> name) { | |
| 93 if (name->Equals(v8::String::New("NativeJSLocale"))) { | |
| 94 return v8::FunctionTemplate::New(JSLocale); | |
| 95 } else if (name->Equals(v8::String::New("NativeJSAvailableLocales"))) { | |
| 96 return v8::FunctionTemplate::New(JSAvailableLocales); | |
| 97 } else if (name->Equals(v8::String::New("NativeJSMaximizedLocale"))) { | |
| 98 return v8::FunctionTemplate::New(JSMaximizedLocale); | |
| 99 } else if (name->Equals(v8::String::New("NativeJSMinimizedLocale"))) { | |
| 100 return v8::FunctionTemplate::New(JSMinimizedLocale); | |
| 101 } else if (name->Equals(v8::String::New("NativeJSDisplayLanguage"))) { | |
| 102 return v8::FunctionTemplate::New(JSDisplayLanguage); | |
| 103 } else if (name->Equals(v8::String::New("NativeJSDisplayScript"))) { | |
| 104 return v8::FunctionTemplate::New(JSDisplayScript); | |
| 105 } else if (name->Equals(v8::String::New("NativeJSDisplayRegion"))) { | |
| 106 return v8::FunctionTemplate::New(JSDisplayRegion); | |
| 107 } else if (name->Equals(v8::String::New("NativeJSDisplayName"))) { | |
| 108 return v8::FunctionTemplate::New(JSDisplayName); | |
| 109 } | |
| 110 | |
| 111 return v8::Handle<v8::FunctionTemplate>(); | |
| 112 } | |
| 113 | |
| 114 v8::Handle<v8::Value> I18NExtension::JSLocale(const v8::Arguments& args) { | |
| 115 // TODO(cira): Fetch browser locale. Accept en-US as good default for now. | |
| 116 // We could possibly pass browser locale as a parameter in the constructor. | |
| 117 std::string locale_name("en-US"); | |
| 118 if (args.Length() == 1 && args[0]->IsString()) { | |
| 119 locale_name = *v8::String::Utf8Value(args[0]->ToString()); | |
| 120 } | |
| 121 | |
| 122 v8::Local<v8::Object> locale = v8::Object::New(); | |
| 123 locale->Set(v8::String::New("locale"), v8::String::New(locale_name.c_str())); | |
| 124 | |
| 125 icu::Locale icu_locale(locale_name.c_str()); | |
| 126 | |
| 127 const char* language = icu_locale.getLanguage(); | |
| 128 locale->Set(v8::String::New("language"), v8::String::New(language)); | |
| 129 | |
| 130 const char* script = icu_locale.getScript(); | |
| 131 if (strlen(script)) { | |
| 132 locale->Set(v8::String::New("script"), v8::String::New(script)); | |
| 133 } | |
| 134 | |
| 135 const char* region = icu_locale.getCountry(); | |
| 136 if (strlen(region)) { | |
| 137 locale->Set(v8::String::New("region"), v8::String::New(region)); | |
| 138 } | |
| 139 | |
| 140 return locale; | |
| 141 } | |
| 142 | |
| 143 // TODO(cira): Filter out locales that Chrome doesn't support. | |
| 144 v8::Handle<v8::Value> I18NExtension::JSAvailableLocales( | |
| 145 const v8::Arguments& args) { | |
| 146 v8::Local<v8::Array> all_locales = v8::Array::New(); | |
| 147 | |
| 148 int count = 0; | |
| 149 const Locale* icu_locales = icu::Locale::getAvailableLocales(count); | |
| 150 for (int i = 0; i < count; ++i) { | |
| 151 all_locales->Set(i, v8::String::New(icu_locales[i].getName())); | |
| 152 } | |
| 153 | |
| 154 return all_locales; | |
| 155 } | |
| 156 | |
| 157 // Use - as tag separator, not _ that ICU uses. | |
| 158 static std::string NormalizeLocale(const std::string& locale) { | |
| 159 std::string result(locale); | |
| 160 // TODO(cira): remove STL dependency. | |
| 161 std::replace(result.begin(), result.end(), '_', '-'); | |
| 162 return result; | |
| 163 } | |
| 164 | |
| 165 v8::Handle<v8::Value> I18NExtension::JSMaximizedLocale( | |
| 166 const v8::Arguments& args) { | |
| 167 if (!args.Length() || !args[0]->IsString()) { | |
| 168 return v8::Undefined(); | |
| 169 } | |
| 170 | |
| 171 UErrorCode status = U_ZERO_ERROR; | |
| 172 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString()); | |
| 173 char max_locale[ULOC_FULLNAME_CAPACITY]; | |
| 174 uloc_addLikelySubtags(locale_name.c_str(), max_locale, | |
| 175 sizeof(max_locale), &status); | |
| 176 if (U_FAILURE(status)) { | |
| 177 return v8::Undefined(); | |
| 178 } | |
| 179 | |
| 180 return v8::String::New(NormalizeLocale(max_locale).c_str()); | |
| 181 } | |
| 182 | |
| 183 v8::Handle<v8::Value> I18NExtension::JSMinimizedLocale( | |
| 184 const v8::Arguments& args) { | |
| 185 if (!args.Length() || !args[0]->IsString()) { | |
| 186 return v8::Undefined(); | |
| 187 } | |
| 188 | |
| 189 UErrorCode status = U_ZERO_ERROR; | |
| 190 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString()); | |
| 191 char min_locale[ULOC_FULLNAME_CAPACITY]; | |
| 192 uloc_minimizeSubtags(locale_name.c_str(), min_locale, | |
| 193 sizeof(min_locale), &status); | |
| 194 if (U_FAILURE(status)) { | |
| 195 return v8::Undefined(); | |
| 196 } | |
| 197 | |
| 198 return v8::String::New(NormalizeLocale(min_locale).c_str()); | |
| 199 } | |
| 200 | |
| 201 // Common code for JSDisplayXXX methods. | |
| 202 static v8::Handle<v8::Value> GetDisplayItem(const v8::Arguments& args, | |
| 203 const std::string& item) { | |
| 204 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
| 205 return v8::Undefined(); | |
| 206 } | |
| 207 | |
| 208 std::string base_locale = *v8::String::Utf8Value(args[0]->ToString()); | |
| 209 icu::Locale icu_locale(base_locale.c_str()); | |
| 210 icu::Locale display_locale = | |
| 211 icu::Locale(*v8::String::Utf8Value(args[1]->ToString())); | |
| 212 UnicodeString result; | |
| 213 if (item == "language") { | |
| 214 icu_locale.getDisplayLanguage(display_locale, result); | |
| 215 } else if (item == "script") { | |
| 216 icu_locale.getDisplayScript(display_locale, result); | |
| 217 } else if (item == "region") { | |
| 218 icu_locale.getDisplayCountry(display_locale, result); | |
| 219 } else if (item == "name") { | |
| 220 icu_locale.getDisplayName(display_locale, result); | |
| 221 } else { | |
| 222 return v8::Undefined(); | |
| 223 } | |
| 224 | |
| 225 if (result.length()) { | |
| 226 return v8::String::New( | |
| 227 reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length()); | |
| 228 } | |
| 229 | |
| 230 return v8::Undefined(); | |
| 231 } | |
| 232 | |
| 233 v8::Handle<v8::Value> I18NExtension::JSDisplayLanguage( | |
| 234 const v8::Arguments& args) { | |
| 235 return GetDisplayItem(args, "language"); | |
| 236 } | |
| 237 | |
| 238 v8::Handle<v8::Value> I18NExtension::JSDisplayScript( | |
| 239 const v8::Arguments& args) { | |
| 240 return GetDisplayItem(args, "script"); | |
| 241 } | |
| 242 | |
| 243 v8::Handle<v8::Value> I18NExtension::JSDisplayRegion( | |
| 244 const v8::Arguments& args) { | |
| 245 return GetDisplayItem(args, "region"); | |
| 246 } | |
| 247 | |
| 248 v8::Handle<v8::Value> I18NExtension::JSDisplayName(const v8::Arguments& args) { | |
| 249 return GetDisplayItem(args, "name"); | |
| 250 } | |
| 251 | |
| 252 I18NExtension* I18NExtension::get() { | |
| 253 if (!extension_) { | |
| 254 extension_ = new I18NExtension(); | |
| 255 } | |
| 256 return extension_; | |
| 257 } | |
| 258 | |
| 259 void I18NExtension::Register() { | |
| 260 static v8::DeclareExtension i18n_extension_declaration(I18NExtension::get()); | |
| 261 } | |
| 262 | |
| 263 } } // namespace v8::internal | |
| OLD | NEW |