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
3 // found in the LICENSE file.
5 #include "ui/base/l10n/l10n_util.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/file_util.h"
15 #include "base/i18n/file_util_icu.h"
16 #include "base/i18n/rtl.h"
17 #include "base/i18n/string_compare.h"
18 #include "base/lazy_instance.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/path_service.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/strings/sys_string_conversions.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "build/build_config.h"
28 #include "third_party/icu/source/common/unicode/rbbi.h"
29 #include "third_party/icu/source/common/unicode/uloc.h"
30 #include "ui/base/l10n/l10n_util_collator.h"
31 #include "ui/base/l10n/l10n_util_plurals.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/base/ui_base_paths.h"
35 #if defined(OS_ANDROID)
36 #include "ui/base/l10n/l10n_util_android.h"
44 #include "ui/base/l10n/l10n_util_win.h"
49 static const char* const kAcceptLanguageList
[] = {
66 "de-AT", // German (Austria)
67 "de-CH", // German (Switzerland)
68 "de-DE", // German (Germany)
71 "en-AU", // English (Australia)
72 "en-CA", // English (Canada)
73 "en-GB", // English (UK)
74 "en-NZ", // English (New Zealand)
75 "en-US", // English (US)
76 "en-ZA", // English (South Africa)
78 // TODO(jungshik) : Do we want to list all es-Foo for Latin-American
79 // Spanish speaking countries?
81 "es-419", // Spanish (Latin America)
89 "fr-CA", // French (Canada)
90 "fr-CH", // French (Switzerland)
91 "fr-FR", // French (France)
109 "it-CH", // Italian (Switzerland)
110 "it-IT", // Italian (Italy)
132 "nb", // Norwegian (Bokmal)
135 "nn", // Norwegian (Nynorsk)
144 "pt-BR", // Portuguese (Brazil)
145 "pt-PT", // Portuguese (Portugal)
151 "sh", // Serbo-Croatian
182 "zh-CN", // Chinese (Simplified)
183 "zh-TW", // Chinese (Traditional)
187 // Returns true if |locale_name| has an alias in the ICU data file.
188 bool IsDuplicateName(const std::string
& locale_name
) {
189 static const char* const kDuplicateNames
[] = {
200 // Skip all 'es_RR'. Currently, we use 'es' for es-ES (Spanish in Spain).
201 // 'es-419' (Spanish in Latin America) is not available in ICU so that it
202 // has to be added manually in GetAvailableLocales().
203 if (LowerCaseEqualsASCII(locale_name
.substr(0, 3), "es_"))
205 for (size_t i
= 0; i
< arraysize(kDuplicateNames
); ++i
) {
206 if (base::strcasecmp(kDuplicateNames
[i
], locale_name
.c_str()) == 0)
212 // We added 30+ minimally populated locales with only a few entries
213 // (exemplar character set, script, writing direction and its own
214 // lanaguage name). These locales have to be distinguished from the
215 // fully populated locales to which Chrome is localized.
216 bool IsLocalePartiallyPopulated(const std::string
& locale_name
) {
217 // For partially populated locales, even the translation for "English"
218 // is not available. A more robust/elegant way to check is to add a special
219 // field (say, 'isPartial' to our version of ICU locale files) and
220 // check its value, but this hack seems to work well.
221 return !l10n_util::IsLocaleNameTranslated("en", locale_name
);
224 #if !defined(OS_MACOSX)
225 bool IsLocaleAvailable(const std::string
& locale
) {
226 // If locale has any illegal characters in it, we don't want to try to
227 // load it because it may be pointing outside the locale data file directory.
228 if (!base::i18n::IsFilenameLegal(base::ASCIIToUTF16(locale
)))
231 // IsLocalePartiallyPopulated() can be called here for an early return w/o
232 // checking the resource availability below. It'd help when Chrome is run
233 // under a system locale Chrome is not localized to (e.g.Farsi on Linux),
234 // but it'd slow down the start up time a little bit for locales Chrome is
235 // localized to. So, we don't call it here.
236 if (!l10n_util::IsLocaleSupportedByOS(locale
))
239 // If the ResourceBundle is not yet initialized, return false to avoid the
240 // CHECK failure in ResourceBundle::GetSharedInstance().
241 if (!ResourceBundle::HasSharedInstance())
244 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function
245 // so that this can be invoked without initializing the global instance.
246 // See crbug.com/230432: CHECK failure in GetUserDataDir().
247 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale
);
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
256 void AdjustParagraphDirectionality(base::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,
261 static_cast<base::char16
>(base::i18n::kRightToLeftMark
));
266 struct AvailableLocalesTraits
267 : base::DefaultLazyInstanceTraits
<std::vector
<std::string
> > {
268 static std::vector
<std::string
>* New(void* instance
) {
269 std::vector
<std::string
>* locales
=
270 base::DefaultLazyInstanceTraits
<std::vector
<std::string
> >::New(
272 int num_locales
= uloc_countAvailable();
273 for (int i
= 0; i
< num_locales
; ++i
) {
274 std::string locale_name
= uloc_getAvailable(i
);
275 // Filter out the names that have aliases.
276 if (IsDuplicateName(locale_name
))
278 // Filter out locales for which we have only partially populated data
279 // and to which Chrome is not localized.
280 if (IsLocalePartiallyPopulated(locale_name
))
282 if (!l10n_util::IsLocaleSupportedByOS(locale_name
))
284 // Normalize underscores to hyphens because that's what our locale files
286 std::replace(locale_name
.begin(), locale_name
.end(), '_', '-');
288 // Map the Chinese locale names over to zh-CN and zh-TW.
289 if (LowerCaseEqualsASCII(locale_name
, "zh-hans")) {
290 locale_name
= "zh-CN";
291 } else if (LowerCaseEqualsASCII(locale_name
, "zh-hant")) {
292 locale_name
= "zh-TW";
294 locales
->push_back(locale_name
);
297 // Manually add 'es-419' to the list. See the comment in IsDuplicateName().
298 locales
->push_back("es-419");
303 base::LazyInstance
<std::vector
<std::string
>, AvailableLocalesTraits
>
304 g_available_locales
= LAZY_INSTANCE_INITIALIZER
;
308 namespace l10n_util
{
310 std::string
GetCanonicalLocale(const std::string
& locale
) {
311 return base::i18n::GetCanonicalLocale(locale
.c_str());
314 std::string
GetLanguage(const std::string
& locale
) {
315 const std::string::size_type hyphen_pos
= locale
.find('-');
316 return std::string(locale
, 0, hyphen_pos
);
319 bool CheckAndResolveLocale(const std::string
& locale
,
320 std::string
* resolved_locale
) {
321 #if defined(OS_MACOSX)
325 if (IsLocaleAvailable(locale
)) {
326 *resolved_locale
= locale
;
330 // If there's a variant, skip over it so we can try without the region
331 // code. For example, ca_ES@valencia should cause us to try ca@valencia
333 std::string::size_type variant_pos
= locale
.find('@');
334 if (variant_pos
!= std::string::npos
)
337 // If the locale matches language but not country, use that instead.
338 // TODO(jungshik) : Nothing is done about languages that Chrome
339 // does not support but available on Windows. We fall
340 // back to en-US in GetApplicationLocale so that it's a not critical,
341 // but we can do better.
342 const std::string
lang(GetLanguage(locale
));
343 if (lang
.size() < locale
.size()) {
344 std::string
region(locale
, lang
.size() + 1);
345 std::string
tmp_locale(lang
);
346 // Map es-RR other than es-ES to es-419 (Chrome's Latin American
348 if (LowerCaseEqualsASCII(lang
, "es") &&
349 !LowerCaseEqualsASCII(region
, "es")) {
350 tmp_locale
.append("-419");
351 } else if (LowerCaseEqualsASCII(lang
, "zh")) {
352 // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN.
353 if (LowerCaseEqualsASCII(region
, "hk") ||
354 LowerCaseEqualsASCII(region
, "mo")) { // Macao
355 tmp_locale
.append("-TW");
357 tmp_locale
.append("-CN");
359 } else if (LowerCaseEqualsASCII(lang
, "en")) {
360 // Map Australian, Canadian, New Zealand and South African English
361 // to British English for now.
362 // TODO(jungshik): en-CA may have to change sides once
363 // we have OS locale separate from app locale (Chrome's UI language).
364 if (LowerCaseEqualsASCII(region
, "au") ||
365 LowerCaseEqualsASCII(region
, "ca") ||
366 LowerCaseEqualsASCII(region
, "nz") ||
367 LowerCaseEqualsASCII(region
, "za")) {
368 tmp_locale
.append("-GB");
370 tmp_locale
.append("-US");
373 if (IsLocaleAvailable(tmp_locale
)) {
374 resolved_locale
->swap(tmp_locale
);
379 // Google updater uses no, tl, iw and en for our nb, fil, he, and en-US.
390 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(alias_map
); ++i
) {
391 if (LowerCaseEqualsASCII(lang
, alias_map
[i
].source
)) {
392 std::string
tmp_locale(alias_map
[i
].dest
);
393 if (IsLocaleAvailable(tmp_locale
)) {
394 resolved_locale
->swap(tmp_locale
);
404 std::string
GetApplicationLocaleInternal(const std::string
& pref_locale
) {
405 #if defined(OS_MACOSX)
407 // Use any override (Cocoa for the browser), otherwise use the preference
408 // passed to the function.
409 std::string app_locale
= l10n_util::GetLocaleOverride();
410 if (app_locale
.empty())
411 app_locale
= pref_locale
;
413 // The above should handle all of the cases Chrome normally hits, but for some
414 // unit tests, we need something to fall back too.
415 if (app_locale
.empty())
416 app_locale
= "en-US";
422 std::string resolved_locale
;
423 std::vector
<std::string
> candidates
;
425 // We only use --lang and the app pref on Windows. On Linux, we only
426 // look at the LC_*/LANG environment variables. We do, however, pass --lang
427 // to renderer and plugin processes so they know what language the parent
428 // process decided to use.
432 // First, try the preference value.
433 if (!pref_locale
.empty())
434 candidates
.push_back(GetCanonicalLocale(pref_locale
));
436 // Next, try the overridden locale.
437 const std::vector
<std::string
>& languages
= l10n_util::GetLocaleOverrides();
438 if (!languages
.empty()) {
439 candidates
.reserve(candidates
.size() + languages
.size());
440 std::transform(languages
.begin(), languages
.end(),
441 std::back_inserter(candidates
), &GetCanonicalLocale
);
443 // If no override was set, defer to ICU
444 candidates
.push_back(base::i18n::GetConfiguredLocale());
447 #elif defined(OS_ANDROID)
449 // On Android, query java.util.Locale for the default locale.
450 candidates
.push_back(GetDefaultLocale());
452 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
454 // GLib implements correct environment variable parsing with
455 // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
456 // We used to use our custom parsing code along with ICU for this purpose.
457 // If we have a port that does not depend on GTK, we have to
458 // restore our custom code for that port.
459 const char* const* languages
= g_get_language_names();
460 DCHECK(languages
); // A valid pointer is guaranteed.
461 DCHECK(*languages
); // At least one entry, "C", is guaranteed.
463 for (; *languages
!= NULL
; ++languages
) {
464 candidates
.push_back(base::i18n::GetCanonicalLocale(*languages
));
469 // By default, use the application locale preference. This applies to ChromeOS
470 // and linux systems without glib.
471 if (!pref_locale
.empty())
472 candidates
.push_back(pref_locale
);
476 std::vector
<std::string
>::const_iterator i
= candidates
.begin();
477 for (; i
!= candidates
.end(); ++i
) {
478 if (CheckAndResolveLocale(*i
, &resolved_locale
)) {
479 return resolved_locale
;
483 // Fallback on en-US.
484 const std::string
fallback_locale("en-US");
485 if (IsLocaleAvailable(fallback_locale
)) {
486 return fallback_locale
;
489 return std::string();
494 std::string
GetApplicationLocale(const std::string
& pref_locale
,
495 bool set_icu_locale
) {
496 const std::string locale
= GetApplicationLocaleInternal(pref_locale
);
497 if (set_icu_locale
&& !locale
.empty())
498 base::i18n::SetICUDefaultLocale(locale
);
502 std::string
GetApplicationLocale(const std::string
& pref_locale
) {
503 return GetApplicationLocale(pref_locale
, true /* set_icu_locale */);
506 bool IsLocaleNameTranslated(const char* locale
,
507 const std::string
& display_locale
) {
508 base::string16 display_name
=
509 l10n_util::GetDisplayNameForLocale(locale
, display_locale
, false);
510 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not
511 // uloc_getDisplayName returns the actual translation or the default
512 // value (locale code), we have to rely on this hack to tell whether
513 // the translation is available or not. If ICU doesn't have a translated
514 // name for this locale, GetDisplayNameForLocale will just return the
516 return !base::IsStringASCII(display_name
) ||
517 base::UTF16ToASCII(display_name
) != locale
;
520 base::string16
GetDisplayNameForLocale(const std::string
& locale
,
521 const std::string
& display_locale
,
523 std::string locale_code
= locale
;
524 // Internally, we use the language code of zh-CN and zh-TW, but we want the
525 // display names to be Chinese (Simplified) and Chinese (Traditional) instead
526 // of Chinese (China) and Chinese (Taiwan).
527 // Translate uses "tl" (Tagalog) to mean "fil" (Filipino) until Google
528 // translate is changed to understand "fil". Make "tl" alias to "fil".
529 if (locale_code
== "zh-CN")
530 locale_code
= "zh-Hans";
531 else if (locale_code
== "zh-TW")
532 locale_code
= "zh-Hant";
533 else if (locale_code
== "tl")
536 base::string16 display_name
;
537 #if defined(OS_ANDROID)
538 // Use Java API to get locale display name so that we can remove most of
539 // the lang data from icu data to reduce binary size, except for zh-Hans and
540 // zh-Hant because the current Android Java API doesn't support scripts.
541 // TODO(wangxianzhu): remove the special handling of zh-Hans and zh-Hant once
542 // Android Java API supports scripts.
543 if (!StartsWithASCII(locale_code
, "zh-Han", true)) {
544 display_name
= GetDisplayNameForLocale(locale_code
, display_locale
);
548 UErrorCode error
= U_ZERO_ERROR
;
549 const int kBufferSize
= 1024;
551 int actual_size
= uloc_getDisplayName(
552 locale_code
.c_str(), display_locale
.c_str(),
553 WriteInto(&display_name
, kBufferSize
), kBufferSize
- 1, &error
);
554 DCHECK(U_SUCCESS(error
));
555 display_name
.resize(actual_size
);
558 // Add directional markup so parentheses are properly placed.
559 if (is_for_ui
&& base::i18n::IsRTL())
560 base::i18n::AdjustStringForLocaleDirection(&display_name
);
564 base::string16
GetDisplayNameForCountry(const std::string
& country_code
,
565 const std::string
& display_locale
) {
566 return GetDisplayNameForLocale("_" + country_code
, display_locale
, false);
569 std::string
NormalizeLocale(const std::string
& locale
) {
570 std::string
normalized_locale(locale
);
571 std::replace(normalized_locale
.begin(), normalized_locale
.end(), '-', '_');
573 return normalized_locale
;
576 void GetParentLocales(const std::string
& current_locale
,
577 std::vector
<std::string
>* parent_locales
) {
578 std::string
locale(NormalizeLocale(current_locale
));
580 const int kNameCapacity
= 256;
581 char parent
[kNameCapacity
];
582 base::strlcpy(parent
, locale
.c_str(), kNameCapacity
);
583 parent_locales
->push_back(parent
);
584 UErrorCode err
= U_ZERO_ERROR
;
585 while (uloc_getParent(parent
, parent
, kNameCapacity
, &err
) > 0) {
588 parent_locales
->push_back(parent
);
592 bool IsValidLocaleSyntax(const std::string
& locale
) {
593 // Check that the length is plausible.
594 if (locale
.size() < 2 || locale
.size() >= ULOC_FULLNAME_CAPACITY
)
597 // Strip off the part after an '@' sign, which might contain keywords,
598 // as in en_IE@currency=IEP or fr@collation=phonebook;calendar=islamic-civil.
599 // We don't validate that part much, just check that there's at least one
600 // equals sign in a plausible place. Normalize the prefix so that hyphens
601 // are changed to underscores.
602 std::string prefix
= NormalizeLocale(locale
);
603 size_t split_point
= locale
.find("@");
604 if (split_point
!= std::string::npos
) {
605 std::string keywords
= locale
.substr(split_point
+ 1);
606 prefix
= locale
.substr(0, split_point
);
608 size_t equals_loc
= keywords
.find("=");
609 if (equals_loc
== std::string::npos
||
610 equals_loc
< 1 || equals_loc
> keywords
.size() - 2)
614 // Check that all characters before the at-sign are alphanumeric or
616 for (size_t i
= 0; i
< prefix
.size(); i
++) {
618 if (!IsAsciiAlpha(ch
) && !IsAsciiDigit(ch
) && ch
!= '_')
622 // Check that the initial token (before the first hyphen/underscore)
623 // is 1 - 3 alphabetical characters (a language tag).
624 for (size_t i
= 0; i
< prefix
.size(); i
++) {
631 if (!IsAsciiAlpha(ch
))
635 // Check that the all tokens after the initial token are 1 - 8 characters.
636 // (Tokenize/StringTokenizer don't work here, they collapse multiple
637 // delimiters into one.)
640 for (size_t i
= 0; i
< prefix
.size(); i
++) {
641 if (prefix
[i
] != '_') {
646 if (token_index
> 0 && (token_len
< 1 || token_len
> 8)) {
652 if (token_index
== 0 && (token_len
< 1 || token_len
> 3)) {
654 } else if (token_len
< 1 || token_len
> 8) {
661 std::string
GetStringUTF8(int message_id
) {
662 return base::UTF16ToUTF8(GetStringUTF16(message_id
));
665 base::string16
GetStringUTF16(int message_id
) {
666 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
667 base::string16 str
= rb
.GetLocalizedString(message_id
);
668 AdjustParagraphDirectionality(&str
);
673 base::string16
GetStringFUTF16(int message_id
,
674 const std::vector
<base::string16
>& replacements
,
675 std::vector
<size_t>* offsets
) {
676 // TODO(tc): We could save a string copy if we got the raw string as
677 // a StringPiece and were able to call ReplaceStringPlaceholders with
678 // a StringPiece format string and base::string16 substitution strings. In
679 // practice, the strings should be relatively short.
680 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
681 const base::string16
& format_string
= rb
.GetLocalizedString(message_id
);
684 // Make sure every replacement string is being used, so we don't just
685 // silently fail to insert one. If |offsets| is non-NULL, then don't do this
686 // check as the code may simply want to find the placeholders rather than
687 // actually replacing them.
689 std::string utf8_string
= base::UTF16ToUTF8(format_string
);
691 // $9 is the highest allowed placeholder.
692 for (size_t i
= 0; i
< 9; ++i
) {
693 bool placeholder_should_exist
= replacements
.size() > i
;
695 std::string placeholder
=
696 base::StringPrintf("$%d", static_cast<int>(i
+ 1));
697 size_t pos
= utf8_string
.find(placeholder
.c_str());
698 if (placeholder_should_exist
) {
699 DCHECK_NE(std::string::npos
, pos
) <<
700 " Didn't find a " << placeholder
<< " placeholder in " <<
703 DCHECK_EQ(std::string::npos
, pos
) <<
704 " Unexpectedly found a " << placeholder
<< " placeholder in " <<
711 base::string16 formatted
= ReplaceStringPlaceholders(
712 format_string
, replacements
, offsets
);
713 AdjustParagraphDirectionality(&formatted
);
718 std::string
GetStringFUTF8(int message_id
,
719 const base::string16
& a
) {
720 return base::UTF16ToUTF8(GetStringFUTF16(message_id
, a
));
723 std::string
GetStringFUTF8(int message_id
,
724 const base::string16
& a
,
725 const base::string16
& b
) {
726 return base::UTF16ToUTF8(GetStringFUTF16(message_id
, a
, b
));
729 std::string
GetStringFUTF8(int message_id
,
730 const base::string16
& a
,
731 const base::string16
& b
,
732 const base::string16
& c
) {
733 return base::UTF16ToUTF8(GetStringFUTF16(message_id
, a
, b
, c
));
736 std::string
GetStringFUTF8(int message_id
,
737 const base::string16
& a
,
738 const base::string16
& b
,
739 const base::string16
& c
,
740 const base::string16
& d
) {
741 return base::UTF16ToUTF8(GetStringFUTF16(message_id
, a
, b
, c
, d
));
744 base::string16
GetStringFUTF16(int message_id
,
745 const base::string16
& a
) {
746 std::vector
<base::string16
> replacements
;
747 replacements
.push_back(a
);
748 return GetStringFUTF16(message_id
, replacements
, NULL
);
751 base::string16
GetStringFUTF16(int message_id
,
752 const base::string16
& a
,
753 const base::string16
& b
) {
754 return GetStringFUTF16(message_id
, a
, b
, NULL
);
757 base::string16
GetStringFUTF16(int message_id
,
758 const base::string16
& a
,
759 const base::string16
& b
,
760 const base::string16
& c
) {
761 std::vector
<base::string16
> replacements
;
762 replacements
.push_back(a
);
763 replacements
.push_back(b
);
764 replacements
.push_back(c
);
765 return GetStringFUTF16(message_id
, replacements
, NULL
);
768 base::string16
GetStringFUTF16(int message_id
,
769 const base::string16
& a
,
770 const base::string16
& b
,
771 const base::string16
& c
,
772 const base::string16
& d
) {
773 std::vector
<base::string16
> replacements
;
774 replacements
.push_back(a
);
775 replacements
.push_back(b
);
776 replacements
.push_back(c
);
777 replacements
.push_back(d
);
778 return GetStringFUTF16(message_id
, replacements
, NULL
);
781 base::string16
GetStringFUTF16(int message_id
,
782 const base::string16
& a
,
783 const base::string16
& b
,
784 const base::string16
& c
,
785 const base::string16
& d
,
786 const base::string16
& e
) {
787 std::vector
<base::string16
> replacements
;
788 replacements
.push_back(a
);
789 replacements
.push_back(b
);
790 replacements
.push_back(c
);
791 replacements
.push_back(d
);
792 replacements
.push_back(e
);
793 return GetStringFUTF16(message_id
, replacements
, NULL
);
796 base::string16
GetStringFUTF16(int message_id
,
797 const base::string16
& a
,
800 std::vector
<size_t> offsets
;
801 std::vector
<base::string16
> replacements
;
802 replacements
.push_back(a
);
803 base::string16 result
= GetStringFUTF16(message_id
, replacements
, &offsets
);
804 DCHECK(offsets
.size() == 1);
805 *offset
= offsets
[0];
809 base::string16
GetStringFUTF16(int message_id
,
810 const base::string16
& a
,
811 const base::string16
& b
,
812 std::vector
<size_t>* offsets
) {
813 std::vector
<base::string16
> replacements
;
814 replacements
.push_back(a
);
815 replacements
.push_back(b
);
816 return GetStringFUTF16(message_id
, replacements
, offsets
);
819 base::string16
GetStringFUTF16Int(int message_id
, int a
) {
820 return GetStringFUTF16(message_id
, base::UTF8ToUTF16(base::IntToString(a
)));
823 base::string16
GetStringFUTF16Int(int message_id
, int64 a
) {
824 return GetStringFUTF16(message_id
, base::UTF8ToUTF16(base::Int64ToString(a
)));
827 // Specialization of operator() method for base::string16 version.
829 bool StringComparator
<base::string16
>::operator()(const base::string16
& lhs
,
830 const base::string16
& rhs
) {
831 // If we can not get collator instance for specified locale, just do simple
835 return base::i18n::CompareString16WithCollator(collator_
, lhs
, rhs
) ==
839 base::string16
GetPluralStringFUTF16(const std::vector
<int>& message_ids
,
841 scoped_ptr
<icu::PluralFormat
> format
= BuildPluralFormat(message_ids
);
844 UErrorCode err
= U_ZERO_ERROR
;
845 icu::UnicodeString result_files_string
= format
->format(number
, err
);
846 int capacity
= result_files_string
.length() + 1;
847 DCHECK_GT(capacity
, 1);
848 base::string16 result
;
849 result_files_string
.extract(
850 static_cast<UChar
*>(WriteInto(&result
, capacity
)), capacity
, err
);
851 DCHECK(U_SUCCESS(err
));
855 std::string
GetPluralStringFUTF8(const std::vector
<int>& message_ids
,
857 return base::UTF16ToUTF8(GetPluralStringFUTF16(message_ids
, number
));
860 void SortStrings16(const std::string
& locale
,
861 std::vector
<base::string16
>* strings
) {
862 SortVectorWithStringKey(locale
, strings
, false);
865 const std::vector
<std::string
>& GetAvailableLocales() {
866 return g_available_locales
.Get();
869 void GetAcceptLanguagesForLocale(const std::string
& display_locale
,
870 std::vector
<std::string
>* locale_codes
) {
871 for (size_t i
= 0; i
< arraysize(kAcceptLanguageList
); ++i
) {
872 if (!l10n_util::IsLocaleNameTranslated(kAcceptLanguageList
[i
],
874 // TODO(jungshik) : Put them at the of the list with language codes
875 // enclosed by brackets instead of skipping.
877 locale_codes
->push_back(kAcceptLanguageList
[i
]);
881 int GetLocalizedContentsWidthInPixels(int pixel_resource_id
) {
883 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id
), &width
);
888 const char* const* GetAcceptLanguageListForTesting() {
889 return kAcceptLanguageList
;
892 size_t GetAcceptLanguageListSizeForTesting() {
893 return arraysize(kAcceptLanguageList
);
896 } // namespace l10n_util