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/files/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/strings/string_number_conversions.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h"
24 #include "base/strings/sys_string_conversions.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "build/build_config.h"
27 #include "third_party/icu/source/common/unicode/rbbi.h"
28 #include "third_party/icu/source/common/unicode/uloc.h"
29 #include "ui/base/l10n/l10n_util_collator.h"
30 #include "ui/base/l10n/l10n_util_plurals.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/base/ui_base_paths.h"
34 #if defined(OS_ANDROID)
35 #include "base/android/locale_utils.h"
36 #include "ui/base/l10n/l10n_util_android.h"
44 #include "ui/base/l10n/l10n_util_win.h"
49 static const char* const kAcceptLanguageList
[] = {
61 "ckb", // Kurdish (Arabci), Sorani
67 "de-AT", // German (Austria)
68 "de-CH", // German (Switzerland)
69 "de-DE", // German (Germany)
70 "de-LI", // German (Liechtenstein)
73 "en-AU", // English (Australia)
74 "en-CA", // English (Canada)
75 "en-GB", // English (UK)
76 "en-NZ", // English (New Zealand)
77 "en-US", // English (US)
78 "en-ZA", // English (South Africa)
80 // TODO(jungshik) : Do we want to list all es-Foo for Latin-American
81 // Spanish speaking countries?
83 "es-419", // Spanish (Latin America)
91 "fr-CA", // French (Canada)
92 "fr-CH", // French (Switzerland)
93 "fr-FR", // French (France)
111 "it-CH", // Italian (Switzerland)
112 "it-IT", // Italian (Italy)
134 "nb", // Norwegian (Bokmal)
137 "nn", // Norwegian (Nynorsk)
146 "pt-BR", // Portuguese (Brazil)
147 "pt-PT", // Portuguese (Portugal)
153 "sh", // Serbo-Croatian
184 "zh-CN", // Chinese (Simplified)
185 "zh-TW", // Chinese (Traditional)
189 // Returns true if |locale_name| has an alias in the ICU data file.
190 bool IsDuplicateName(const std::string
& locale_name
) {
191 static const char* const kDuplicateNames
[] = {
194 "pt", // pt-BR and pt-PT are used.
203 // Skip all the es_Foo other than es_419 for now.
204 if (base::StartsWith(locale_name
, "es_",
205 base::CompareCase::INSENSITIVE_ASCII
))
206 return !base::EndsWith(locale_name
, "419", base::CompareCase::SENSITIVE
);
208 for (size_t i
= 0; i
< arraysize(kDuplicateNames
); ++i
) {
209 if (base::EqualsCaseInsensitiveASCII(kDuplicateNames
[i
], locale_name
))
215 // We added 30+ minimally populated locales with only a few entries
216 // (exemplar character set, script, writing direction and its own
217 // lanaguage name). These locales have to be distinguished from the
218 // fully populated locales to which Chrome is localized.
219 bool IsLocalePartiallyPopulated(const std::string
& locale_name
) {
220 // For partially populated locales, even the translation for "English"
221 // is not available. A more robust/elegant way to check is to add a special
222 // field (say, 'isPartial' to our version of ICU locale files) and
223 // check its value, but this hack seems to work well.
224 return !l10n_util::IsLocaleNameTranslated("en", locale_name
);
227 #if !defined(OS_MACOSX)
228 bool IsLocaleAvailable(const std::string
& locale
) {
229 // If locale has any illegal characters in it, we don't want to try to
230 // load it because it may be pointing outside the locale data file directory.
231 if (!base::i18n::IsFilenameLegal(base::ASCIIToUTF16(locale
)))
234 // IsLocalePartiallyPopulated() can be called here for an early return w/o
235 // checking the resource availability below. It'd help when Chrome is run
236 // under a system locale Chrome is not localized to (e.g.Farsi on Linux),
237 // but it'd slow down the start up time a little bit for locales Chrome is
238 // localized to. So, we don't call it here.
239 if (!l10n_util::IsLocaleSupportedByOS(locale
))
242 // If the ResourceBundle is not yet initialized, return false to avoid the
243 // CHECK failure in ResourceBundle::GetSharedInstance().
244 if (!ResourceBundle::HasSharedInstance())
247 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function
248 // so that this can be invoked without initializing the global instance.
249 // See crbug.com/230432: CHECK failure in GetUserDataDir().
250 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale
);
254 // On Linux, the text layout engine Pango determines paragraph directionality
255 // by looking at the first strongly-directional character in the text. This
256 // means text such as "Google Chrome foo bar..." will be layed out LTR even
257 // if "foo bar" is RTL. So this function prepends the necessary RLM in such
259 void AdjustParagraphDirectionality(base::string16
* paragraph
) {
260 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
261 if (base::i18n::IsRTL() &&
262 base::i18n::StringContainsStrongRTLChars(*paragraph
)) {
263 paragraph
->insert(0, 1,
264 static_cast<base::char16
>(base::i18n::kRightToLeftMark
));
269 struct AvailableLocalesTraits
270 : base::DefaultLazyInstanceTraits
<std::vector
<std::string
> > {
271 static std::vector
<std::string
>* New(void* instance
) {
272 std::vector
<std::string
>* locales
=
273 base::DefaultLazyInstanceTraits
<std::vector
<std::string
> >::New(
275 int num_locales
= uloc_countAvailable();
276 for (int i
= 0; i
< num_locales
; ++i
) {
277 std::string locale_name
= uloc_getAvailable(i
);
278 // Filter out the names that have aliases.
279 if (IsDuplicateName(locale_name
))
281 // Filter out locales for which we have only partially populated data
282 // and to which Chrome is not localized.
283 if (IsLocalePartiallyPopulated(locale_name
))
285 if (!l10n_util::IsLocaleSupportedByOS(locale_name
))
287 // Normalize underscores to hyphens because that's what our locale files
289 std::replace(locale_name
.begin(), locale_name
.end(), '_', '-');
291 // Map the Chinese locale names over to zh-CN and zh-TW.
292 if (base::LowerCaseEqualsASCII(locale_name
, "zh-hans")) {
293 locale_name
= "zh-CN";
294 } else if (base::LowerCaseEqualsASCII(locale_name
, "zh-hant")) {
295 locale_name
= "zh-TW";
297 locales
->push_back(locale_name
);
304 base::LazyInstance
<std::vector
<std::string
>, AvailableLocalesTraits
>
305 g_available_locales
= LAZY_INSTANCE_INITIALIZER
;
309 namespace l10n_util
{
311 std::string
GetLanguage(const std::string
& locale
) {
312 const std::string::size_type hyphen_pos
= locale
.find('-');
313 return std::string(locale
, 0, hyphen_pos
);
316 bool CheckAndResolveLocale(const std::string
& locale
,
317 std::string
* resolved_locale
) {
318 #if defined(OS_MACOSX)
322 if (IsLocaleAvailable(locale
)) {
323 *resolved_locale
= locale
;
327 // If there's a variant, skip over it so we can try without the region
328 // code. For example, ca_ES@valencia should cause us to try ca@valencia
330 std::string::size_type variant_pos
= locale
.find('@');
331 if (variant_pos
!= std::string::npos
)
334 // If the locale matches language but not country, use that instead.
335 // TODO(jungshik) : Nothing is done about languages that Chrome
336 // does not support but available on Windows. We fall
337 // back to en-US in GetApplicationLocale so that it's a not critical,
338 // but we can do better.
339 const std::string
lang(GetLanguage(locale
));
340 if (lang
.size() < locale
.size()) {
341 std::string
region(locale
, lang
.size() + 1);
342 std::string
tmp_locale(lang
);
343 // Map es-RR other than es-ES to es-419 (Chrome's Latin American
345 if (base::LowerCaseEqualsASCII(lang
, "es") &&
346 !base::LowerCaseEqualsASCII(region
, "es")) {
347 tmp_locale
.append("-419");
348 } else if (base::LowerCaseEqualsASCII(lang
, "zh")) {
349 // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN.
350 if (base::LowerCaseEqualsASCII(region
, "hk") ||
351 base::LowerCaseEqualsASCII(region
, "mo")) { // Macao
352 tmp_locale
.append("-TW");
354 tmp_locale
.append("-CN");
356 } else if (base::LowerCaseEqualsASCII(lang
, "en")) {
357 // Map Australian, Canadian, New Zealand and South African English
358 // to British English for now.
359 // TODO(jungshik): en-CA may have to change sides once
360 // we have OS locale separate from app locale (Chrome's UI language).
361 if (base::LowerCaseEqualsASCII(region
, "au") ||
362 base::LowerCaseEqualsASCII(region
, "ca") ||
363 base::LowerCaseEqualsASCII(region
, "nz") ||
364 base::LowerCaseEqualsASCII(region
, "za")) {
365 tmp_locale
.append("-GB");
367 tmp_locale
.append("-US");
370 if (IsLocaleAvailable(tmp_locale
)) {
371 resolved_locale
->swap(tmp_locale
);
376 // Google updater uses no, tl, iw and en for our nb, fil, he, and en-US.
387 for (size_t i
= 0; i
< arraysize(alias_map
); ++i
) {
388 if (base::LowerCaseEqualsASCII(lang
, alias_map
[i
].source
)) {
389 std::string
tmp_locale(alias_map
[i
].dest
);
390 if (IsLocaleAvailable(tmp_locale
)) {
391 resolved_locale
->swap(tmp_locale
);
401 std::string
GetApplicationLocaleInternal(const std::string
& pref_locale
) {
402 #if defined(OS_MACOSX)
404 // Use any override (Cocoa for the browser), otherwise use the preference
405 // passed to the function.
406 std::string app_locale
= l10n_util::GetLocaleOverride();
407 if (app_locale
.empty())
408 app_locale
= pref_locale
;
410 // The above should handle all of the cases Chrome normally hits, but for some
411 // unit tests, we need something to fall back too.
412 if (app_locale
.empty())
413 app_locale
= "en-US";
419 std::string resolved_locale
;
420 std::vector
<std::string
> candidates
;
422 // We only use --lang and the app pref on Windows. On Linux, we only
423 // look at the LC_*/LANG environment variables. We do, however, pass --lang
424 // to renderer and plugin processes so they know what language the parent
425 // process decided to use.
429 // First, try the preference value.
430 if (!pref_locale
.empty())
431 candidates
.push_back(base::i18n::GetCanonicalLocale(pref_locale
));
433 // Next, try the overridden locale.
434 const std::vector
<std::string
>& languages
= l10n_util::GetLocaleOverrides();
435 if (!languages
.empty()) {
436 candidates
.reserve(candidates
.size() + languages
.size());
437 std::transform(languages
.begin(), languages
.end(),
438 std::back_inserter(candidates
),
439 &base::i18n::GetCanonicalLocale
);
441 // If no override was set, defer to ICU
442 candidates
.push_back(base::i18n::GetConfiguredLocale());
445 #elif defined(OS_ANDROID)
447 // On Android, query java.util.Locale for the default locale.
448 candidates
.push_back(base::android::GetDefaultLocale());
450 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
452 // GLib implements correct environment variable parsing with
453 // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
454 // We used to use our custom parsing code along with ICU for this purpose.
455 // If we have a port that does not depend on GTK, we have to
456 // restore our custom code for that port.
457 const char* const* languages
= g_get_language_names();
458 DCHECK(languages
); // A valid pointer is guaranteed.
459 DCHECK(*languages
); // At least one entry, "C", is guaranteed.
461 for (; *languages
!= NULL
; ++languages
) {
462 candidates
.push_back(base::i18n::GetCanonicalLocale(*languages
));
467 // By default, use the application locale preference. This applies to ChromeOS
468 // and linux systems without glib.
469 if (!pref_locale
.empty())
470 candidates
.push_back(pref_locale
);
474 std::vector
<std::string
>::const_iterator i
= candidates
.begin();
475 for (; i
!= candidates
.end(); ++i
) {
476 if (CheckAndResolveLocale(*i
, &resolved_locale
)) {
477 return resolved_locale
;
481 // Fallback on en-US.
482 const std::string
fallback_locale("en-US");
483 if (IsLocaleAvailable(fallback_locale
)) {
484 return fallback_locale
;
487 return std::string();
492 std::string
GetApplicationLocale(const std::string
& pref_locale
,
493 bool set_icu_locale
) {
494 const std::string locale
= GetApplicationLocaleInternal(pref_locale
);
495 if (set_icu_locale
&& !locale
.empty())
496 base::i18n::SetICUDefaultLocale(locale
);
500 std::string
GetApplicationLocale(const std::string
& pref_locale
) {
501 return GetApplicationLocale(pref_locale
, true /* set_icu_locale */);
504 bool IsLocaleNameTranslated(const char* locale
,
505 const std::string
& display_locale
) {
506 base::string16 display_name
=
507 l10n_util::GetDisplayNameForLocale(locale
, display_locale
, false);
508 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not
509 // uloc_getDisplayName returns the actual translation or the default
510 // value (locale code), we have to rely on this hack to tell whether
511 // the translation is available or not. If ICU doesn't have a translated
512 // name for this locale, GetDisplayNameForLocale will just return the
514 return !base::IsStringASCII(display_name
) ||
515 base::UTF16ToASCII(display_name
) != locale
;
518 base::string16
GetDisplayNameForLocale(const std::string
& locale
,
519 const std::string
& display_locale
,
521 std::string locale_code
= locale
;
522 // Internally, we use the language code of zh-CN and zh-TW, but we want the
523 // display names to be Chinese (Simplified) and Chinese (Traditional) instead
524 // of Chinese (China) and Chinese (Taiwan).
525 // Translate uses "tl" (Tagalog) to mean "fil" (Filipino) until Google
526 // translate is changed to understand "fil". Make "tl" alias to "fil".
527 if (locale_code
== "zh-CN")
528 locale_code
= "zh-Hans";
529 else if (locale_code
== "zh-TW")
530 locale_code
= "zh-Hant";
531 else if (locale_code
== "tl")
533 else if (locale_code
== "mo")
534 locale_code
= "ro-MD";
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 (!base::StartsWith(locale_code
, "zh-Han", base::CompareCase::SENSITIVE
)) {
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 base::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 (!base::IsAsciiAlpha(ch
) && !base::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 (!base::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
= base::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 base::string16
GetPluralStringFUTF16(int message_id
, int number
) {
828 base::string16 pattern
= GetStringUTF16(message_id
);
829 UErrorCode err
= U_ZERO_ERROR
;
830 icu::MessageFormat
format(
831 icu::UnicodeString(FALSE
, pattern
.data(), pattern
.length()), err
);
832 icu::UnicodeString result_unistring
;
833 FormatNumberInPlural(format
, number
, &result_unistring
, &err
);
834 int capacity
= result_unistring
.length() + 1;
835 DCHECK_GT(capacity
, 1);
836 base::string16 result
;
837 result_unistring
.extract(
838 static_cast<UChar
*>(base::WriteInto(&result
, capacity
)), capacity
, err
);
839 DCHECK(U_SUCCESS(err
));
843 std::string
GetPluralStringFUTF8(int message_id
, int number
) {
844 return base::UTF16ToUTF8(GetPluralStringFUTF16(message_id
, number
));
847 void SortStrings16(const std::string
& locale
,
848 std::vector
<base::string16
>* strings
) {
849 SortVectorWithStringKey(locale
, strings
, false);
852 const std::vector
<std::string
>& GetAvailableLocales() {
853 return g_available_locales
.Get();
856 void GetAcceptLanguagesForLocale(const std::string
& display_locale
,
857 std::vector
<std::string
>* locale_codes
) {
858 for (size_t i
= 0; i
< arraysize(kAcceptLanguageList
); ++i
) {
859 if (!l10n_util::IsLocaleNameTranslated(kAcceptLanguageList
[i
],
861 // TODO(jungshik) : Put them at the of the list with language codes
862 // enclosed by brackets instead of skipping.
864 locale_codes
->push_back(kAcceptLanguageList
[i
]);
868 int GetLocalizedContentsWidthInPixels(int pixel_resource_id
) {
870 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id
), &width
);
875 const char* const* GetAcceptLanguageListForTesting() {
876 return kAcceptLanguageList
;
879 size_t GetAcceptLanguageListSizeForTesting() {
880 return arraysize(kAcceptLanguageList
);
883 } // namespace l10n_util