Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / l10n / l10n_util.cc
blob23ca7ce1af9ad7c66e2daac06e1bbe133d52316b
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"
7 #include <algorithm>
8 #include <cstdlib>
9 #include <iterator>
10 #include <string>
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"
37 #endif
39 #if defined(USE_GLIB)
40 #include <glib.h>
41 #endif
43 #if defined(OS_WIN)
44 #include "ui/base/l10n/l10n_util_win.h"
45 #endif // OS_WIN
47 namespace {
49 static const char* const kAcceptLanguageList[] = {
50 "af", // Afrikaans
51 "am", // Amharic
52 "ar", // Arabic
53 "az", // Azerbaijani
54 "be", // Belarusian
55 "bg", // Bulgarian
56 "bh", // Bihari
57 "bn", // Bengali
58 "br", // Breton
59 "bs", // Bosnian
60 "ca", // Catalan
61 "ckb", // Kurdish (Arabci), Sorani
62 "co", // Corsican
63 "cs", // Czech
64 "cy", // Welsh
65 "da", // Danish
66 "de", // German
67 "de-AT", // German (Austria)
68 "de-CH", // German (Switzerland)
69 "de-DE", // German (Germany)
70 "de-LI", // German (Liechtenstein)
71 "el", // Greek
72 "en", // English
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)
79 "eo", // Esperanto
80 // TODO(jungshik) : Do we want to list all es-Foo for Latin-American
81 // Spanish speaking countries?
82 "es", // Spanish
83 "es-419", // Spanish (Latin America)
84 "et", // Estonian
85 "eu", // Basque
86 "fa", // Persian
87 "fi", // Finnish
88 "fil", // Filipino
89 "fo", // Faroese
90 "fr", // French
91 "fr-CA", // French (Canada)
92 "fr-CH", // French (Switzerland)
93 "fr-FR", // French (France)
94 "fy", // Frisian
95 "ga", // Irish
96 "gd", // Scots Gaelic
97 "gl", // Galician
98 "gn", // Guarani
99 "gu", // Gujarati
100 "ha", // Hausa
101 "haw", // Hawaiian
102 "he", // Hebrew
103 "hi", // Hindi
104 "hr", // Croatian
105 "hu", // Hungarian
106 "hy", // Armenian
107 "ia", // Interlingua
108 "id", // Indonesian
109 "is", // Icelandic
110 "it", // Italian
111 "it-CH", // Italian (Switzerland)
112 "it-IT", // Italian (Italy)
113 "ja", // Japanese
114 "jw", // Javanese
115 "ka", // Georgian
116 "kk", // Kazakh
117 "km", // Cambodian
118 "kn", // Kannada
119 "ko", // Korean
120 "ku", // Kurdish
121 "ky", // Kyrgyz
122 "la", // Latin
123 "ln", // Lingala
124 "lo", // Laothian
125 "lt", // Lithuanian
126 "lv", // Latvian
127 "mk", // Macedonian
128 "ml", // Malayalam
129 "mn", // Mongolian
130 "mo", // Moldavian
131 "mr", // Marathi
132 "ms", // Malay
133 "mt", // Maltese
134 "nb", // Norwegian (Bokmal)
135 "ne", // Nepali
136 "nl", // Dutch
137 "nn", // Norwegian (Nynorsk)
138 "no", // Norwegian
139 "oc", // Occitan
140 "om", // Oromo
141 "or", // Oriya
142 "pa", // Punjabi
143 "pl", // Polish
144 "ps", // Pashto
145 "pt", // Portuguese
146 "pt-BR", // Portuguese (Brazil)
147 "pt-PT", // Portuguese (Portugal)
148 "qu", // Quechua
149 "rm", // Romansh
150 "ro", // Romanian
151 "ru", // Russian
152 "sd", // Sindhi
153 "sh", // Serbo-Croatian
154 "si", // Sinhalese
155 "sk", // Slovak
156 "sl", // Slovenian
157 "sn", // Shona
158 "so", // Somali
159 "sq", // Albanian
160 "sr", // Serbian
161 "st", // Sesotho
162 "su", // Sundanese
163 "sv", // Swedish
164 "sw", // Swahili
165 "ta", // Tamil
166 "te", // Telugu
167 "tg", // Tajik
168 "th", // Thai
169 "ti", // Tigrinya
170 "tk", // Turkmen
171 "to", // Tonga
172 "tr", // Turkish
173 "tt", // Tatar
174 "tw", // Twi
175 "ug", // Uighur
176 "uk", // Ukrainian
177 "ur", // Urdu
178 "uz", // Uzbek
179 "vi", // Vietnamese
180 "xh", // Xhosa
181 "yi", // Yiddish
182 "yo", // Yoruba
183 "zh", // Chinese
184 "zh-CN", // Chinese (Simplified)
185 "zh-TW", // Chinese (Traditional)
186 "zu", // Zulu
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[] = {
192 "en",
193 "en_001",
194 "pt", // pt-BR and pt-PT are used.
195 "zh",
196 "zh_hans_cn",
197 "zh_hant_hk",
198 "zh_hant_mo",
199 "zh_hans_sg",
200 "zh_hant_tw"
203 // Skip all the es_Foo other than es_419 for now.
204 if (StartsWithASCII(locale_name, "es_", false))
205 return !EndsWith(locale_name, "419", true);
207 for (size_t i = 0; i < arraysize(kDuplicateNames); ++i) {
208 if (base::strcasecmp(kDuplicateNames[i], locale_name.c_str()) == 0)
209 return true;
211 return false;
214 // We added 30+ minimally populated locales with only a few entries
215 // (exemplar character set, script, writing direction and its own
216 // lanaguage name). These locales have to be distinguished from the
217 // fully populated locales to which Chrome is localized.
218 bool IsLocalePartiallyPopulated(const std::string& locale_name) {
219 // For partially populated locales, even the translation for "English"
220 // is not available. A more robust/elegant way to check is to add a special
221 // field (say, 'isPartial' to our version of ICU locale files) and
222 // check its value, but this hack seems to work well.
223 return !l10n_util::IsLocaleNameTranslated("en", locale_name);
226 #if !defined(OS_MACOSX)
227 bool IsLocaleAvailable(const std::string& locale) {
228 // If locale has any illegal characters in it, we don't want to try to
229 // load it because it may be pointing outside the locale data file directory.
230 if (!base::i18n::IsFilenameLegal(base::ASCIIToUTF16(locale)))
231 return false;
233 // IsLocalePartiallyPopulated() can be called here for an early return w/o
234 // checking the resource availability below. It'd help when Chrome is run
235 // under a system locale Chrome is not localized to (e.g.Farsi on Linux),
236 // but it'd slow down the start up time a little bit for locales Chrome is
237 // localized to. So, we don't call it here.
238 if (!l10n_util::IsLocaleSupportedByOS(locale))
239 return false;
241 // If the ResourceBundle is not yet initialized, return false to avoid the
242 // CHECK failure in ResourceBundle::GetSharedInstance().
243 if (!ResourceBundle::HasSharedInstance())
244 return false;
246 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function
247 // so that this can be invoked without initializing the global instance.
248 // See crbug.com/230432: CHECK failure in GetUserDataDir().
249 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale);
251 #endif
253 // On Linux, the text layout engine Pango determines paragraph directionality
254 // by looking at the first strongly-directional character in the text. This
255 // means text such as "Google Chrome foo bar..." will be layed out LTR even
256 // if "foo bar" is RTL. So this function prepends the necessary RLM in such
257 // cases.
258 void AdjustParagraphDirectionality(base::string16* paragraph) {
259 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
260 if (base::i18n::IsRTL() &&
261 base::i18n::StringContainsStrongRTLChars(*paragraph)) {
262 paragraph->insert(0, 1,
263 static_cast<base::char16>(base::i18n::kRightToLeftMark));
265 #endif
268 struct AvailableLocalesTraits
269 : base::DefaultLazyInstanceTraits<std::vector<std::string> > {
270 static std::vector<std::string>* New(void* instance) {
271 std::vector<std::string>* locales =
272 base::DefaultLazyInstanceTraits<std::vector<std::string> >::New(
273 instance);
274 int num_locales = uloc_countAvailable();
275 for (int i = 0; i < num_locales; ++i) {
276 std::string locale_name = uloc_getAvailable(i);
277 // Filter out the names that have aliases.
278 if (IsDuplicateName(locale_name))
279 continue;
280 // Filter out locales for which we have only partially populated data
281 // and to which Chrome is not localized.
282 if (IsLocalePartiallyPopulated(locale_name))
283 continue;
284 if (!l10n_util::IsLocaleSupportedByOS(locale_name))
285 continue;
286 // Normalize underscores to hyphens because that's what our locale files
287 // use.
288 std::replace(locale_name.begin(), locale_name.end(), '_', '-');
290 // Map the Chinese locale names over to zh-CN and zh-TW.
291 if (LowerCaseEqualsASCII(locale_name, "zh-hans")) {
292 locale_name = "zh-CN";
293 } else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) {
294 locale_name = "zh-TW";
296 locales->push_back(locale_name);
299 return locales;
303 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits>
304 g_available_locales = LAZY_INSTANCE_INITIALIZER;
306 } // namespace
308 namespace l10n_util {
310 std::string GetLanguage(const std::string& locale) {
311 const std::string::size_type hyphen_pos = locale.find('-');
312 return std::string(locale, 0, hyphen_pos);
315 bool CheckAndResolveLocale(const std::string& locale,
316 std::string* resolved_locale) {
317 #if defined(OS_MACOSX)
318 NOTIMPLEMENTED();
319 return false;
320 #else
321 if (IsLocaleAvailable(locale)) {
322 *resolved_locale = locale;
323 return true;
326 // If there's a variant, skip over it so we can try without the region
327 // code. For example, ca_ES@valencia should cause us to try ca@valencia
328 // before ca.
329 std::string::size_type variant_pos = locale.find('@');
330 if (variant_pos != std::string::npos)
331 return false;
333 // If the locale matches language but not country, use that instead.
334 // TODO(jungshik) : Nothing is done about languages that Chrome
335 // does not support but available on Windows. We fall
336 // back to en-US in GetApplicationLocale so that it's a not critical,
337 // but we can do better.
338 const std::string lang(GetLanguage(locale));
339 if (lang.size() < locale.size()) {
340 std::string region(locale, lang.size() + 1);
341 std::string tmp_locale(lang);
342 // Map es-RR other than es-ES to es-419 (Chrome's Latin American
343 // Spanish locale).
344 if (LowerCaseEqualsASCII(lang, "es") &&
345 !LowerCaseEqualsASCII(region, "es")) {
346 tmp_locale.append("-419");
347 } else if (LowerCaseEqualsASCII(lang, "zh")) {
348 // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN.
349 if (LowerCaseEqualsASCII(region, "hk") ||
350 LowerCaseEqualsASCII(region, "mo")) { // Macao
351 tmp_locale.append("-TW");
352 } else {
353 tmp_locale.append("-CN");
355 } else if (LowerCaseEqualsASCII(lang, "en")) {
356 // Map Australian, Canadian, New Zealand and South African English
357 // to British English for now.
358 // TODO(jungshik): en-CA may have to change sides once
359 // we have OS locale separate from app locale (Chrome's UI language).
360 if (LowerCaseEqualsASCII(region, "au") ||
361 LowerCaseEqualsASCII(region, "ca") ||
362 LowerCaseEqualsASCII(region, "nz") ||
363 LowerCaseEqualsASCII(region, "za")) {
364 tmp_locale.append("-GB");
365 } else {
366 tmp_locale.append("-US");
369 if (IsLocaleAvailable(tmp_locale)) {
370 resolved_locale->swap(tmp_locale);
371 return true;
375 // Google updater uses no, tl, iw and en for our nb, fil, he, and en-US.
376 struct {
377 const char* source;
378 const char* dest;
379 } alias_map[] = {
380 {"no", "nb"},
381 {"tl", "fil"},
382 {"iw", "he"},
383 {"en", "en-US"},
386 for (size_t i = 0; i < arraysize(alias_map); ++i) {
387 if (LowerCaseEqualsASCII(lang, alias_map[i].source)) {
388 std::string tmp_locale(alias_map[i].dest);
389 if (IsLocaleAvailable(tmp_locale)) {
390 resolved_locale->swap(tmp_locale);
391 return true;
396 return false;
397 #endif
400 std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
401 #if defined(OS_MACOSX)
403 // Use any override (Cocoa for the browser), otherwise use the preference
404 // passed to the function.
405 std::string app_locale = l10n_util::GetLocaleOverride();
406 if (app_locale.empty())
407 app_locale = pref_locale;
409 // The above should handle all of the cases Chrome normally hits, but for some
410 // unit tests, we need something to fall back too.
411 if (app_locale.empty())
412 app_locale = "en-US";
414 return app_locale;
416 #else
418 std::string resolved_locale;
419 std::vector<std::string> candidates;
421 // We only use --lang and the app pref on Windows. On Linux, we only
422 // look at the LC_*/LANG environment variables. We do, however, pass --lang
423 // to renderer and plugin processes so they know what language the parent
424 // process decided to use.
426 #if defined(OS_WIN)
428 // First, try the preference value.
429 if (!pref_locale.empty())
430 candidates.push_back(base::i18n::GetCanonicalLocale(pref_locale));
432 // Next, try the overridden locale.
433 const std::vector<std::string>& languages = l10n_util::GetLocaleOverrides();
434 if (!languages.empty()) {
435 candidates.reserve(candidates.size() + languages.size());
436 std::transform(languages.begin(), languages.end(),
437 std::back_inserter(candidates),
438 &base::i18n::GetCanonicalLocale);
439 } else {
440 // If no override was set, defer to ICU
441 candidates.push_back(base::i18n::GetConfiguredLocale());
444 #elif defined(OS_ANDROID)
446 // On Android, query java.util.Locale for the default locale.
447 candidates.push_back(base::android::GetDefaultLocale());
449 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
451 // GLib implements correct environment variable parsing with
452 // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
453 // We used to use our custom parsing code along with ICU for this purpose.
454 // If we have a port that does not depend on GTK, we have to
455 // restore our custom code for that port.
456 const char* const* languages = g_get_language_names();
457 DCHECK(languages); // A valid pointer is guaranteed.
458 DCHECK(*languages); // At least one entry, "C", is guaranteed.
460 for (; *languages != NULL; ++languages) {
461 candidates.push_back(base::i18n::GetCanonicalLocale(*languages));
464 #else
466 // By default, use the application locale preference. This applies to ChromeOS
467 // and linux systems without glib.
468 if (!pref_locale.empty())
469 candidates.push_back(pref_locale);
471 #endif
473 std::vector<std::string>::const_iterator i = candidates.begin();
474 for (; i != candidates.end(); ++i) {
475 if (CheckAndResolveLocale(*i, &resolved_locale)) {
476 return resolved_locale;
480 // Fallback on en-US.
481 const std::string fallback_locale("en-US");
482 if (IsLocaleAvailable(fallback_locale)) {
483 return fallback_locale;
486 return std::string();
488 #endif
491 std::string GetApplicationLocale(const std::string& pref_locale,
492 bool set_icu_locale) {
493 const std::string locale = GetApplicationLocaleInternal(pref_locale);
494 if (set_icu_locale && !locale.empty())
495 base::i18n::SetICUDefaultLocale(locale);
496 return locale;
499 std::string GetApplicationLocale(const std::string& pref_locale) {
500 return GetApplicationLocale(pref_locale, true /* set_icu_locale */);
503 bool IsLocaleNameTranslated(const char* locale,
504 const std::string& display_locale) {
505 base::string16 display_name =
506 l10n_util::GetDisplayNameForLocale(locale, display_locale, false);
507 // Because ICU sets the error code to U_USING_DEFAULT_WARNING whether or not
508 // uloc_getDisplayName returns the actual translation or the default
509 // value (locale code), we have to rely on this hack to tell whether
510 // the translation is available or not. If ICU doesn't have a translated
511 // name for this locale, GetDisplayNameForLocale will just return the
512 // locale code.
513 return !base::IsStringASCII(display_name) ||
514 base::UTF16ToASCII(display_name) != locale;
517 base::string16 GetDisplayNameForLocale(const std::string& locale,
518 const std::string& display_locale,
519 bool is_for_ui) {
520 std::string locale_code = locale;
521 // Internally, we use the language code of zh-CN and zh-TW, but we want the
522 // display names to be Chinese (Simplified) and Chinese (Traditional) instead
523 // of Chinese (China) and Chinese (Taiwan).
524 // Translate uses "tl" (Tagalog) to mean "fil" (Filipino) until Google
525 // translate is changed to understand "fil". Make "tl" alias to "fil".
526 if (locale_code == "zh-CN")
527 locale_code = "zh-Hans";
528 else if (locale_code == "zh-TW")
529 locale_code = "zh-Hant";
530 else if (locale_code == "tl")
531 locale_code = "fil";
532 else if (locale_code == "mo")
533 locale_code = "ro-MD";
535 base::string16 display_name;
536 #if defined(OS_ANDROID)
537 // Use Java API to get locale display name so that we can remove most of
538 // the lang data from icu data to reduce binary size, except for zh-Hans and
539 // zh-Hant because the current Android Java API doesn't support scripts.
540 // TODO(wangxianzhu): remove the special handling of zh-Hans and zh-Hant once
541 // Android Java API supports scripts.
542 if (!StartsWithASCII(locale_code, "zh-Han", true)) {
543 display_name = GetDisplayNameForLocale(locale_code, display_locale);
544 } else
545 #endif
547 UErrorCode error = U_ZERO_ERROR;
548 const int kBufferSize = 1024;
550 int actual_size = uloc_getDisplayName(
551 locale_code.c_str(), display_locale.c_str(),
552 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error);
553 DCHECK(U_SUCCESS(error));
554 display_name.resize(actual_size);
557 // Add directional markup so parentheses are properly placed.
558 if (is_for_ui && base::i18n::IsRTL())
559 base::i18n::AdjustStringForLocaleDirection(&display_name);
560 return display_name;
563 base::string16 GetDisplayNameForCountry(const std::string& country_code,
564 const std::string& display_locale) {
565 return GetDisplayNameForLocale("_" + country_code, display_locale, false);
568 std::string NormalizeLocale(const std::string& locale) {
569 std::string normalized_locale(locale);
570 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_');
572 return normalized_locale;
575 void GetParentLocales(const std::string& current_locale,
576 std::vector<std::string>* parent_locales) {
577 std::string locale(NormalizeLocale(current_locale));
579 const int kNameCapacity = 256;
580 char parent[kNameCapacity];
581 base::strlcpy(parent, locale.c_str(), kNameCapacity);
582 parent_locales->push_back(parent);
583 UErrorCode err = U_ZERO_ERROR;
584 while (uloc_getParent(parent, parent, kNameCapacity, &err) > 0) {
585 if (U_FAILURE(err))
586 break;
587 parent_locales->push_back(parent);
591 bool IsValidLocaleSyntax(const std::string& locale) {
592 // Check that the length is plausible.
593 if (locale.size() < 2 || locale.size() >= ULOC_FULLNAME_CAPACITY)
594 return false;
596 // Strip off the part after an '@' sign, which might contain keywords,
597 // as in en_IE@currency=IEP or fr@collation=phonebook;calendar=islamic-civil.
598 // We don't validate that part much, just check that there's at least one
599 // equals sign in a plausible place. Normalize the prefix so that hyphens
600 // are changed to underscores.
601 std::string prefix = NormalizeLocale(locale);
602 size_t split_point = locale.find("@");
603 if (split_point != std::string::npos) {
604 std::string keywords = locale.substr(split_point + 1);
605 prefix = locale.substr(0, split_point);
607 size_t equals_loc = keywords.find("=");
608 if (equals_loc == std::string::npos ||
609 equals_loc < 1 || equals_loc > keywords.size() - 2)
610 return false;
613 // Check that all characters before the at-sign are alphanumeric or
614 // underscore.
615 for (size_t i = 0; i < prefix.size(); i++) {
616 char ch = prefix[i];
617 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_')
618 return false;
621 // Check that the initial token (before the first hyphen/underscore)
622 // is 1 - 3 alphabetical characters (a language tag).
623 for (size_t i = 0; i < prefix.size(); i++) {
624 char ch = prefix[i];
625 if (ch == '_') {
626 if (i < 1 || i > 3)
627 return false;
628 break;
630 if (!IsAsciiAlpha(ch))
631 return false;
634 // Check that the all tokens after the initial token are 1 - 8 characters.
635 // (Tokenize/StringTokenizer don't work here, they collapse multiple
636 // delimiters into one.)
637 int token_len = 0;
638 int token_index = 0;
639 for (size_t i = 0; i < prefix.size(); i++) {
640 if (prefix[i] != '_') {
641 token_len++;
642 continue;
645 if (token_index > 0 && (token_len < 1 || token_len > 8)) {
646 return false;
648 token_index++;
649 token_len = 0;
651 if (token_index == 0 && (token_len < 1 || token_len > 3)) {
652 return false;
653 } else if (token_len < 1 || token_len > 8) {
654 return false;
657 return true;
660 std::string GetStringUTF8(int message_id) {
661 return base::UTF16ToUTF8(GetStringUTF16(message_id));
664 base::string16 GetStringUTF16(int message_id) {
665 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
666 base::string16 str = rb.GetLocalizedString(message_id);
667 AdjustParagraphDirectionality(&str);
669 return str;
672 base::string16 GetStringFUTF16(int message_id,
673 const std::vector<base::string16>& replacements,
674 std::vector<size_t>* offsets) {
675 // TODO(tc): We could save a string copy if we got the raw string as
676 // a StringPiece and were able to call ReplaceStringPlaceholders with
677 // a StringPiece format string and base::string16 substitution strings. In
678 // practice, the strings should be relatively short.
679 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
680 const base::string16& format_string = rb.GetLocalizedString(message_id);
682 #ifndef NDEBUG
683 // Make sure every replacement string is being used, so we don't just
684 // silently fail to insert one. If |offsets| is non-NULL, then don't do this
685 // check as the code may simply want to find the placeholders rather than
686 // actually replacing them.
687 if (!offsets) {
688 std::string utf8_string = base::UTF16ToUTF8(format_string);
690 // $9 is the highest allowed placeholder.
691 for (size_t i = 0; i < 9; ++i) {
692 bool placeholder_should_exist = replacements.size() > i;
694 std::string placeholder =
695 base::StringPrintf("$%d", static_cast<int>(i + 1));
696 size_t pos = utf8_string.find(placeholder.c_str());
697 if (placeholder_should_exist) {
698 DCHECK_NE(std::string::npos, pos) <<
699 " Didn't find a " << placeholder << " placeholder in " <<
700 utf8_string;
701 } else {
702 DCHECK_EQ(std::string::npos, pos) <<
703 " Unexpectedly found a " << placeholder << " placeholder in " <<
704 utf8_string;
708 #endif
710 base::string16 formatted = ReplaceStringPlaceholders(
711 format_string, replacements, offsets);
712 AdjustParagraphDirectionality(&formatted);
714 return formatted;
717 std::string GetStringFUTF8(int message_id,
718 const base::string16& a) {
719 return base::UTF16ToUTF8(GetStringFUTF16(message_id, a));
722 std::string GetStringFUTF8(int message_id,
723 const base::string16& a,
724 const base::string16& b) {
725 return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b));
728 std::string GetStringFUTF8(int message_id,
729 const base::string16& a,
730 const base::string16& b,
731 const base::string16& c) {
732 return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b, c));
735 std::string GetStringFUTF8(int message_id,
736 const base::string16& a,
737 const base::string16& b,
738 const base::string16& c,
739 const base::string16& d) {
740 return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b, c, d));
743 base::string16 GetStringFUTF16(int message_id,
744 const base::string16& a) {
745 std::vector<base::string16> replacements;
746 replacements.push_back(a);
747 return GetStringFUTF16(message_id, replacements, NULL);
750 base::string16 GetStringFUTF16(int message_id,
751 const base::string16& a,
752 const base::string16& b) {
753 return GetStringFUTF16(message_id, a, b, NULL);
756 base::string16 GetStringFUTF16(int message_id,
757 const base::string16& a,
758 const base::string16& b,
759 const base::string16& c) {
760 std::vector<base::string16> replacements;
761 replacements.push_back(a);
762 replacements.push_back(b);
763 replacements.push_back(c);
764 return GetStringFUTF16(message_id, replacements, NULL);
767 base::string16 GetStringFUTF16(int message_id,
768 const base::string16& a,
769 const base::string16& b,
770 const base::string16& c,
771 const base::string16& d) {
772 std::vector<base::string16> replacements;
773 replacements.push_back(a);
774 replacements.push_back(b);
775 replacements.push_back(c);
776 replacements.push_back(d);
777 return GetStringFUTF16(message_id, replacements, NULL);
780 base::string16 GetStringFUTF16(int message_id,
781 const base::string16& a,
782 const base::string16& b,
783 const base::string16& c,
784 const base::string16& d,
785 const base::string16& e) {
786 std::vector<base::string16> replacements;
787 replacements.push_back(a);
788 replacements.push_back(b);
789 replacements.push_back(c);
790 replacements.push_back(d);
791 replacements.push_back(e);
792 return GetStringFUTF16(message_id, replacements, NULL);
795 base::string16 GetStringFUTF16(int message_id,
796 const base::string16& a,
797 size_t* offset) {
798 DCHECK(offset);
799 std::vector<size_t> offsets;
800 std::vector<base::string16> replacements;
801 replacements.push_back(a);
802 base::string16 result = GetStringFUTF16(message_id, replacements, &offsets);
803 DCHECK(offsets.size() == 1);
804 *offset = offsets[0];
805 return result;
808 base::string16 GetStringFUTF16(int message_id,
809 const base::string16& a,
810 const base::string16& b,
811 std::vector<size_t>* offsets) {
812 std::vector<base::string16> replacements;
813 replacements.push_back(a);
814 replacements.push_back(b);
815 return GetStringFUTF16(message_id, replacements, offsets);
818 base::string16 GetStringFUTF16Int(int message_id, int a) {
819 return GetStringFUTF16(message_id, base::UTF8ToUTF16(base::IntToString(a)));
822 base::string16 GetStringFUTF16Int(int message_id, int64 a) {
823 return GetStringFUTF16(message_id, base::UTF8ToUTF16(base::Int64ToString(a)));
826 base::string16 GetPluralStringFUTF16(int message_id, int number) {
827 base::string16 pattern = GetStringUTF16(message_id);
828 UErrorCode err = U_ZERO_ERROR;
829 icu::MessageFormat format(
830 icu::UnicodeString(FALSE, pattern.data(), pattern.length()), err);
831 icu::UnicodeString result_unistring;
832 FormatNumberInPlural(format, number, &result_unistring, &err);
833 int capacity = result_unistring.length() + 1;
834 DCHECK_GT(capacity, 1);
835 base::string16 result;
836 result_unistring.extract(
837 static_cast<UChar*>(WriteInto(&result, capacity)), capacity, err);
838 DCHECK(U_SUCCESS(err));
839 return result;
842 std::string GetPluralStringFUTF8(int message_id, int number) {
843 return base::UTF16ToUTF8(GetPluralStringFUTF16(message_id, number));
846 void SortStrings16(const std::string& locale,
847 std::vector<base::string16>* strings) {
848 SortVectorWithStringKey(locale, strings, false);
851 const std::vector<std::string>& GetAvailableLocales() {
852 return g_available_locales.Get();
855 void GetAcceptLanguagesForLocale(const std::string& display_locale,
856 std::vector<std::string>* locale_codes) {
857 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) {
858 if (!l10n_util::IsLocaleNameTranslated(kAcceptLanguageList[i],
859 display_locale))
860 // TODO(jungshik) : Put them at the of the list with language codes
861 // enclosed by brackets instead of skipping.
862 continue;
863 locale_codes->push_back(kAcceptLanguageList[i]);
867 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) {
868 int width = 0;
869 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width);
870 DCHECK_GT(width, 0);
871 return width;
874 const char* const* GetAcceptLanguageListForTesting() {
875 return kAcceptLanguageList;
878 size_t GetAcceptLanguageListSizeForTesting() {
879 return arraysize(kAcceptLanguageList);
882 } // namespace l10n_util