OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/l10n/l10n_util.h" | 5 #include "ui/base/l10n/l10n_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <string> | 10 #include <string> |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // has to be added manually in GetAvailableLocales(). | 201 // has to be added manually in GetAvailableLocales(). |
202 if (LowerCaseEqualsASCII(locale_name.substr(0, 3), "es_")) | 202 if (LowerCaseEqualsASCII(locale_name.substr(0, 3), "es_")) |
203 return true; | 203 return true; |
204 for (size_t i = 0; i < arraysize(kDuplicateNames); ++i) { | 204 for (size_t i = 0; i < arraysize(kDuplicateNames); ++i) { |
205 if (base::strcasecmp(kDuplicateNames[i], locale_name.c_str()) == 0) | 205 if (base::strcasecmp(kDuplicateNames[i], locale_name.c_str()) == 0) |
206 return true; | 206 return true; |
207 } | 207 } |
208 return false; | 208 return false; |
209 } | 209 } |
210 | 210 |
211 bool IsLocaleNameTranslated(const char* locale, | |
212 const std::string& display_locale) { | |
213 string16 display_name = | |
214 l10n_util::GetDisplayNameForLocale(locale, display_locale, false); | |
215 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not | |
216 // uloc_getDisplayName returns the actual translation or the default | |
217 // value (locale code), we have to rely on this hack to tell whether | |
218 // the translation is available or not. If ICU doesn't have a translated | |
219 // name for this locale, GetDisplayNameForLocale will just return the | |
220 // locale code. | |
221 return !IsStringASCII(display_name) || UTF16ToASCII(display_name) != locale; | |
222 } | |
223 | |
224 // We added 30+ minimally populated locales with only a few entries | 211 // We added 30+ minimally populated locales with only a few entries |
225 // (exemplar character set, script, writing direction and its own | 212 // (exemplar character set, script, writing direction and its own |
226 // lanaguage name). These locales have to be distinguished from the | 213 // lanaguage name). These locales have to be distinguished from the |
227 // fully populated locales to which Chrome is localized. | 214 // fully populated locales to which Chrome is localized. |
228 bool IsLocalePartiallyPopulated(const std::string& locale_name) { | 215 bool IsLocalePartiallyPopulated(const std::string& locale_name) { |
229 // For partially populated locales, even the translation for "English" | 216 // For partially populated locales, even the translation for "English" |
230 // is not available. A more robust/elegant way to check is to add a special | 217 // is not available. A more robust/elegant way to check is to add a special |
231 // field (say, 'isPartial' to our version of ICU locale files) and | 218 // field (say, 'isPartial' to our version of ICU locale files) and |
232 // check its value, but this hack seems to work well. | 219 // check its value, but this hack seems to work well. |
233 return !IsLocaleNameTranslated("en", locale_name); | 220 return !l10n_util::IsLocaleNameTranslated("en", locale_name); |
234 } | 221 } |
235 | 222 |
236 #if !defined(OS_MACOSX) | 223 #if !defined(OS_MACOSX) |
237 bool IsLocaleAvailable(const std::string& locale) { | 224 bool IsLocaleAvailable(const std::string& locale) { |
238 // If locale has any illegal characters in it, we don't want to try to | 225 // If locale has any illegal characters in it, we don't want to try to |
239 // load it because it may be pointing outside the locale data file directory. | 226 // load it because it may be pointing outside the locale data file directory. |
240 if (!file_util::IsFilenameLegal(ASCIIToUTF16(locale))) | 227 if (!file_util::IsFilenameLegal(ASCIIToUTF16(locale))) |
241 return false; | 228 return false; |
242 | 229 |
243 // IsLocalePartiallyPopulated() can be called here for an early return w/o | 230 // IsLocalePartiallyPopulated() can be called here for an early return w/o |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } | 385 } |
399 }; | 386 }; |
400 | 387 |
401 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits > | 388 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits > |
402 g_available_locales = LAZY_INSTANCE_INITIALIZER; | 389 g_available_locales = LAZY_INSTANCE_INITIALIZER; |
403 | 390 |
404 } // namespace | 391 } // namespace |
405 | 392 |
406 namespace l10n_util { | 393 namespace l10n_util { |
407 | 394 |
| 395 bool IsLocaleNameTranslated(const std::string& locale, |
| 396 const std::string& display_locale) { |
| 397 string16 display_name = |
| 398 l10n_util::GetDisplayNameForLocale(locale, display_locale, false); |
| 399 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not |
| 400 // uloc_getDisplayName returns the actual translation or the default |
| 401 // value (locale code), we have to rely on this hack to tell whether |
| 402 // the translation is available or not. If ICU doesn't have a translated |
| 403 // name for this locale, GetDisplayNameForLocale will just return the |
| 404 // locale code. |
| 405 return !IsStringASCII(display_name) || UTF16ToASCII(display_name) != locale; |
| 406 } |
| 407 |
408 std::string GetApplicationLocale(const std::string& pref_locale) { | 408 std::string GetApplicationLocale(const std::string& pref_locale) { |
409 #if defined(OS_MACOSX) | 409 #if defined(OS_MACOSX) |
410 | 410 |
411 // Use any override (Cocoa for the browser), otherwise use the preference | 411 // Use any override (Cocoa for the browser), otherwise use the preference |
412 // passed to the function. | 412 // passed to the function. |
413 std::string app_locale = l10n_util::GetLocaleOverride(); | 413 std::string app_locale = l10n_util::GetLocaleOverride(); |
414 if (app_locale.empty()) | 414 if (app_locale.empty()) |
415 app_locale = pref_locale; | 415 app_locale = pref_locale; |
416 | 416 |
417 // The above should handle all of the cases Chrome normally hits, but for some | 417 // The above should handle all of the cases Chrome normally hits, but for some |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 } | 852 } |
853 | 853 |
854 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { | 854 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { |
855 int width = 0; | 855 int width = 0; |
856 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); | 856 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); |
857 DCHECK_GT(width, 0); | 857 DCHECK_GT(width, 0); |
858 return width; | 858 return width; |
859 } | 859 } |
860 | 860 |
861 } // namespace l10n_util | 861 } // namespace l10n_util |
OLD | NEW |