#include "base/command_line.h"
#include "base/compiler_specific.h"
-#include "base/file_util.h"
+#include "base/files/file_util.h"
#include "base/i18n/file_util_icu.h"
#include "base/i18n/rtl.h"
#include "base/i18n/string_compare.h"
bool IsLocaleAvailable(const std::string& locale) {
// If locale has any illegal characters in it, we don't want to try to
// load it because it may be pointing outside the locale data file directory.
- if (!file_util::IsFilenameLegal(base::ASCIIToUTF16(locale)))
+ if (!base::i18n::IsFilenameLegal(base::ASCIIToUTF16(locale)))
return false;
// IsLocalePartiallyPopulated() can be called here for an early return w/o
return base::i18n::GetCanonicalLocale(locale.c_str());
}
+std::string GetLanguage(const std::string& locale) {
+ const std::string::size_type hyphen_pos = locale.find('-');
+ return std::string(locale, 0, hyphen_pos);
+}
+
bool CheckAndResolveLocale(const std::string& locale,
std::string* resolved_locale) {
#if defined(OS_MACOSX)
// does not support but available on Windows. We fall
// back to en-US in GetApplicationLocale so that it's a not critical,
// but we can do better.
- 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);
+ const std::string lang(GetLanguage(locale));
+ if (lang.size() < locale.size()) {
+ std::string region(locale, lang.size() + 1);
std::string tmp_locale(lang);
// Map es-RR other than es-ES to es-419 (Chrome's Latin American
// Spanish locale).
#endif
}
-std::string GetApplicationLocale(const std::string& pref_locale) {
+std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
#if defined(OS_MACOSX)
// Use any override (Cocoa for the browser), otherwise use the preference
if (app_locale.empty())
app_locale = "en-US";
- // Windows/Linux call SetICUDefaultLocale after determining the actual locale
- // with CheckAndResolveLocal to make ICU APIs work in that locale.
- // Mac doesn't use a locale directory tree of resources (it uses Mac style
- // resources), so mirror the Windows/Linux behavior of calling
- // SetICUDefaultLocale.
- base::i18n::SetICUDefaultLocale(app_locale);
return app_locale;
#else
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;
}
}
// Fallback on en-US.
const std::string fallback_locale("en-US");
if (IsLocaleAvailable(fallback_locale)) {
- base::i18n::SetICUDefaultLocale(fallback_locale);
return fallback_locale;
}
#endif
}
+std::string GetApplicationLocale(const std::string& pref_locale,
+ bool set_icu_locale) {
+ const std::string locale = GetApplicationLocaleInternal(pref_locale);
+ if (set_icu_locale && !locale.empty())
+ base::i18n::SetICUDefaultLocale(locale);
+ return locale;
+}
+
+std::string GetApplicationLocale(const std::string& pref_locale) {
+ return GetApplicationLocale(pref_locale, true /* set_icu_locale */);
+}
+
bool IsLocaleNameTranslated(const char* locale,
const std::string& display_locale) {
base::string16 display_name =
// the translation is available or not. If ICU doesn't have a translated
// name for this locale, GetDisplayNameForLocale will just return the
// locale code.
- return !IsStringASCII(display_name) ||
+ return !base::IsStringASCII(display_name) ||
base::UTF16ToASCII(display_name) != locale;
}
std::string locale_code = locale;
// Internally, we use the language code of zh-CN and zh-TW, but we want the
// display names to be Chinese (Simplified) and Chinese (Traditional) instead
- // of Chinese (China) and Chinese (Taiwan). To do that, we pass zh-Hans
- // and zh-Hant to ICU. Even with this mapping, we'd get
- // 'Chinese (Simplified Han)' and 'Chinese (Traditional Han)' in English and
- // even longer results in other languages. Arguably, they're better than
- // the current results : Chinese (China) / Chinese (Taiwan).
- // TODO(jungshik): Do one of the following:
- // 1. Special-case Chinese by getting the custom-translation for them
- // 2. Recycle IDS_ENCODING_{SIMP,TRAD}_CHINESE.
- // 3. Get translations for two directly from the ICU resouce bundle
- // because they're not accessible with other any API.
- // 4. Patch ICU to special-case zh-Hans/zh-Hant for us.
- // #1 and #2 wouldn't work if display_locale != current UI locale although
- // we can think of additional hack to work around the problem.
- // #3 can be potentially expensive.
+ // of Chinese (China) and Chinese (Taiwan).
+ // Translate uses "tl" (Tagalog) to mean "fil" (Filipino) until Google
+ // translate is changed to understand "fil". Make "tl" alias to "fil".
if (locale_code == "zh-CN")
locale_code = "zh-Hans";
else if (locale_code == "zh-TW")
locale_code = "zh-Hant";
+ else if (locale_code == "tl")
+ locale_code = "fil";
base::string16 display_name;
#if defined(OS_ANDROID)
return GetStringFUTF16(message_id, base::UTF8ToUTF16(base::Int64ToString(a)));
}
-// Specialization of operator() method for base::string16 version.
-template <>
-bool StringComparator<base::string16>::operator()(const base::string16& lhs,
- const base::string16& rhs) {
- // If we can not get collator instance for specified locale, just do simple
- // string compare.
- 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);
return width;
}
+const char* const* GetAcceptLanguageListForTesting() {
+ return kAcceptLanguageList;
+}
+
+size_t GetAcceptLanguageListSizeForTesting() {
+ return arraysize(kAcceptLanguageList);
+}
+
} // namespace l10n_util