This source file includes following definitions.
- IsDuplicateName
- IsLocalePartiallyPopulated
- IsLocaleAvailable
- AdjustParagraphDirectionality
- GetCanonicalLocale
- CheckAndResolveLocale
- GetApplicationLocale
- IsLocaleNameTranslated
- GetDisplayNameForLocale
- GetDisplayNameForCountry
- NormalizeLocale
- GetParentLocales
- IsValidLocaleSyntax
- GetStringUTF8
- GetStringUTF16
- GetStringFUTF16
- GetStringFUTF8
- GetStringFUTF8
- GetStringFUTF8
- GetStringFUTF8
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16
- GetStringFUTF16Int
- GetStringFUTF16Int
- GetPluralStringFUTF16
- GetPluralStringFUTF8
- SortStrings16
- GetAvailableLocales
- GetAcceptLanguagesForLocale
- GetLocalizedContentsWidthInPixels
#include "ui/base/l10n/l10n_util.h"
#include <algorithm>
#include <cstdlib>
#include <iterator>
#include <string>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/i18n/file_util_icu.h"
#include "base/i18n/rtl.h"
#include "base/i18n/string_compare.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "third_party/icu/source/common/unicode/rbbi.h"
#include "third_party/icu/source/common/unicode/uloc.h"
#include "ui/base/l10n/l10n_util_collator.h"
#include "ui/base/l10n/l10n_util_plurals.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#if defined(OS_ANDROID)
#include "ui/base/l10n/l10n_util_android.h"
#endif
#if defined(USE_GLIB)
#include <glib.h>
#endif
#if defined(OS_WIN)
#include "ui/base/l10n/l10n_util_win.h"
#endif
namespace {
static const char* const kAcceptLanguageList[] = {
"af",
"am",
"ar",
"az",
"be",
"bg",
"bh",
"bn",
"br",
"bs",
"ca",
"co",
"cs",
"cy",
"da",
"de",
"de-AT",
"de-CH",
"de-DE",
"el",
"en",
"en-AU",
"en-CA",
"en-GB",
"en-NZ",
"en-US",
"en-ZA",
"eo",
"es",
"es-419",
"et",
"eu",
"fa",
"fi",
"fil",
"fo",
"fr",
"fr-CA",
"fr-CH",
"fr-FR",
"fy",
"ga",
"gd",
"gl",
"gn",
"gu",
"ha",
"haw",
"he",
"hi",
"hr",
"hu",
"hy",
"ia",
"id",
"is",
"it",
"it-CH",
"it-IT",
"ja",
"jw",
"ka",
"kk",
"km",
"kn",
"ko",
"ku",
"ky",
"la",
"ln",
"lo",
"lt",
"lv",
"mk",
"ml",
"mn",
"mo",
"mr",
"ms",
"mt",
"nb",
"ne",
"nl",
"nn",
"no",
"oc",
"om",
"or",
"pa",
"pl",
"ps",
"pt",
"pt-BR",
"pt-PT",
"qu",
"rm",
"ro",
"ru",
"sd",
"sh",
"si",
"sk",
"sl",
"sn",
"so",
"sq",
"sr",
"st",
"su",
"sv",
"sw",
"ta",
"te",
"tg",
"th",
"ti",
"tk",
"to",
"tr",
"tt",
"tw",
"ug",
"uk",
"ur",
"uz",
"vi",
"xh",
"yi",
"yo",
"zh",
"zh-CN",
"zh-TW",
"zu",
};
bool IsDuplicateName(const std::string& locale_name) {
static const char* const kDuplicateNames[] = {
"en",
"pt",
"zh",
"zh_hans_cn",
"zh_hant_hk",
"zh_hant_mo",
"zh_hans_sg",
"zh_hant_tw"
};
if (LowerCaseEqualsASCII(locale_name.substr(0, 3), "es_"))
return true;
for (size_t i = 0; i < arraysize(kDuplicateNames); ++i) {
if (base::strcasecmp(kDuplicateNames[i], locale_name.c_str()) == 0)
return true;
}
return false;
}
bool IsLocalePartiallyPopulated(const std::string& locale_name) {
return !l10n_util::IsLocaleNameTranslated("en", locale_name);
}
#if !defined(OS_MACOSX)
bool IsLocaleAvailable(const std::string& locale) {
if (!file_util::IsFilenameLegal(base::ASCIIToUTF16(locale)))
return false;
if (!l10n_util::IsLocaleSupportedByOS(locale))
return false;
if (!ResourceBundle::HasSharedInstance())
return false;
return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale);
}
#endif
void AdjustParagraphDirectionality(base::string16* paragraph) {
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
if (base::i18n::IsRTL() &&
base::i18n::StringContainsStrongRTLChars(*paragraph)) {
paragraph->insert(0, 1,
static_cast<base::char16>(base::i18n::kRightToLeftMark));
}
#endif
}
#if defined(OS_WIN)
std::string GetCanonicalLocale(const std::string& locale) {
return base::i18n::GetCanonicalLocale(locale.c_str());
}
#endif
struct AvailableLocalesTraits
: base::DefaultLazyInstanceTraits<std::vector<std::string> > {
static std::vector<std::string>* New(void* instance) {
std::vector<std::string>* locales =
base::DefaultLazyInstanceTraits<std::vector<std::string> >::New(
instance);
int num_locales = uloc_countAvailable();
for (int i = 0; i < num_locales; ++i) {
std::string locale_name = uloc_getAvailable(i);
if (IsDuplicateName(locale_name))
continue;
if (IsLocalePartiallyPopulated(locale_name))
continue;
if (!l10n_util::IsLocaleSupportedByOS(locale_name))
continue;
std::replace(locale_name.begin(), locale_name.end(), '_', '-');
if (LowerCaseEqualsASCII(locale_name, "zh-hans")) {
locale_name = "zh-CN";
} else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) {
locale_name = "zh-TW";
}
locales->push_back(locale_name);
}
locales->push_back("es-419");
return locales;
}
};
base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits>
g_available_locales = LAZY_INSTANCE_INITIALIZER;
}
namespace l10n_util {
bool CheckAndResolveLocale(const std::string& locale,
std::string* resolved_locale) {
#if defined(OS_MACOSX)
NOTIMPLEMENTED();
return false;
#else
if (IsLocaleAvailable(locale)) {
*resolved_locale = locale;
return true;
}
std::string::size_type variant_pos = locale.find('@');
if (variant_pos != std::string::npos)
return false;
std::string::size_type hyphen_pos = locale.find('-');
std::string lang(locale, 0, hyphen_pos);
if (hyphen_pos != std::string::npos && hyphen_pos > 0) {
std::string region(locale, hyphen_pos + 1);
std::string tmp_locale(lang);
if (LowerCaseEqualsASCII(lang, "es") &&
!LowerCaseEqualsASCII(region, "es")) {
tmp_locale.append("-419");
} else if (LowerCaseEqualsASCII(lang, "zh")) {
if (LowerCaseEqualsASCII(region, "hk") ||
LowerCaseEqualsASCII(region, "mo")) {
tmp_locale.append("-TW");
} else {
tmp_locale.append("-CN");
}
} else if (LowerCaseEqualsASCII(lang, "en")) {
if (LowerCaseEqualsASCII(region, "au") ||
LowerCaseEqualsASCII(region, "ca") ||
LowerCaseEqualsASCII(region, "nz") ||
LowerCaseEqualsASCII(region, "za")) {
tmp_locale.append("-GB");
} else {
tmp_locale.append("-US");
}
}
if (IsLocaleAvailable(tmp_locale)) {
resolved_locale->swap(tmp_locale);
return true;
}
}
struct {
const char* source;
const char* dest;
} alias_map[] = {
{"no", "nb"},
{"tl", "fil"},
{"iw", "he"},
{"en", "en-US"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(alias_map); ++i) {
if (LowerCaseEqualsASCII(lang, alias_map[i].source)) {
std::string tmp_locale(alias_map[i].dest);
if (IsLocaleAvailable(tmp_locale)) {
resolved_locale->swap(tmp_locale);
return true;
}
}
}
return false;
#endif
}
std::string GetApplicationLocale(const std::string& pref_locale) {
#if defined(OS_MACOSX)
std::string app_locale = l10n_util::GetLocaleOverride();
if (app_locale.empty())
app_locale = pref_locale;
if (app_locale.empty())
app_locale = "en-US";
base::i18n::SetICUDefaultLocale(app_locale);
return app_locale;
#else
std::string resolved_locale;
std::vector<std::string> candidates;
#if defined(OS_WIN)
if (!pref_locale.empty())
candidates.push_back(GetCanonicalLocale(pref_locale));
const std::vector<std::string>& languages = l10n_util::GetLocaleOverrides();
if (!languages.empty()) {
candidates.reserve(candidates.size() + languages.size());
std::transform(languages.begin(), languages.end(),
std::back_inserter(candidates), &GetCanonicalLocale);
} else {
candidates.push_back(base::i18n::GetConfiguredLocale());
}
#elif defined(OS_ANDROID)
candidates.push_back(GetDefaultLocale());
#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
const char* const* languages = g_get_language_names();
DCHECK(languages);
DCHECK(*languages);
for (; *languages != NULL; ++languages) {
candidates.push_back(base::i18n::GetCanonicalLocale(*languages));
}
#else
if (!pref_locale.empty())
candidates.push_back(pref_locale);
#endif
std::vector<std::string>::const_iterator i = candidates.begin();
for (; i != candidates.end(); ++i) {
if (CheckAndResolveLocale(*i, &resolved_locale)) {
base::i18n::SetICUDefaultLocale(resolved_locale);
return resolved_locale;
}
}
const std::string fallback_locale("en-US");
if (IsLocaleAvailable(fallback_locale)) {
base::i18n::SetICUDefaultLocale(fallback_locale);
return fallback_locale;
}
return std::string();
#endif
}
bool IsLocaleNameTranslated(const char* locale,
const std::string& display_locale) {
base::string16 display_name =
l10n_util::GetDisplayNameForLocale(locale, display_locale, false);
return !IsStringASCII(display_name) ||
base::UTF16ToASCII(display_name) != locale;
}
base::string16 GetDisplayNameForLocale(const std::string& locale,
const std::string& display_locale,
bool is_for_ui) {
std::string locale_code = locale;
if (locale_code == "zh-CN")
locale_code = "zh-Hans";
else if (locale_code == "zh-TW")
locale_code = "zh-Hant";
base::string16 display_name;
#if defined(OS_ANDROID)
if (!StartsWithASCII(locale_code, "zh-Han", true)) {
display_name = GetDisplayNameForLocale(locale_code, display_locale);
} else
#endif
{
UErrorCode error = U_ZERO_ERROR;
const int kBufferSize = 1024;
int actual_size = uloc_getDisplayName(
locale_code.c_str(), display_locale.c_str(),
WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error);
DCHECK(U_SUCCESS(error));
display_name.resize(actual_size);
}
if (is_for_ui && base::i18n::IsRTL())
base::i18n::AdjustStringForLocaleDirection(&display_name);
return display_name;
}
base::string16 GetDisplayNameForCountry(const std::string& country_code,
const std::string& display_locale) {
return GetDisplayNameForLocale("_" + country_code, display_locale, false);
}
std::string NormalizeLocale(const std::string& locale) {
std::string normalized_locale(locale);
std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_');
return normalized_locale;
}
void GetParentLocales(const std::string& current_locale,
std::vector<std::string>* parent_locales) {
std::string locale(NormalizeLocale(current_locale));
const int kNameCapacity = 256;
char parent[kNameCapacity];
base::strlcpy(parent, locale.c_str(), kNameCapacity);
parent_locales->push_back(parent);
UErrorCode err = U_ZERO_ERROR;
while (uloc_getParent(parent, parent, kNameCapacity, &err) > 0) {
if (U_FAILURE(err))
break;
parent_locales->push_back(parent);
}
}
bool IsValidLocaleSyntax(const std::string& locale) {
if (locale.size() < 2 || locale.size() >= ULOC_FULLNAME_CAPACITY)
return false;
std::string prefix = NormalizeLocale(locale);
size_t split_point = locale.find("@");
if (split_point != std::string::npos) {
std::string keywords = locale.substr(split_point + 1);
prefix = locale.substr(0, split_point);
size_t equals_loc = keywords.find("=");
if (equals_loc == std::string::npos ||
equals_loc < 1 || equals_loc > keywords.size() - 2)
return false;
}
for (size_t i = 0; i < prefix.size(); i++) {
char ch = prefix[i];
if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_')
return false;
}
for (size_t i = 0; i < prefix.size(); i++) {
char ch = prefix[i];
if (ch == '_') {
if (i < 1 || i > 3)
return false;
break;
}
if (!IsAsciiAlpha(ch))
return false;
}
int token_len = 0;
int token_index = 0;
for (size_t i = 0; i < prefix.size(); i++) {
if (prefix[i] != '_') {
token_len++;
continue;
}
if (token_index > 0 && (token_len < 1 || token_len > 8)) {
return false;
}
token_index++;
token_len = 0;
}
if (token_index == 0 && (token_len < 1 || token_len > 3)) {
return false;
} else if (token_len < 1 || token_len > 8) {
return false;
}
return true;
}
std::string GetStringUTF8(int message_id) {
return base::UTF16ToUTF8(GetStringUTF16(message_id));
}
base::string16 GetStringUTF16(int message_id) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
base::string16 str = rb.GetLocalizedString(message_id);
AdjustParagraphDirectionality(&str);
return str;
}
base::string16 GetStringFUTF16(int message_id,
const std::vector<base::string16>& replacements,
std::vector<size_t>* offsets) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
const base::string16& format_string = rb.GetLocalizedString(message_id);
#ifndef NDEBUG
if (!offsets) {
std::string utf8_string = base::UTF16ToUTF8(format_string);
for (size_t i = 0; i < 9; ++i) {
bool placeholder_should_exist = replacements.size() > i;
std::string placeholder =
base::StringPrintf("$%d", static_cast<int>(i + 1));
size_t pos = utf8_string.find(placeholder.c_str());
if (placeholder_should_exist) {
DCHECK_NE(std::string::npos, pos) <<
" Didn't find a " << placeholder << " placeholder in " <<
utf8_string;
} else {
DCHECK_EQ(std::string::npos, pos) <<
" Unexpectedly found a " << placeholder << " placeholder in " <<
utf8_string;
}
}
}
#endif
base::string16 formatted = ReplaceStringPlaceholders(
format_string, replacements, offsets);
AdjustParagraphDirectionality(&formatted);
return formatted;
}
std::string GetStringFUTF8(int message_id,
const base::string16& a) {
return base::UTF16ToUTF8(GetStringFUTF16(message_id, a));
}
std::string GetStringFUTF8(int message_id,
const base::string16& a,
const base::string16& b) {
return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b));
}
std::string GetStringFUTF8(int message_id,
const base::string16& a,
const base::string16& b,
const base::string16& c) {
return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b, c));
}
std::string GetStringFUTF8(int message_id,
const base::string16& a,
const base::string16& b,
const base::string16& c,
const base::string16& d) {
return base::UTF16ToUTF8(GetStringFUTF16(message_id, a, b, c, d));
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a) {
std::vector<base::string16> replacements;
replacements.push_back(a);
return GetStringFUTF16(message_id, replacements, NULL);
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
const base::string16& b) {
return GetStringFUTF16(message_id, a, b, NULL);
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
const base::string16& b,
const base::string16& c) {
std::vector<base::string16> replacements;
replacements.push_back(a);
replacements.push_back(b);
replacements.push_back(c);
return GetStringFUTF16(message_id, replacements, NULL);
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
const base::string16& b,
const base::string16& c,
const base::string16& d) {
std::vector<base::string16> replacements;
replacements.push_back(a);
replacements.push_back(b);
replacements.push_back(c);
replacements.push_back(d);
return GetStringFUTF16(message_id, replacements, NULL);
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
const base::string16& b,
const base::string16& c,
const base::string16& d,
const base::string16& e) {
std::vector<base::string16> replacements;
replacements.push_back(a);
replacements.push_back(b);
replacements.push_back(c);
replacements.push_back(d);
replacements.push_back(e);
return GetStringFUTF16(message_id, replacements, NULL);
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
size_t* offset) {
DCHECK(offset);
std::vector<size_t> offsets;
std::vector<base::string16> replacements;
replacements.push_back(a);
base::string16 result = GetStringFUTF16(message_id, replacements, &offsets);
DCHECK(offsets.size() == 1);
*offset = offsets[0];
return result;
}
base::string16 GetStringFUTF16(int message_id,
const base::string16& a,
const base::string16& b,
std::vector<size_t>* offsets) {
std::vector<base::string16> replacements;
replacements.push_back(a);
replacements.push_back(b);
return GetStringFUTF16(message_id, replacements, offsets);
}
base::string16 GetStringFUTF16Int(int message_id, int a) {
return GetStringFUTF16(message_id, base::UTF8ToUTF16(base::IntToString(a)));
}
base::string16 GetStringFUTF16Int(int message_id, int64 a) {
return GetStringFUTF16(message_id, base::UTF8ToUTF16(base::Int64ToString(a)));
}
template <>
bool StringComparator<base::string16>::operator()(const base::string16& lhs,
const base::string16& rhs) {
if (!collator_)
return lhs < rhs;
return base::i18n::CompareString16WithCollator(collator_, lhs, rhs) ==
UCOL_LESS;
};
base::string16 GetPluralStringFUTF16(const std::vector<int>& message_ids,
int number) {
scoped_ptr<icu::PluralFormat> format = BuildPluralFormat(message_ids);
DCHECK(format);
UErrorCode err = U_ZERO_ERROR;
icu::UnicodeString result_files_string = format->format(number, err);
int capacity = result_files_string.length() + 1;
DCHECK_GT(capacity, 1);
base::string16 result;
result_files_string.extract(
static_cast<UChar*>(WriteInto(&result, capacity)), capacity, err);
DCHECK(U_SUCCESS(err));
return result;
}
std::string GetPluralStringFUTF8(const std::vector<int>& message_ids,
int number) {
return base::UTF16ToUTF8(GetPluralStringFUTF16(message_ids, number));
}
void SortStrings16(const std::string& locale,
std::vector<base::string16>* strings) {
SortVectorWithStringKey(locale, strings, false);
}
const std::vector<std::string>& GetAvailableLocales() {
return g_available_locales.Get();
}
void GetAcceptLanguagesForLocale(const std::string& display_locale,
std::vector<std::string>* locale_codes) {
for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) {
if (!l10n_util::IsLocaleNameTranslated(kAcceptLanguageList[i],
display_locale))
continue;
locale_codes->push_back(kAcceptLanguageList[i]);
}
}
int GetLocalizedContentsWidthInPixels(int pixel_resource_id) {
int width = 0;
base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width);
DCHECK_GT(width, 0);
return width;
}
}