Commit 092b3741 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Clean up l10n_util.cc.

- Split GetApplicationLocaleInternal() so it's more readable.
- Use more C++11 features.
- Fit nits and lint errors.

Change-Id: Iff2e9b3fce4622891ebb39ace03c64d1e7603d2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1918163Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721912}
parent 26e4f728
...@@ -232,8 +232,9 @@ bool IsDuplicateName(const std::string& locale_name) { ...@@ -232,8 +232,9 @@ bool IsDuplicateName(const std::string& locale_name) {
// Skip all the es_Foo other than es_419 for now. // Skip all the es_Foo other than es_419 for now.
if (base::StartsWith(locale_name, "es_", if (base::StartsWith(locale_name, "es_",
base::CompareCase::INSENSITIVE_ASCII)) base::CompareCase::INSENSITIVE_ASCII)) {
return !base::EndsWith(locale_name, "419", base::CompareCase::SENSITIVE); return !base::EndsWith(locale_name, "419", base::CompareCase::SENSITIVE);
}
for (const char* duplicate_name : kDuplicateNames) { for (const char* duplicate_name : kDuplicateNames) {
if (base::EqualsCaseInsensitiveASCII(duplicate_name, locale_name)) if (base::EqualsCaseInsensitiveASCII(duplicate_name, locale_name))
return true; return true;
...@@ -339,10 +340,7 @@ std::string GetLanguage(const std::string& locale) { ...@@ -339,10 +340,7 @@ std::string GetLanguage(const std::string& locale) {
// and generic locale fallback based on ICU/CLDR. // and generic locale fallback based on ICU/CLDR.
bool CheckAndResolveLocale(const std::string& locale, bool CheckAndResolveLocale(const std::string& locale,
std::string* resolved_locale) { std::string* resolved_locale) {
#if defined(OS_MACOSX) #if !defined(OS_MACOSX)
NOTIMPLEMENTED();
return false;
#else
if (IsLocaleAvailable(locale)) { if (IsLocaleAvailable(locale)) {
*resolved_locale = locale; *resolved_locale = locale;
return true; return true;
...@@ -407,11 +405,11 @@ bool CheckAndResolveLocale(const std::string& locale, ...@@ -407,11 +405,11 @@ bool CheckAndResolveLocale(const std::string& locale,
struct { struct {
const char* source; const char* source;
const char* dest; const char* dest;
} alias_map[] = { } static constexpr kAliasMap[] = {
{"en", "en-US"}, {"iw", "he"}, {"no", "nb"}, {"en", "en-US"}, {"iw", "he"}, {"no", "nb"},
{"pt", "pt-BR"}, {"tl", "fil"}, {"zh", "zh-CN"}, {"pt", "pt-BR"}, {"tl", "fil"}, {"zh", "zh-CN"},
}; };
for (const auto& alias : alias_map) { for (const auto& alias : kAliasMap) {
if (base::LowerCaseEqualsASCII(lang, alias.source)) { if (base::LowerCaseEqualsASCII(lang, alias.source)) {
std::string tmp_locale(alias.dest); std::string tmp_locale(alias.dest);
if (IsLocaleAvailable(tmp_locale)) { if (IsLocaleAvailable(tmp_locale)) {
...@@ -420,14 +418,15 @@ bool CheckAndResolveLocale(const std::string& locale, ...@@ -420,14 +418,15 @@ bool CheckAndResolveLocale(const std::string& locale,
} }
} }
} }
#else
NOTIMPLEMENTED();
#endif // !defined(OS_MACOSX)
return false; return false;
#endif
} }
std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
std::string GetApplicationLocaleInternalMac(const std::string& pref_locale) {
// Use any override (Cocoa for the browser), otherwise use the preference // Use any override (Cocoa for the browser), otherwise use the preference
// passed to the function. // passed to the function.
std::string app_locale = l10n_util::GetLocaleOverride(); std::string app_locale = l10n_util::GetLocaleOverride();
...@@ -440,9 +439,11 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) { ...@@ -440,9 +439,11 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
app_locale = "en-US"; app_locale = "en-US";
return app_locale; return app_locale;
}
#endif
#else #if !defined(OS_MACOSX)
std::string GetApplicationLocaleInternalNonMac(const std::string& pref_locale) {
std::string resolved_locale; std::string resolved_locale;
std::vector<std::string> candidates; std::vector<std::string> candidates;
...@@ -452,7 +453,6 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) { ...@@ -452,7 +453,6 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
// process decided to use. // process decided to use.
#if defined(OS_WIN) #if defined(OS_WIN)
// First, try the preference value. // First, try the preference value.
if (!pref_locale.empty()) if (!pref_locale.empty())
candidates.push_back(base::i18n::GetCanonicalLocale(pref_locale)); candidates.push_back(base::i18n::GetCanonicalLocale(pref_locale));
...@@ -468,18 +468,14 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) { ...@@ -468,18 +468,14 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
// If no override was set, defer to ICU // If no override was set, defer to ICU
candidates.push_back(base::i18n::GetConfiguredLocale()); candidates.push_back(base::i18n::GetConfiguredLocale());
} }
#elif defined(OS_ANDROID) #elif defined(OS_ANDROID)
// Try pref_locale first. // Try pref_locale first.
if (!pref_locale.empty()) if (!pref_locale.empty())
candidates.push_back(base::i18n::GetCanonicalLocale(pref_locale)); candidates.push_back(base::i18n::GetCanonicalLocale(pref_locale));
// On Android, query java.util.Locale for the default locale. // On Android, query java.util.Locale for the default locale.
candidates.push_back(base::android::GetDefaultLocaleString()); candidates.push_back(base::android::GetDefaultLocaleString());
#elif defined(USE_GLIB) && !defined(OS_CHROMEOS) #elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
// GLib implements correct environment variable parsing with // GLib implements correct environment variable parsing with
// the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG. // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
// We used to use our custom parsing code along with ICU for this purpose. // We used to use our custom parsing code along with ICU for this purpose.
...@@ -489,18 +485,15 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) { ...@@ -489,18 +485,15 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
DCHECK(languages); // A valid pointer is guaranteed. DCHECK(languages); // A valid pointer is guaranteed.
DCHECK(*languages); // At least one entry, "C", is guaranteed. DCHECK(*languages); // At least one entry, "C", is guaranteed.
for (; *languages != NULL; ++languages) { for (; *languages; ++languages) {
candidates.push_back(base::i18n::GetCanonicalLocale(*languages)); candidates.push_back(base::i18n::GetCanonicalLocale(*languages));
} }
#else #else
// By default, use the application locale preference. This applies to ChromeOS // By default, use the application locale preference. This applies to ChromeOS
// and linux systems without glib. // and linux systems without glib.
if (!pref_locale.empty()) if (!pref_locale.empty())
candidates.push_back(pref_locale); candidates.push_back(pref_locale);
#endif // defined(OS_WIN)
#endif
std::vector<std::string>::const_iterator i = candidates.begin(); std::vector<std::string>::const_iterator i = candidates.begin();
for (; i != candidates.end(); ++i) { for (; i != candidates.end(); ++i) {
...@@ -511,12 +504,18 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) { ...@@ -511,12 +504,18 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
// Fallback on en-US. // Fallback on en-US.
const std::string fallback_locale("en-US"); const std::string fallback_locale("en-US");
if (IsLocaleAvailable(fallback_locale)) { if (IsLocaleAvailable(fallback_locale))
return fallback_locale; return fallback_locale;
}
return std::string(); return std::string();
}
#endif // !defined(OS_MACOSX)
std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
#if defined(OS_MACOSX)
return GetApplicationLocaleInternalMac(pref_locale);
#else
return GetApplicationLocaleInternalNonMac(pref_locale);
#endif #endif
} }
...@@ -598,7 +597,7 @@ base::string16 GetDisplayNameForLocale(const std::string& locale, ...@@ -598,7 +597,7 @@ base::string16 GetDisplayNameForLocale(const std::string& locale,
DCHECK(U_SUCCESS(error)); DCHECK(U_SUCCESS(error));
display_name.resize(actual_size); display_name.resize(actual_size);
} }
#endif #endif // defined(OS_IOS)
// Add directional markup so parentheses are properly placed. // Add directional markup so parentheses are properly placed.
if (is_for_ui && base::i18n::IsRTL()) if (is_for_ui && base::i18n::IsRTL())
...@@ -651,15 +650,15 @@ bool IsValidLocaleSyntax(const std::string& locale) { ...@@ -651,15 +650,15 @@ bool IsValidLocaleSyntax(const std::string& locale) {
prefix = locale.substr(0, split_point); prefix = locale.substr(0, split_point);
size_t equals_loc = keywords.find("="); size_t equals_loc = keywords.find("=");
if (equals_loc == std::string::npos || if (equals_loc == 0 || equals_loc == std::string::npos ||
equals_loc < 1 || equals_loc > keywords.size() - 2) equals_loc > keywords.size() - 2) {
return false; return false;
} }
}
// Check that all characters before the at-sign are alphanumeric or // Check that all characters before the at-sign are alphanumeric or
// underscore. // underscore.
for (size_t i = 0; i < prefix.size(); i++) { for (char ch : prefix) {
char ch = prefix[i];
if (!base::IsAsciiAlpha(ch) && !base::IsAsciiDigit(ch) && ch != '_') if (!base::IsAsciiAlpha(ch) && !base::IsAsciiDigit(ch) && ch != '_')
return false; return false;
} }
...@@ -682,8 +681,8 @@ bool IsValidLocaleSyntax(const std::string& locale) { ...@@ -682,8 +681,8 @@ bool IsValidLocaleSyntax(const std::string& locale) {
// delimiters into one.) // delimiters into one.)
int token_len = 0; int token_len = 0;
int token_index = 0; int token_index = 0;
for (size_t i = 0; i < prefix.size(); i++) { for (char ch : prefix) {
if (prefix[i] != '_') { if (ch != '_') {
token_len++; token_len++;
continue; continue;
} }
...@@ -694,11 +693,10 @@ bool IsValidLocaleSyntax(const std::string& locale) { ...@@ -694,11 +693,10 @@ bool IsValidLocaleSyntax(const std::string& locale) {
token_index++; token_index++;
token_len = 0; token_len = 0;
} }
if (token_index == 0 && (token_len < 1 || token_len > 3)) { if (token_index == 0 && (token_len < 1 || token_len > 3))
return false; return false;
} else if (token_len < 1 || token_len > 8) { if (token_len < 1 || token_len > 8)
return false; return false;
}
return true; return true;
} }
...@@ -786,26 +784,22 @@ std::string GetStringFUTF8(int message_id, ...@@ -786,26 +784,22 @@ std::string GetStringFUTF8(int message_id,
base::string16 GetStringFUTF16(int message_id, base::string16 GetStringFUTF16(int message_id,
const base::string16& a) { const base::string16& a) {
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a};
replacements.push_back(a); return GetStringFUTF16(message_id, replacements, nullptr);
return GetStringFUTF16(message_id, replacements, NULL);
} }
base::string16 GetStringFUTF16(int message_id, base::string16 GetStringFUTF16(int message_id,
const base::string16& a, const base::string16& a,
const base::string16& b) { const base::string16& b) {
return GetStringFUTF16(message_id, a, b, NULL); return GetStringFUTF16(message_id, a, b, nullptr);
} }
base::string16 GetStringFUTF16(int message_id, base::string16 GetStringFUTF16(int message_id,
const base::string16& a, const base::string16& a,
const base::string16& b, const base::string16& b,
const base::string16& c) { const base::string16& c) {
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a, b, c};
replacements.push_back(a); return GetStringFUTF16(message_id, replacements, nullptr);
replacements.push_back(b);
replacements.push_back(c);
return GetStringFUTF16(message_id, replacements, NULL);
} }
base::string16 GetStringFUTF16(int message_id, base::string16 GetStringFUTF16(int message_id,
...@@ -813,12 +807,8 @@ base::string16 GetStringFUTF16(int message_id, ...@@ -813,12 +807,8 @@ base::string16 GetStringFUTF16(int message_id,
const base::string16& b, const base::string16& b,
const base::string16& c, const base::string16& c,
const base::string16& d) { const base::string16& d) {
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a, b, c, d};
replacements.push_back(a); return GetStringFUTF16(message_id, replacements, nullptr);
replacements.push_back(b);
replacements.push_back(c);
replacements.push_back(d);
return GetStringFUTF16(message_id, replacements, NULL);
} }
base::string16 GetStringFUTF16(int message_id, base::string16 GetStringFUTF16(int message_id,
...@@ -827,13 +817,8 @@ base::string16 GetStringFUTF16(int message_id, ...@@ -827,13 +817,8 @@ base::string16 GetStringFUTF16(int message_id,
const base::string16& c, const base::string16& c,
const base::string16& d, const base::string16& d,
const base::string16& e) { const base::string16& e) {
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a, b, c, d, e};
replacements.push_back(a); return GetStringFUTF16(message_id, replacements, nullptr);
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, base::string16 GetStringFUTF16(int message_id,
...@@ -841,10 +826,9 @@ base::string16 GetStringFUTF16(int message_id, ...@@ -841,10 +826,9 @@ base::string16 GetStringFUTF16(int message_id,
size_t* offset) { size_t* offset) {
DCHECK(offset); DCHECK(offset);
std::vector<size_t> offsets; std::vector<size_t> offsets;
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a};
replacements.push_back(a);
base::string16 result = GetStringFUTF16(message_id, replacements, &offsets); base::string16 result = GetStringFUTF16(message_id, replacements, &offsets);
DCHECK(offsets.size() == 1); DCHECK_EQ(1u, offsets.size());
*offset = offsets[0]; *offset = offsets[0];
return result; return result;
} }
...@@ -853,9 +837,7 @@ base::string16 GetStringFUTF16(int message_id, ...@@ -853,9 +837,7 @@ base::string16 GetStringFUTF16(int message_id,
const base::string16& a, const base::string16& a,
const base::string16& b, const base::string16& b,
std::vector<size_t>* offsets) { std::vector<size_t>* offsets) {
std::vector<base::string16> replacements; std::vector<base::string16> replacements = {a, b};
replacements.push_back(a);
replacements.push_back(b);
return GetStringFUTF16(message_id, replacements, offsets); return GetStringFUTF16(message_id, replacements, offsets);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment