| 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 // If the ResourceBundle is not yet initialized, return false to avoid the | 239 // If the ResourceBundle is not yet initialized, return false to avoid the |
| 240 // CHECK failure in ResourceBundle::GetSharedInstance(). | 240 // CHECK failure in ResourceBundle::GetSharedInstance(). |
| 241 if (!ResourceBundle::HasSharedInstance()) | 241 if (!ResourceBundle::HasSharedInstance()) |
| 242 return false; | 242 return false; |
| 243 | 243 |
| 244 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function | 244 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function |
| 245 // so that this can be invoked without initializing the global instance. | 245 // so that this can be invoked without initializing the global instance. |
| 246 // See crbug.com/230432: CHECK failure in GetUserDataDir(). | 246 // See crbug.com/230432: CHECK failure in GetUserDataDir(). |
| 247 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale); | 247 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale); |
| 248 } | 248 } |
| 249 #endif |
| 250 |
| 251 // On Linux, the text layout engine Pango determines paragraph directionality |
| 252 // by looking at the first strongly-directional character in the text. This |
| 253 // means text such as "Google Chrome foo bar..." will be layed out LTR even |
| 254 // if "foo bar" is RTL. So this function prepends the necessary RLM in such |
| 255 // cases. |
| 256 void AdjustParagraphDirectionality(string16* paragraph) { |
| 257 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 258 if (base::i18n::IsRTL() && |
| 259 base::i18n::StringContainsStrongRTLChars(*paragraph)) { |
| 260 paragraph->insert(0, 1, static_cast<char16>(base::i18n::kRightToLeftMark)); |
| 261 } |
| 262 #endif |
| 263 } |
| 264 |
| 265 #if defined(OS_WIN) |
| 266 std::string GetCanonicalLocale(const std::string& locale) { |
| 267 return base::i18n::GetCanonicalLocale(locale.c_str()); |
| 268 } |
| 269 #endif |
| 270 |
| 271 struct AvailableLocalesTraits |
| 272 : base::DefaultLazyInstanceTraits<std::vector<std::string> > { |
| 273 static std::vector<std::string>* New(void* instance) { |
| 274 std::vector<std::string>* locales = |
| 275 base::DefaultLazyInstanceTraits<std::vector<std::string> >::New( |
| 276 instance); |
| 277 int num_locales = uloc_countAvailable(); |
| 278 for (int i = 0; i < num_locales; ++i) { |
| 279 std::string locale_name = uloc_getAvailable(i); |
| 280 // Filter out the names that have aliases. |
| 281 if (IsDuplicateName(locale_name)) |
| 282 continue; |
| 283 // Filter out locales for which we have only partially populated data |
| 284 // and to which Chrome is not localized. |
| 285 if (IsLocalePartiallyPopulated(locale_name)) |
| 286 continue; |
| 287 if (!l10n_util::IsLocaleSupportedByOS(locale_name)) |
| 288 continue; |
| 289 // Normalize underscores to hyphens because that's what our locale files |
| 290 // use. |
| 291 std::replace(locale_name.begin(), locale_name.end(), '_', '-'); |
| 292 |
| 293 // Map the Chinese locale names over to zh-CN and zh-TW. |
| 294 if (LowerCaseEqualsASCII(locale_name, "zh-hans")) { |
| 295 locale_name = "zh-CN"; |
| 296 } else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) { |
| 297 locale_name = "zh-TW"; |
| 298 } |
| 299 locales->push_back(locale_name); |
| 300 } |
| 301 |
| 302 // Manually add 'es-419' to the list. See the comment in IsDuplicateName(). |
| 303 locales->push_back("es-419"); |
| 304 return locales; |
| 305 } |
| 306 }; |
| 307 |
| 308 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits> |
| 309 g_available_locales = LAZY_INSTANCE_INITIALIZER; |
| 310 |
| 311 } // namespace |
| 312 |
| 313 namespace l10n_util { |
| 249 | 314 |
| 250 bool CheckAndResolveLocale(const std::string& locale, | 315 bool CheckAndResolveLocale(const std::string& locale, |
| 251 std::string* resolved_locale) { | 316 std::string* resolved_locale) { |
| 317 #if defined(OS_MACOSX) |
| 318 NOTIMPLEMENTED(); |
| 319 return false; |
| 320 #else |
| 252 if (IsLocaleAvailable(locale)) { | 321 if (IsLocaleAvailable(locale)) { |
| 253 *resolved_locale = locale; | 322 *resolved_locale = locale; |
| 254 return true; | 323 return true; |
| 255 } | 324 } |
| 256 | 325 |
| 257 // If there's a variant, skip over it so we can try without the region | 326 // If there's a variant, skip over it so we can try without the region |
| 258 // code. For example, ca_ES@valencia should cause us to try ca@valencia | 327 // code. For example, ca_ES@valencia should cause us to try ca@valencia |
| 259 // before ca. | 328 // before ca. |
| 260 std::string::size_type variant_pos = locale.find('@'); | 329 std::string::size_type variant_pos = locale.find('@'); |
| 261 if (variant_pos != std::string::npos) | 330 if (variant_pos != std::string::npos) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 if (LowerCaseEqualsASCII(lang, alias_map[i].source)) { | 388 if (LowerCaseEqualsASCII(lang, alias_map[i].source)) { |
| 320 std::string tmp_locale(alias_map[i].dest); | 389 std::string tmp_locale(alias_map[i].dest); |
| 321 if (IsLocaleAvailable(tmp_locale)) { | 390 if (IsLocaleAvailable(tmp_locale)) { |
| 322 resolved_locale->swap(tmp_locale); | 391 resolved_locale->swap(tmp_locale); |
| 323 return true; | 392 return true; |
| 324 } | 393 } |
| 325 } | 394 } |
| 326 } | 395 } |
| 327 | 396 |
| 328 return false; | 397 return false; |
| 329 } | |
| 330 #endif | |
| 331 | |
| 332 // On Linux, the text layout engine Pango determines paragraph directionality | |
| 333 // by looking at the first strongly-directional character in the text. This | |
| 334 // means text such as "Google Chrome foo bar..." will be layed out LTR even | |
| 335 // if "foo bar" is RTL. So this function prepends the necessary RLM in such | |
| 336 // cases. | |
| 337 void AdjustParagraphDirectionality(string16* paragraph) { | |
| 338 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 339 if (base::i18n::IsRTL() && | |
| 340 base::i18n::StringContainsStrongRTLChars(*paragraph)) { | |
| 341 paragraph->insert(0, 1, static_cast<char16>(base::i18n::kRightToLeftMark)); | |
| 342 } | |
| 343 #endif | 398 #endif |
| 344 } | 399 } |
| 345 | 400 |
| 346 #if defined(OS_WIN) | |
| 347 std::string GetCanonicalLocale(const std::string& locale) { | |
| 348 return base::i18n::GetCanonicalLocale(locale.c_str()); | |
| 349 } | |
| 350 #endif | |
| 351 | |
| 352 struct AvailableLocalesTraits : | |
| 353 base::DefaultLazyInstanceTraits<std::vector<std::string> > { | |
| 354 static std::vector<std::string>* New(void* instance) { | |
| 355 std::vector<std::string>* locales = | |
| 356 base::DefaultLazyInstanceTraits<std::vector<std::string> >::New( | |
| 357 instance); | |
| 358 int num_locales = uloc_countAvailable(); | |
| 359 for (int i = 0; i < num_locales; ++i) { | |
| 360 std::string locale_name = uloc_getAvailable(i); | |
| 361 // Filter out the names that have aliases. | |
| 362 if (IsDuplicateName(locale_name)) | |
| 363 continue; | |
| 364 // Filter out locales for which we have only partially populated data | |
| 365 // and to which Chrome is not localized. | |
| 366 if (IsLocalePartiallyPopulated(locale_name)) | |
| 367 continue; | |
| 368 if (!l10n_util::IsLocaleSupportedByOS(locale_name)) | |
| 369 continue; | |
| 370 // Normalize underscores to hyphens because that's what our locale files | |
| 371 // use. | |
| 372 std::replace(locale_name.begin(), locale_name.end(), '_', '-'); | |
| 373 | |
| 374 // Map the Chinese locale names over to zh-CN and zh-TW. | |
| 375 if (LowerCaseEqualsASCII(locale_name, "zh-hans")) { | |
| 376 locale_name = "zh-CN"; | |
| 377 } else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) { | |
| 378 locale_name = "zh-TW"; | |
| 379 } | |
| 380 locales->push_back(locale_name); | |
| 381 } | |
| 382 | |
| 383 // Manually add 'es-419' to the list. See the comment in IsDuplicateName(). | |
| 384 locales->push_back("es-419"); | |
| 385 return locales; | |
| 386 } | |
| 387 }; | |
| 388 | |
| 389 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits > | |
| 390 g_available_locales = LAZY_INSTANCE_INITIALIZER; | |
| 391 | |
| 392 } // namespace | |
| 393 | |
| 394 namespace l10n_util { | |
| 395 | |
| 396 std::string GetApplicationLocale(const std::string& pref_locale) { | 401 std::string GetApplicationLocale(const std::string& pref_locale) { |
| 397 #if defined(OS_MACOSX) | 402 #if defined(OS_MACOSX) |
| 398 | 403 |
| 399 // Use any override (Cocoa for the browser), otherwise use the preference | 404 // Use any override (Cocoa for the browser), otherwise use the preference |
| 400 // passed to the function. | 405 // passed to the function. |
| 401 std::string app_locale = l10n_util::GetLocaleOverride(); | 406 std::string app_locale = l10n_util::GetLocaleOverride(); |
| 402 if (app_locale.empty()) | 407 if (app_locale.empty()) |
| 403 app_locale = pref_locale; | 408 app_locale = pref_locale; |
| 404 | 409 |
| 405 // The above should handle all of the cases Chrome normally hits, but for some | 410 // The above should handle all of the cases Chrome normally hits, but for some |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 } | 880 } |
| 876 | 881 |
| 877 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { | 882 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { |
| 878 int width = 0; | 883 int width = 0; |
| 879 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); | 884 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); |
| 880 DCHECK_GT(width, 0); | 885 DCHECK_GT(width, 0); |
| 881 return width; | 886 return width; |
| 882 } | 887 } |
| 883 | 888 |
| 884 } // namespace l10n_util | 889 } // namespace l10n_util |
| OLD | NEW |