Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/extensions/experimental/i18n-locale.cc

Issue 6840024: Moving locale code from i18n-extension.cc. Each part of the API now has separate cc file, and onl... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/extensions/experimental/i18n-locale.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 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-locale.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 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) {
40 // TODO(cira): Fetch browser locale. Accept en-US as good default for now.
41 // We could possibly pass browser locale as a parameter in the constructor.
42 std::string locale_name("en-US");
43 if (args.Length() == 1 && args[0]->IsString()) {
44 locale_name = *v8::String::Utf8Value(args[0]->ToString());
45 }
46
47 v8::Local<v8::Object> locale = v8::Object::New();
48 locale->Set(v8::String::New("locale"), v8::String::New(locale_name.c_str()));
49
50 icu::Locale icu_locale(locale_name.c_str());
51
52 const char* language = icu_locale.getLanguage();
53 locale->Set(v8::String::New("language"), v8::String::New(language));
54
55 const char* script = icu_locale.getScript();
56 if (strlen(script)) {
57 locale->Set(v8::String::New("script"), v8::String::New(script));
58 }
59
60 const char* region = icu_locale.getCountry();
61 if (strlen(region)) {
62 locale->Set(v8::String::New("region"), v8::String::New(region));
63 }
64
65 return locale;
66 }
67
68 // TODO(cira): Filter out locales that Chrome doesn't support.
69 v8::Handle<v8::Value> I18NLocale::JSAvailableLocales(
70 const v8::Arguments& args) {
71 v8::Local<v8::Array> all_locales = v8::Array::New();
72
73 int count = 0;
74 const icu::Locale* icu_locales = icu::Locale::getAvailableLocales(count);
75 for (int i = 0; i < count; ++i) {
76 all_locales->Set(i, v8::String::New(icu_locales[i].getName()));
77 }
78
79 return all_locales;
80 }
81
82 // Use - as tag separator, not _ that ICU uses.
83 static std::string NormalizeLocale(const std::string& locale) {
84 std::string result(locale);
85 // TODO(cira): remove STL dependency.
86 std::replace(result.begin(), result.end(), '_', '-');
87 return result;
88 }
89
90 v8::Handle<v8::Value> I18NLocale::JSMaximizedLocale(const v8::Arguments& args) {
91 if (!args.Length() || !args[0]->IsString()) {
92 return v8::Undefined();
93 }
94
95 UErrorCode status = U_ZERO_ERROR;
96 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString());
97 char max_locale[ULOC_FULLNAME_CAPACITY];
98 uloc_addLikelySubtags(locale_name.c_str(), max_locale,
99 sizeof(max_locale), &status);
100 if (U_FAILURE(status)) {
101 return v8::Undefined();
102 }
103
104 return v8::String::New(NormalizeLocale(max_locale).c_str());
105 }
106
107 v8::Handle<v8::Value> I18NLocale::JSMinimizedLocale(const v8::Arguments& args) {
108 if (!args.Length() || !args[0]->IsString()) {
109 return v8::Undefined();
110 }
111
112 UErrorCode status = U_ZERO_ERROR;
113 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString());
114 char min_locale[ULOC_FULLNAME_CAPACITY];
115 uloc_minimizeSubtags(locale_name.c_str(), min_locale,
116 sizeof(min_locale), &status);
117 if (U_FAILURE(status)) {
118 return v8::Undefined();
119 }
120
121 return v8::String::New(NormalizeLocale(min_locale).c_str());
122 }
123
124 // Common code for JSDisplayXXX methods.
125 static v8::Handle<v8::Value> GetDisplayItem(const v8::Arguments& args,
126 const std::string& item) {
127 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) {
128 return v8::Undefined();
129 }
130
131 std::string base_locale = *v8::String::Utf8Value(args[0]->ToString());
132 icu::Locale icu_locale(base_locale.c_str());
133 icu::Locale display_locale =
134 icu::Locale(*v8::String::Utf8Value(args[1]->ToString()));
135 icu::UnicodeString result;
136 if (item == "language") {
137 icu_locale.getDisplayLanguage(display_locale, result);
138 } else if (item == "script") {
139 icu_locale.getDisplayScript(display_locale, result);
140 } else if (item == "region") {
141 icu_locale.getDisplayCountry(display_locale, result);
142 } else if (item == "name") {
143 icu_locale.getDisplayName(display_locale, result);
144 } else {
145 return v8::Undefined();
146 }
147
148 if (result.length()) {
149 return v8::String::New(
150 reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length());
151 }
152
153 return v8::Undefined();
154 }
155
156 v8::Handle<v8::Value> I18NLocale::JSDisplayLanguage(const v8::Arguments& args) {
157 return GetDisplayItem(args, "language");
158 }
159
160 v8::Handle<v8::Value> I18NLocale::JSDisplayScript(const v8::Arguments& args) {
161 return GetDisplayItem(args, "script");
162 }
163
164 v8::Handle<v8::Value> I18NLocale::JSDisplayRegion(const v8::Arguments& args) {
165 return GetDisplayItem(args, "region");
166 }
167
168 v8::Handle<v8::Value> I18NLocale::JSDisplayName(const v8::Arguments& args) {
169 return GetDisplayItem(args, "name");
170 }
171
172 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/experimental/i18n-locale.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698