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