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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 if (IsLocaleAvailable(fallback_locale)) { | 485 if (IsLocaleAvailable(fallback_locale)) { |
499 base::i18n::SetICUDefaultLocale(fallback_locale); | 486 base::i18n::SetICUDefaultLocale(fallback_locale); |
500 return fallback_locale; | 487 return fallback_locale; |
501 } | 488 } |
502 | 489 |
503 return std::string(); | 490 return std::string(); |
504 | 491 |
505 #endif | 492 #endif |
506 } | 493 } |
507 | 494 |
| 495 bool IsLocaleNameTranslated(const char* locale, |
| 496 const std::string& display_locale) { |
| 497 string16 display_name = |
| 498 l10n_util::GetDisplayNameForLocale(locale, display_locale, false); |
| 499 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not |
| 500 // uloc_getDisplayName returns the actual translation or the default |
| 501 // value (locale code), we have to rely on this hack to tell whether |
| 502 // the translation is available or not. If ICU doesn't have a translated |
| 503 // name for this locale, GetDisplayNameForLocale will just return the |
| 504 // locale code. |
| 505 return !IsStringASCII(display_name) || UTF16ToASCII(display_name) != locale; |
| 506 } |
| 507 |
508 string16 GetDisplayNameForLocale(const std::string& locale, | 508 string16 GetDisplayNameForLocale(const std::string& locale, |
509 const std::string& display_locale, | 509 const std::string& display_locale, |
510 bool is_for_ui) { | 510 bool is_for_ui) { |
511 std::string locale_code = locale; | 511 std::string locale_code = locale; |
512 // Internally, we use the language code of zh-CN and zh-TW, but we want the | 512 // Internally, we use the language code of zh-CN and zh-TW, but we want the |
513 // display names to be Chinese (Simplified) and Chinese (Traditional) instead | 513 // display names to be Chinese (Simplified) and Chinese (Traditional) instead |
514 // of Chinese (China) and Chinese (Taiwan). To do that, we pass zh-Hans | 514 // of Chinese (China) and Chinese (Taiwan). To do that, we pass zh-Hans |
515 // and zh-Hant to ICU. Even with this mapping, we'd get | 515 // and zh-Hant to ICU. Even with this mapping, we'd get |
516 // 'Chinese (Simplified Han)' and 'Chinese (Traditional Han)' in English and | 516 // 'Chinese (Simplified Han)' and 'Chinese (Traditional Han)' in English and |
517 // even longer results in other languages. Arguably, they're better than | 517 // even longer results in other languages. Arguably, they're better than |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 SortVectorWithStringKey(locale, strings, false); | 836 SortVectorWithStringKey(locale, strings, false); |
837 } | 837 } |
838 | 838 |
839 const std::vector<std::string>& GetAvailableLocales() { | 839 const std::vector<std::string>& GetAvailableLocales() { |
840 return g_available_locales.Get(); | 840 return g_available_locales.Get(); |
841 } | 841 } |
842 | 842 |
843 void GetAcceptLanguagesForLocale(const std::string& display_locale, | 843 void GetAcceptLanguagesForLocale(const std::string& display_locale, |
844 std::vector<std::string>* locale_codes) { | 844 std::vector<std::string>* locale_codes) { |
845 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { | 845 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { |
846 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) | 846 if (!l10n_util::IsLocaleNameTranslated(kAcceptLanguageList[i], |
| 847 display_locale)) |
847 // TODO(jungshik) : Put them at the of the list with language codes | 848 // TODO(jungshik) : Put them at the of the list with language codes |
848 // enclosed by brackets instead of skipping. | 849 // enclosed by brackets instead of skipping. |
849 continue; | 850 continue; |
850 locale_codes->push_back(kAcceptLanguageList[i]); | 851 locale_codes->push_back(kAcceptLanguageList[i]); |
851 } | 852 } |
852 } | 853 } |
853 | 854 |
854 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { | 855 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { |
855 int width = 0; | 856 int width = 0; |
856 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); | 857 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); |
857 DCHECK_GT(width, 0); | 858 DCHECK_GT(width, 0); |
858 return width; | 859 return width; |
859 } | 860 } |
860 | 861 |
861 } // namespace l10n_util | 862 } // namespace l10n_util |
OLD | NEW |