diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/DEPS | 25 | ||||
-rw-r--r-- | app/app.vcproj | 16 | ||||
-rw-r--r-- | app/l10n_util.cc | 745 | ||||
-rw-r--r-- | app/l10n_util.h | 370 | ||||
-rw-r--r-- | app/l10n_util_posix.cc | 14 | ||||
-rw-r--r-- | app/l10n_util_unittest.cc | 435 | ||||
-rw-r--r-- | app/l10n_util_win.cc | 120 | ||||
-rw-r--r-- | app/l10n_util_win.h | 51 | ||||
-rw-r--r-- | app/resource_bundle_linux.cc | 2 | ||||
-rw-r--r-- | app/resource_bundle_win.cc | 2 |
10 files changed, 1770 insertions, 10 deletions
@@ -1,8 +1,17 @@ -include_rules = [ - "+net", - # TODO(beng): Sever this link once we have extracted all deps from - # chrome/common. - "+chrome/common", - # TODO(beng): Sever this link after glen fixes it. - "+chrome/browser/extensions/extension.h", -] +include_rules = [
+ "+grit",
+ "+net",
+ # TODO(beng): Sever these links once we have extracted all deps from
+ # chrome/common.
+ "+chrome/common/chrome_paths.h",
+ "+chrome/common/chrome_switches.h",
+ "+chrome/common/gfx/chrome_canvas.h",
+ "+chrome/common/gfx/chrome_font.h",
+ "+chrome/common/gtk_util.h",
+
+ # TODO(beng): l10n_util_unittest.cc:
+ "+chrome/test/data/resource.h",
+
+ # TODO(beng): resource_bundle.cc: Sever this link after glen fixes it.
+ "+chrome/browser/extensions/extension.h",
+]
diff --git a/app/app.vcproj b/app/app.vcproj index 02d76cd..c4c489c 100644 --- a/app/app.vcproj +++ b/app/app.vcproj @@ -130,6 +130,22 @@ >
</File>
<File
+ RelativePath=".\l10n_util.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\l10n_util.h"
+ >
+ </File>
+ <File
+ RelativePath=".\l10n_util_win.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\l10n_util_win.h"
+ >
+ </File>
+ <File
RelativePath=".\resource_bundle.cc"
>
</File>
diff --git a/app/l10n_util.cc b/app/l10n_util.cc new file mode 100644 index 0000000..55ce530 --- /dev/null +++ b/app/l10n_util.cc @@ -0,0 +1,745 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "build/build_config.h" + +#include "app/l10n_util.h" + +#include "app/resource_bundle.h" +#include "base/command_line.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "base/scoped_ptr.h" +#include "base/string16.h" +#include "base/string_piece.h" +#include "base/string_util.h" +#include "base/sys_string_conversions.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/gfx/chrome_canvas.h" +#include "unicode/uscript.h" + +// TODO(playmobil): remove this undef once SkPostConfig.h is fixed. +// skia/include/corecg/SkPostConfig.h #defines strcasecmp() so we can't use +// base::strcasecmp() without #undefing it here. +#undef strcasecmp + +namespace { + +#if defined(OS_WIN) +static const FilePath::CharType kLocaleFileExtension[] = L".dll"; +#elif defined(OS_POSIX) +static const FilePath::CharType kLocaleFileExtension[] = ".pak"; +#endif + +// Added to the end of strings that are too big in TrucateString. +static const wchar_t* const kElideString = L"\x2026"; + +// Get language and region from the OS. +void GetLanguageAndRegionFromOS(std::string* lang, std::string* region) { + // Later we may have to change this to be OS-dependent so that + // it's not affected by ICU's default locale. It's all right + // to do this way because SetICUDefaultLocale is internal + // to this file and we know where/when it's called. + Locale locale = Locale::getDefault(); + const char* language = locale.getLanguage(); + const char* country = locale.getCountry(); + DCHECK(language); + *lang = language; + *region = country; +} + +// Convert Chrome locale name to ICU locale name +std::string ICULocaleName(const std::wstring& locale_string) { + // If not Spanish, just return it. + if (locale_string.substr(0, 2) != L"es") + return WideToASCII(locale_string); + // Expand es to es-ES. + if (LowerCaseEqualsASCII(locale_string, "es")) + return "es-ES"; + // Map es-419 (Latin American Spanish) to es-FOO depending on the system + // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map + // to es-MX (the most populous in Spanish-speaking Latin America). + if (LowerCaseEqualsASCII(locale_string, "es-419")) { + std::string lang, region; + GetLanguageAndRegionFromOS(&lang, ®ion); + if (LowerCaseEqualsASCII(lang, "es") && + !LowerCaseEqualsASCII(region, "es")) { + lang.append("-"); + lang.append(region); + return lang; + } + return "es-MX"; + } + // Currently, Chrome has only "es" and "es-419", but later we may have + // more specific "es-RR". + return WideToASCII(locale_string); +} + +// Sets the default locale of ICU. +// When the application locale (UI locale) of Chrome is specified with +// '--lang' command line flag or 'intl.app_locale' entry in the "Preferences", +// the default locale of ICU need to be changed to match the application locale +// so that ICU functions work correctly in a locale-dependent manner. +// This is handy in that we don't have to call GetApplicationLocale() +// everytime we call locale-dependent ICU APIs as long as we make sure +// that this is called before any locale-dependent API is called. +UBool SetICUDefaultLocale(const std::wstring& locale_string) { + Locale locale(ICULocaleName(locale_string).c_str()); + UErrorCode error_code = U_ZERO_ERROR; + Locale::setDefault(locale, error_code); + // This return value is actually bogus because Locale object is + // an ID and setDefault seems to always succeed (regardless of the + // presence of actual locale data). However, + // it does not hurt to have it as a sanity check. + return U_SUCCESS(error_code); +} + +// Returns true if |locale_name| has an alias in the ICU data file. +bool IsDuplicateName(const std::string& locale_name) { + static const char* const kDuplicateNames[] = { + "en", + "pt", + "zh", + "zh_hans_cn", + "zh_hant_tw" + }; + + // Skip all 'es_RR'. Currently, we use 'es' for es-ES (Spanish in Spain). + // 'es-419' (Spanish in Latin America) is not available in ICU so that it + // has to be added manually in GetAvailableLocales(). + 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 IsLocaleAvailable(const std::wstring& locale, + const std::wstring& locale_path) { + std::wstring test_locale = 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. + file_util::ReplaceIllegalCharacters(&test_locale, ' '); + if (test_locale != locale) + return false; + + if (!l10n_util::IsLocaleSupportedByOS(locale)) + return false; + + FilePath test_path = FilePath::FromWStringHack(locale_path) + .Append(FilePath::FromWStringHack(locale)) + .ReplaceExtension(kLocaleFileExtension); + return file_util::PathExists(test_path) && SetICUDefaultLocale(locale); +} + +bool CheckAndResolveLocale(const std::wstring& locale, + const std::wstring& locale_path, + std::wstring* resolved_locale) { + if (IsLocaleAvailable(locale, locale_path)) { + *resolved_locale = locale; + return true; + } + // If the locale matches language but not country, use that instead. + // TODO(jungshik) : Nothing is done about languages that Chrome + // 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::wstring::size_type hyphen_pos = locale.find(L'-'); + if (hyphen_pos != std::wstring::npos && hyphen_pos > 0) { + std::wstring lang(locale, 0, hyphen_pos); + std::wstring region(locale, hyphen_pos + 1); + std::wstring tmp_locale(lang); + // Map es-RR other than es-ES to es-419 (Chrome's Latin American + // Spanish locale). + if (LowerCaseEqualsASCII(lang, "es") && !LowerCaseEqualsASCII(region, "es")) + tmp_locale.append(L"-419"); + else if (LowerCaseEqualsASCII(lang, "zh")) { + // Map zh-HK and zh-MK to zh-TW. Otherwise, zh-FOO is mapped to zh-CN. + if (LowerCaseEqualsASCII(region, "hk") || + LowerCaseEqualsASCII(region, "mk")) { + tmp_locale.append(L"-TW"); + } else { + tmp_locale.append(L"-CN"); + } + } + if (IsLocaleAvailable(tmp_locale, locale_path)) { + resolved_locale->swap(tmp_locale); + return true; + } + } + + // Google updater uses no, iw and en for our nb, he, and en-US. + // We need to map them to our codes. + struct { + const char* source; + const wchar_t* dest;} alias_map[] = { + {"no", L"nb"}, + {"tl", L"fil"}, + {"iw", L"he"}, + {"en", L"en-US"}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(alias_map); ++i) { + if (LowerCaseEqualsASCII(locale, alias_map[i].source)) { + std::wstring tmp_locale(alias_map[i].dest); + if (IsLocaleAvailable(tmp_locale, locale_path)) { + resolved_locale->swap(tmp_locale); + return true; + } + } + } + + return false; +} + +// Get the locale of the operating system. The return value is of the form +// language[-country] (e.g., en-US) where the language is the 2 letter code from +// ISO-639. +std::wstring GetSystemLocale() { + std::string language, region; + GetLanguageAndRegionFromOS(&language, ®ion); + std::string ret; + if (!language.empty()) + ret.append(language); + if (!region.empty()) { + ret.append("-"); + ret.append(region); + } + return ASCIIToWide(ret); +} + +} // namespace + +namespace l10n_util { + +// Represents the locale-specific text direction. +static TextDirection g_text_direction = UNKNOWN_DIRECTION; + +std::wstring GetApplicationLocale(const std::wstring& pref_locale) { +#if defined(OS_MACOSX) + // On the mac, we don't want to test preferences or ICU for the language, + // we want to use whatever Cocoa is using when it loaded the main nib file. + // It handles all the mapping and fallbacks for us, we just need to ask + // Cocoa. + // TODO(pinkerton): break this out into a .mm and ask Cocoa. + return L"en"; +#else + FilePath locale_path; + PathService::Get(chrome::DIR_LOCALES, &locale_path); + std::wstring resolved_locale; + + // First, check to see if there's a --lang flag. + const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); + const std::wstring& lang_arg = + parsed_command_line.GetSwitchValue(switches::kLang); + if (!lang_arg.empty()) { + if (CheckAndResolveLocale(lang_arg, locale_path.ToWStringHack(), + &resolved_locale)) + return resolved_locale; + } + + // Second, try user prefs. + if (!pref_locale.empty()) { + if (CheckAndResolveLocale(pref_locale, locale_path.ToWStringHack(), + &resolved_locale)) + return resolved_locale; + } + + // Next, try the system locale. + const std::wstring system_locale = GetSystemLocale(); + if (CheckAndResolveLocale(system_locale, locale_path.ToWStringHack(), + &resolved_locale)) + return resolved_locale; + + // Fallback on en-US. + const std::wstring fallback_locale(L"en-US"); + if (IsLocaleAvailable(fallback_locale, locale_path.ToWStringHack())) + return fallback_locale; + + // No locale data file was found; we shouldn't get here. + NOTREACHED(); + + return std::wstring(); +#endif +} + +std::wstring GetLocalName(const std::string& locale_code_str, + const std::wstring& app_locale_wstr, + bool is_for_ui) { + const std::string app_locale = WideToASCII(app_locale_wstr); + const char* locale_code = locale_code_str.c_str(); + UErrorCode error = U_ZERO_ERROR; + const int buffer_size = 1024; + +#if defined(WCHAR_T_IS_UTF32) + string16 name_local_utf16; + int actual_size = uloc_getDisplayName(locale_code, app_locale.c_str(), + WriteInto(&name_local_utf16, buffer_size + 1), buffer_size, &error); + std::wstring name_local = UTF16ToWide(name_local_utf16); +#else + std::wstring name_local; + int actual_size = uloc_getDisplayName(locale_code, app_locale.c_str(), + WriteInto(&name_local, buffer_size + 1), buffer_size, &error); +#endif + DCHECK(U_SUCCESS(error)); + name_local.resize(actual_size); + // Add an RTL mark so parentheses are properly placed. + if (is_for_ui && GetTextDirection() == RIGHT_TO_LEFT) { + name_local.push_back(static_cast<wchar_t>(kRightToLeftMark)); + } + return name_local; +} + +std::wstring GetString(int message_id) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + return UTF16ToWide(rb.GetLocalizedString(message_id)); +} + +std::string GetStringUTF8(int message_id) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + return UTF16ToUTF8(rb.GetLocalizedString(message_id)); +} + +static string16 GetStringF(int message_id, + const string16& a, + const string16& b, + const string16& c, + const string16& d, + std::vector<size_t>* offsets) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + const string16& format_string = rb.GetLocalizedString(message_id); + string16 formatted = ReplaceStringPlaceholders(format_string, a, b, c, d, + offsets); + return formatted; +} + +std::wstring GetStringF(int message_id, const std::wstring& a) { + return UTF16ToWide(GetStringF(message_id, WideToUTF16(a), string16(), + string16(), string16(), NULL)); +} + +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b) { + return UTF16ToWide(GetStringF(message_id, WideToUTF16(a), WideToUTF16(b), + string16(), string16(), NULL)); +} + +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b, + const std::wstring& c) { + return UTF16ToWide(GetStringF(message_id, WideToUTF16(a), WideToUTF16(b), + WideToUTF16(c), string16(), NULL)); +} + +std::string GetStringFUTF8(int message_id, + const string16& a) { + return UTF16ToUTF8(GetStringF(message_id, a, string16(), string16(), + string16(), NULL)); +} + +std::string GetStringFUTF8(int message_id, + const string16& a, + const string16& b) { + return UTF16ToUTF8(GetStringF(message_id, a, b, string16(), string16(), + NULL)); +} + +std::string GetStringFUTF8(int message_id, + const string16& a, + const string16& b, + const string16& c) { + return UTF16ToUTF8(GetStringF(message_id, a, b, c, string16(), NULL)); +} + +std::wstring GetStringF(int message_id, const std::wstring& a, size_t* offset) { + DCHECK(offset); + std::vector<size_t> offsets; + string16 result = GetStringF(message_id, WideToUTF16(a), string16(), + string16(), string16(), &offsets); + DCHECK(offsets.size() == 1); + *offset = offsets[0]; + return UTF16ToWide(result); +} + +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b, + std::vector<size_t>* offsets) { + return UTF16ToWide(GetStringF(message_id, WideToUTF16(a), WideToUTF16(b), + string16(), string16(), offsets)); +} + +std::wstring GetStringF(int message_id, int a) { + return GetStringF(message_id, IntToWString(a)); +} + +std::wstring GetStringF(int message_id, int64 a) { + return GetStringF(message_id, Int64ToWString(a)); +} + +std::wstring TruncateString(const std::wstring& string, size_t length) { + if (string.size() <= length) + // String fits, return it. + return string; + + if (length == 0) { + // No room for the ellide string, return an empty string. + return std::wstring(L""); + } + size_t max = length - 1; + + if (max == 0) { + // Just enough room for the elide string. + return kElideString; + } + +#if defined(WCHAR_T_IS_UTF32) + const string16 string_utf16 = WideToUTF16(string); +#else + const std::wstring &string_utf16 = string; +#endif + // Use a line iterator to find the first boundary. + UErrorCode status = U_ZERO_ERROR; + scoped_ptr<RuleBasedBreakIterator> bi(static_cast<RuleBasedBreakIterator*>( + RuleBasedBreakIterator::createLineInstance(Locale::getDefault(), + status))); + if (U_FAILURE(status)) + return string.substr(0, max) + kElideString; + bi->setText(string_utf16.c_str()); + int32_t index = bi->preceding(static_cast<int32_t>(max)); + if (index == BreakIterator::DONE) { + index = static_cast<int32_t>(max); + } else { + // Found a valid break (may be the beginning of the string). Now use + // a character iterator to find the previous non-whitespace character. + StringCharacterIterator char_iterator(string_utf16.c_str()); + if (index == 0) { + // No valid line breaks. Start at the end again. This ensures we break + // on a valid character boundary. + index = static_cast<int32_t>(max); + } + char_iterator.setIndex(index); + while (char_iterator.hasPrevious()) { + char_iterator.previous(); + if (!(u_isspace(char_iterator.current()) || + u_charType(char_iterator.current()) == U_CONTROL_CHAR || + u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { + // Not a whitespace character. Advance the iterator so that we + // include the current character in the truncated string. + char_iterator.next(); + break; + } + } + if (char_iterator.hasPrevious()) { + // Found a valid break point. + index = char_iterator.getIndex(); + } else { + // String has leading whitespace, return the elide string. + return kElideString; + } + } + return string.substr(0, index) + kElideString; +} + +#if defined(WCHAR_T_IS_UTF32) +std::wstring ToLower(const std::wstring& string) { + string16 string_utf16 = WideToUTF16(string); + UnicodeString lower_u_str( + UnicodeString(string_utf16.c_str()).toLower(Locale::getDefault())); + string16 result_utf16; + lower_u_str.extract(0, lower_u_str.length(), + WriteInto(&result_utf16, lower_u_str.length() + 1)); + std::wstring result = UTF16ToWide(result_utf16); + return result; +} +#else +std::wstring ToLower(const std::wstring& string) { + UnicodeString lower_u_str( + UnicodeString(string.c_str()).toLower(Locale::getDefault())); + std::wstring result; + lower_u_str.extract(0, lower_u_str.length(), + WriteInto(&result, lower_u_str.length() + 1)); + return result; +} +#endif // defined(WCHAR_T_IS_UTF32) + +// Returns the text direction for the default ICU locale. It is assumed +// that SetICUDefaultLocale has been called to set the default locale to +// the UI locale of Chrome. +TextDirection GetTextDirection() { + if (g_text_direction == UNKNOWN_DIRECTION) { + const Locale& locale = Locale::getDefault(); + g_text_direction = GetTextDirectionForLocale(locale.getName()); + } + return g_text_direction; +} + +TextDirection GetTextDirectionForLocale(const char* locale_name) { + UScriptCode scripts[10]; // 10 scripts should be enough for any locale. + UErrorCode error = U_ZERO_ERROR; + int n = uscript_getCode(locale_name, scripts, 10, &error); + DCHECK(U_SUCCESS(error) && n > 0); + + // Checking Arabic and Hebrew scripts cover Arabic, Hebrew, Farsi, + // Urdu and Azerbaijani written in Arabic. Syriac script + // (another RTL) is not a living script and we didn't yet localize + // to locales using other living RTL scripts such as Thaana and N'ko. + // TODO(jungshik): Use a new ICU API, uloc_getCharacterOrientation to avoid + // 'hardcoded-comparision' with Arabic and Hebrew scripts once we + // upgrade ICU to 4.0 or later or port it to our copy of ICU. + if (scripts[0] == USCRIPT_ARABIC || scripts[0] == USCRIPT_HEBREW) + return RIGHT_TO_LEFT; + return LEFT_TO_RIGHT; +} + +TextDirection GetFirstStrongCharacterDirection(const std::wstring& text) { +#if defined(WCHAR_T_IS_UTF32) + string16 text_utf16 = WideToUTF16(text); + const UChar* string = text_utf16.c_str(); +#else + const UChar* string = text.c_str(); +#endif + size_t length = text.length(); + size_t position = 0; + while (position < length) { + UChar32 character; + size_t next_position = position; + U16_NEXT(string, next_position, length, character); + + // Now that we have the character, we use ICU in order to query for the + // appropriate Unicode BiDi character type. + int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); + if ((property == U_RIGHT_TO_LEFT) || + (property == U_RIGHT_TO_LEFT_ARABIC) || + (property == U_RIGHT_TO_LEFT_EMBEDDING) || + (property == U_RIGHT_TO_LEFT_OVERRIDE)) { + return RIGHT_TO_LEFT; + } else if ((property == U_LEFT_TO_RIGHT) || + (property == U_LEFT_TO_RIGHT_EMBEDDING) || + (property == U_LEFT_TO_RIGHT_OVERRIDE)) { + return LEFT_TO_RIGHT; + } + + position = next_position; + } + + return LEFT_TO_RIGHT; +} + +bool AdjustStringForLocaleDirection(const std::wstring& text, + std::wstring* localized_text) { + if (GetTextDirection() == LEFT_TO_RIGHT || text.length() == 0) + return false; + + // Marking the string as LTR if the locale is RTL and the string does not + // contain strong RTL characters. Otherwise, mark the string as RTL. + *localized_text = text; + bool has_rtl_chars = StringContainsStrongRTLChars(text); + if (!has_rtl_chars) + WrapStringWithLTRFormatting(localized_text); + else + WrapStringWithRTLFormatting(localized_text); + + return true; +} + +bool StringContainsStrongRTLChars(const std::wstring& text) { +#if defined(WCHAR_T_IS_UTF32) + string16 text_utf16 = WideToUTF16(text); + const UChar* string = text_utf16.c_str(); +#else + const UChar* string = text.c_str(); +#endif + size_t length = text.length(); + size_t position = 0; + while (position < length) { + UChar32 character; + size_t next_position = position; + U16_NEXT(string, next_position, length, character); + + // Now that we have the character, we use ICU in order to query for the + // appropriate Unicode BiDi character type. + int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); + if ((property == U_RIGHT_TO_LEFT) || (property == U_RIGHT_TO_LEFT_ARABIC)) + return true; + + position = next_position; + } + + return false; +} + +void WrapStringWithLTRFormatting(std::wstring* text) { + // Inserting an LRE (Left-To-Right Embedding) mark as the first character. + text->insert(0, 1, static_cast<wchar_t>(kLeftToRightEmbeddingMark)); + + // Inserting a PDF (Pop Directional Formatting) mark as the last character. + text->push_back(static_cast<wchar_t>(kPopDirectionalFormatting)); +} + +void WrapStringWithRTLFormatting(std::wstring* text) { + // Inserting an RLE (Right-To-Left Embedding) mark as the first character. + text->insert(0, 1, static_cast<wchar_t>(kRightToLeftEmbeddingMark)); + + // Inserting a PDF (Pop Directional Formatting) mark as the last character. + text->push_back(static_cast<wchar_t>(kPopDirectionalFormatting)); +} + +void WrapPathWithLTRFormatting(const FilePath& path, + string16* rtl_safe_path) { + // Wrap the overall path with LRE-PDF pair which essentialy marks the + // string as a Left-To-Right string. + // Inserting an LRE (Left-To-Right Embedding) mark as the first character. + rtl_safe_path->push_back(kLeftToRightEmbeddingMark); +#if defined(OS_MACOSX) + rtl_safe_path->append(UTF8ToUTF16(path.value())); +#elif defined(OS_WIN) + rtl_safe_path->append(path.value()); +#else // defined(OS_LINUX) + std::wstring wide_path = base::SysNativeMBToWide(path.value()); + rtl_safe_path->append(WideToUTF16(wide_path)); +#endif + // Inserting a PDF (Pop Directional Formatting) mark as the last character. + rtl_safe_path->push_back(kPopDirectionalFormatting); +} + +int DefaultCanvasTextAlignment() { + if (GetTextDirection() == LEFT_TO_RIGHT) { + return ChromeCanvas::TEXT_ALIGN_LEFT; + } else { + return ChromeCanvas::TEXT_ALIGN_RIGHT; + } +} + + +// Compares the character data stored in two different strings by specified +// Collator instance. +UCollationResult CompareStringWithCollator(const Collator* collator, + const std::wstring& lhs, + const std::wstring& rhs) { + DCHECK(collator); + UErrorCode error = U_ZERO_ERROR; +#if defined(WCHAR_T_IS_UTF32) + // Need to convert to UTF-16 to be compatible with UnicodeString's + // constructor. + string16 lhs_utf16 = WideToUTF16(lhs); + string16 rhs_utf16 = WideToUTF16(rhs); + + UCollationResult result = collator->compare( + static_cast<const UChar*>(lhs_utf16.c_str()), + static_cast<int>(lhs_utf16.length()), + static_cast<const UChar*>(rhs_utf16.c_str()), + static_cast<int>(rhs_utf16.length()), + error); +#else + UCollationResult result = collator->compare( + static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), + static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), + error); +#endif + DCHECK(U_SUCCESS(error)); + return result; +} + +// Specialization of operator() method for std::wstring version. +template <> +bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, + const std::wstring& rhs) { + // If we can not get collator instance for specified locale, just do simple + // string compare. + if (!collator_) + return lhs < rhs; + return CompareStringWithCollator(collator_, lhs, rhs) == UCOL_LESS; +}; + +void SortStrings(const std::wstring& locale, + std::vector<std::wstring>* strings) { + SortVectorWithStringKey(locale, strings, false); +} + +const std::vector<std::string>& GetAvailableLocales() { + static std::vector<std::string> locales; + if (locales.empty()) { + int num_locales = uloc_countAvailable(); + for (int i = 0; i < num_locales; ++i) { + std::string locale_name = uloc_getAvailable(i); + // Filter out the names that have aliases. + if (IsDuplicateName(locale_name)) + continue; + if (!IsLocaleSupportedByOS(ASCIIToWide(locale_name))) + continue; + // Normalize underscores to hyphens because that's what our locale files + // use. + std::replace(locale_name.begin(), locale_name.end(), '_', '-'); + + // Map the Chinese locale names over to zh-CN and zh-TW. + 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); + } + + // Manually add 'es-419' to the list. See the comment in IsDuplicateName(). + locales.push_back("es-419"); + } + return locales; +} + +BiDiLineIterator::~BiDiLineIterator() { + if (bidi_) { + ubidi_close(bidi_); + bidi_ = NULL; + } +} + +UBool BiDiLineIterator::Open(const std::wstring& text, + bool right_to_left, + bool url) { + DCHECK(bidi_ == NULL); + UErrorCode error = U_ZERO_ERROR; + bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error); + if (U_FAILURE(error)) + return false; + if (right_to_left && url) + ubidi_setReorderingMode(bidi_, UBIDI_REORDER_RUNS_ONLY); +#if defined(WCHAR_T_IS_UTF32) + const string16 text_utf16 = WideToUTF16(text); +#else + const std::wstring &text_utf16 = text; +#endif // U_SIZEOF_WCHAR_T != 4 + ubidi_setPara(bidi_, text_utf16.data(), static_cast<int>(text_utf16.length()), + right_to_left ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, + NULL, &error); + return U_SUCCESS(error); +} + +int BiDiLineIterator::CountRuns() { + DCHECK(bidi_ != NULL); + UErrorCode error = U_ZERO_ERROR; + const int runs = ubidi_countRuns(bidi_, &error); + return U_SUCCESS(error) ? runs : 0; +} + +UBiDiDirection BiDiLineIterator::GetVisualRun(int index, + int* start, + int* length) { + DCHECK(bidi_ != NULL); + return ubidi_getVisualRun(bidi_, index, start, length); +} + +void BiDiLineIterator::GetLogicalRun(int start, + int* end, + UBiDiLevel* level) { + DCHECK(bidi_ != NULL); + ubidi_getLogicalRun(bidi_, start, end, level); +} + +} diff --git a/app/l10n_util.h b/app/l10n_util.h new file mode 100644 index 0000000..aafd658 --- /dev/null +++ b/app/l10n_util.h @@ -0,0 +1,370 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file contains utility functions for dealing with localized +// content. + +#ifndef APP_L10N_UTIL_H_ +#define APP_L10N_UTIL_H_ + +#include "build/build_config.h" + +#include <algorithm> +#include <functional> +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/logging.h" +#include "base/scoped_ptr.h" +#include "base/string16.h" +#include "base/string_util.h" +#include "unicode/coll.h" +#include "unicode/locid.h" +#include "unicode/rbbi.h" +#include "unicode/ubidi.h" +#include "unicode/uchar.h" + +class FilePath; +class PrefService; + +namespace l10n_util { + +const char16 kRightToLeftMark = 0x200f; +const char16 kLeftToRightMark = 0x200e; +const char16 kLeftToRightEmbeddingMark = 0x202A; +const char16 kRightToLeftEmbeddingMark = 0x202B; +const char16 kPopDirectionalFormatting = 0x202C; + +// This method is responsible for determining the locale as defined below. In +// nearly all cases you shouldn't call this, rather use GetApplicationLocale +// defined on browser_process. +// +// Returns the locale used by the Application. First we use the value from the +// command line (--lang), second we try the value in the prefs file (passed in +// as |pref_locale|), finally, we fall back on the system locale. We only return +// a value if there's a corresponding resource DLL for the locale. Otherwise, +// we fall back to en-us. +std::wstring GetApplicationLocale(const std::wstring& pref_locale); + +// Given a locale code, return true if the OS is capable of supporting it. +// For instance, Oriya is not well supported on Windows XP and we return +// false for "or". +bool IsLocaleSupportedByOS(const std::wstring& locale); + +// This method returns the Local Name of the Locale Code. For example, for +// |local_code_wstr| = "en-US", it returns "English (United States)". +// |app_locale_wstr| can be obtained in the UI thread - for example: +// const std::wstring app_locale_wstr = g_browser_process-> +// GetApplicationLocale(); +// If |is_for_ui| is true, U+200F is appended so that it can be +// rendered properly in a RTL Chrome. +std::wstring GetLocalName(const std::string& locale_code_str, + const std::wstring& app_locale_wstr, + bool is_for_ui); + +// Pulls resource string from the string bundle and returns it. +std::wstring GetString(int message_id); +std::string GetStringUTF8(int message_id); + +// Get a resource string and replace $1-$2-$3 with |a| and |b| +// respectively. Additionally, $$ is replaced by $. +std::wstring GetStringF(int message_id, + const std::wstring& a); +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b); +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b, + const std::wstring& c); +std::string GetStringFUTF8(int message_id, + const string16& a); +std::string GetStringFUTF8(int message_id, + const string16& a, + const string16& b); +std::string GetStringFUTF8(int message_id, + const string16& a, + const string16& b, + const string16& c); + +// Variants that return the offset(s) of the replaced parameters. The +// vector based version returns offsets ordered by parameter. For example if +// invoked with a and b offsets[0] gives the offset for a and offsets[1] the +// offset of b regardless of where the parameters end up in the string. +std::wstring GetStringF(int message_id, + const std::wstring& a, + size_t* offset); +std::wstring GetStringF(int message_id, + const std::wstring& a, + const std::wstring& b, + std::vector<size_t>* offsets); + +// Convenience formatters for a single number. +std::wstring GetStringF(int message_id, int a); +std::wstring GetStringF(int message_id, int64 a); + +// Truncates the string to length characters. This breaks the string at +// the first word break before length, adding the horizontal ellipsis +// character (unicode character 0x2026) to render ... +// The supplied string is returned if the string has length characters or +// less. +std::wstring TruncateString(const std::wstring& string, size_t length); + +// Returns the lower case equivalent of string. +std::wstring ToLower(const std::wstring& string); + +// Represents the text direction returned by the GetTextDirection() function. +enum TextDirection { + UNKNOWN_DIRECTION, + RIGHT_TO_LEFT, + LEFT_TO_RIGHT, +}; + +// Returns the text direction for the default ICU locale. It is assumed +// that SetICUDefaultLocale has been called to set the default locale to +// the UI locale of Chrome. Its return is one of the following three: +// * LEFT_TO_RIGHT: Left-To-Right (e.g. English, Chinese, etc.); +// * RIGHT_TO_LEFT: Right-To-Left (e.g. Arabic, Hebrew, etc.), and; +// * UNKNOWN_DIRECTION: unknown (or error). +TextDirection GetTextDirection(); + +// Returns the text direction for |locale_name|. +TextDirection GetTextDirectionForLocale(const char* locale_name); + +// Given the string in |text|, returns the directionality of the first +// character with strong directionality in the string. If no character in the +// text has strong directionality, LEFT_TO_RIGHT is returned. The Bidi +// character types L, LRE, LRO, R, AL, RLE, and RLO are considered as strong +// directionality characters. Please refer to http://unicode.org/reports/tr9/ +// for more information. +TextDirection GetFirstStrongCharacterDirection(const std::wstring& text); + +// Given the string in |text|, this function creates a copy of the string with +// the appropriate Unicode formatting marks that mark the string direction +// (either left-to-right or right-to-left). The new string is returned in +// |localized_text|. The function checks both the current locale and the +// contents of the string in order to determine the direction of the returned +// string. The function returns true if the string in |text| was properly +// adjusted. +// +// Certain LTR strings are not rendered correctly when the context is RTL. For +// example, the string "Foo!" will appear as "!Foo" if it is rendered as is in +// an RTL context. Calling this function will make sure the returned localized +// string is always treated as a right-to-left string. This is done by +// inserting certain Unicode formatting marks into the returned string. +// +// TODO(idana) bug# 1206120: this function adjusts the string in question only +// if the current locale is right-to-left. The function does not take care of +// the opposite case (an RTL string displayed in an LTR context) since +// adjusting the string involves inserting Unicode formatting characters that +// Windows does not handle well unless right-to-left language support is +// installed. Since the English version of Windows doesn't have right-to-left +// language support installed by default, inserting the direction Unicode mark +// results in Windows displaying squares. +bool AdjustStringForLocaleDirection(const std::wstring& text, + std::wstring* localized_text); + +// Returns true if the string contains at least one character with strong right +// to left directionality; that is, a character with either R or AL Unicode +// BiDi character type. +bool StringContainsStrongRTLChars(const std::wstring& text); + +// Wraps a string with an LRE-PDF pair which essentialy marks the string as a +// Left-To-Right string. Doing this is useful in order to make sure LTR +// strings are rendered properly in an RTL context. +void WrapStringWithLTRFormatting(std::wstring* text); + +// Wraps a string with an RLE-PDF pair which essentialy marks the string as a +// Right-To-Left string. Doing this is useful in order to make sure RTL +// strings are rendered properly in an LTR context. +void WrapStringWithRTLFormatting(std::wstring* text); + +// Wraps individual file path components to get them to display correctly in an +// RTL UI. All filepaths should be passed through this function before display +// in UI for RTL locales. +void WrapPathWithLTRFormatting(const FilePath& path, + string16* rtl_safe_path); + +// Returns the default text alignment to be used when drawing text on a +// ChromeCanvas based on the directionality of the system locale language. This +// function is used by ChromeCanvas::DrawStringInt when the text alignment is +// not specified. +// +// This function returns either ChromeCanvas::TEXT_ALIGN_LEFT or +// ChromeCanvas::TEXT_ALIGN_RIGHT. +int DefaultCanvasTextAlignment(); + +// Compares the two strings using the specified collator. +UCollationResult CompareStringWithCollator(const Collator* collator, + const std::wstring& lhs, + const std::wstring& rhs); + +// Used by SortStringsUsingMethod. Invokes a method on the objects passed to +// operator (), comparing the string results using a collator. +template <class T, class Method> +class StringMethodComparatorWithCollator : + public std::binary_function<const std::wstring&, + const std::wstring&, + bool> { + public: + StringMethodComparatorWithCollator(Collator* collator, Method method) + : collator_(collator), + method_(method) { } + + // Returns true if lhs preceeds rhs. + bool operator() (T* lhs_t, T* rhs_t) { + return CompareStringWithCollator(collator_, (lhs_t->*method_)(), + (rhs_t->*method_)()) == UCOL_LESS; + } + + private: + Collator* collator_; + Method method_; +}; + +// Used by SortStringsUsingMethod. Invokes a method on the objects passed to +// operator (), comparing the string results using <. +template <class T, class Method> +class StringMethodComparator : public std::binary_function<const std::wstring&, + const std::wstring&, + bool> { + public: + explicit StringMethodComparator(Method method) : method_(method) { } + + // Returns true if lhs preceeds rhs. + bool operator() (T* lhs_t, T* rhs_t) { + return (lhs_t->*method_)() < (rhs_t->*method_)(); + } + + private: + Method method_; +}; + +// Sorts the objects in |elements| using the method |method|, which must return +// a string. Sorting is done using a collator, unless a collator can not be +// found in which case the strings are sorted using the operator <. +template <class T, class Method> +void SortStringsUsingMethod(const std::wstring& locale, + std::vector<T*>* elements, + Method method) { + UErrorCode error = U_ZERO_ERROR; + Locale loc(WideToUTF8(locale).c_str()); + scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); + if (U_FAILURE(error)) { + sort(elements->begin(), elements->end(), + StringMethodComparator<T,Method>(method)); + return; + } + + std::sort(elements->begin(), elements->end(), + StringMethodComparatorWithCollator<T,Method>(collator.get(), method)); +} + +// Compares two elements' string keys and returns true if the first element's +// string key is less than the second element's string key. The Element must +// have a method like the follow format to return the string key. +// const std::wstring& GetStringKey() const; +// This uses the locale specified in the constructor. +template <class Element> +class StringComparator : public std::binary_function<const Element&, + const Element&, + bool> { + public: + explicit StringComparator(Collator* collator) + : collator_(collator) { } + + // Returns true if lhs precedes rhs. + bool operator()(const Element& lhs, const Element& rhs) { + const std::wstring& lhs_string_key = lhs.GetStringKey(); + const std::wstring& rhs_string_key = rhs.GetStringKey(); + + return StringComparator<std::wstring>(collator_)(lhs_string_key, + rhs_string_key); + } + + private: + Collator* collator_; +}; + +// Specialization of operator() method for std::wstring version. +template <> +bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, + const std::wstring& rhs); + +// In place sorting of |elements| of a vector according to the string key of +// each element in the vector by using collation rules for |locale|. +// |begin_index| points to the start position of elements in the vector which +// want to be sorted. |end_index| points to the end position of elements in the +// vector which want to be sorted +template <class Element> +void SortVectorWithStringKey(const std::wstring& locale, + std::vector<Element>* elements, + unsigned int begin_index, + unsigned int end_index, + bool needs_stable_sort) { + DCHECK(begin_index >= 0 && begin_index < end_index && + end_index <= static_cast<unsigned int>(elements->size())); + UErrorCode error = U_ZERO_ERROR; + Locale loc(WideToASCII(locale).c_str()); + scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); + if (U_FAILURE(error)) + collator.reset(); + StringComparator<Element> c(collator.get()); + if (needs_stable_sort) { + stable_sort(elements->begin() + begin_index, + elements->begin() + end_index, + c); + } else { + sort(elements->begin() + begin_index, elements->begin() + end_index, c); + } +} + +template <class Element> +void SortVectorWithStringKey(const std::wstring& locale, + std::vector<Element>* elements, + bool needs_stable_sort) { + SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), + needs_stable_sort); +} + +// In place sorting of strings using collation rules for |locale|. +// TODO(port): this should take string16. +void SortStrings(const std::wstring& locale, + std::vector<std::wstring>* strings); + +// Returns a vector of available locale codes. E.g., a vector containing +// en-US, es, fr, fi, pt-PT, pt-BR, etc. +const std::vector<std::string>& GetAvailableLocales(); + +// A simple wrapper class for the bidirectional iterator of ICU. +// This class uses the bidirectional iterator of ICU to split a line of +// bidirectional texts into visual runs in its display order. +class BiDiLineIterator { + public: + BiDiLineIterator() : bidi_(NULL) { } + ~BiDiLineIterator(); + + // Initializes the bidirectional iterator with the specified text. Returns + // whether initialization succeeded. + UBool Open(const std::wstring& text, bool right_to_left, bool url); + + // Returns the number of visual runs in the text, or zero on error. + int CountRuns(); + + // Gets the logical offset, length, and direction of the specified visual run. + UBiDiDirection GetVisualRun(int index, int* start, int* length); + + // Given a start position, figure out where the run ends (and the BiDiLevel). + void GetLogicalRun(int start, int* end, UBiDiLevel* level); + + private: + UBiDi* bidi_; + + DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); +}; + +} + +#endif // APP_L10N_UTIL_H_ diff --git a/app/l10n_util_posix.cc b/app/l10n_util_posix.cc new file mode 100644 index 0000000..c6797c2 --- /dev/null +++ b/app/l10n_util_posix.cc @@ -0,0 +1,14 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/l10n_util.h" + +namespace l10n_util { + +// Return true blindly for now. +bool IsLocaleSupportedByOS(const std::wstring& locale) { + return true; +} + +} // namespace l10n_util diff --git a/app/l10n_util_unittest.cc b/app/l10n_util_unittest.cc new file mode 100644 index 0000000..0ca5608 --- /dev/null +++ b/app/l10n_util_unittest.cc @@ -0,0 +1,435 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "build/build_config.h" + +#include "app/l10n_util.h" +#include "base/basictypes.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "base/stl_util-inl.h" +#include "base/string_util.h" +#if defined(OS_WIN) +#include "base/win_util.h" +#endif +#include "chrome/common/chrome_paths.h" +#if !defined(OS_MACOSX) +#include "chrome/test/data/resource.h" +#endif +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" +#include "unicode/locid.h" + +namespace { + +class StringWrapper { + public: + explicit StringWrapper(const std::wstring& string) : string_(string) {} + const std::wstring& string() const { return string_; } + + private: + std::wstring string_; + + DISALLOW_COPY_AND_ASSIGN(StringWrapper); +}; + +l10n_util::TextDirection GetTextDirection(const char* locale_name) { + return l10n_util::GetTextDirectionForLocale(locale_name); +} + +} // namespace + +class L10nUtilTest : public PlatformTest { +}; + +#if defined(OS_WIN) +TEST_F(L10nUtilTest, GetString) { + std::wstring s = l10n_util::GetString(IDS_SIMPLE); + EXPECT_EQ(std::wstring(L"Hello World!"), s); + + s = l10n_util::GetStringF(IDS_PLACEHOLDERS, L"chrome", L"10"); + EXPECT_EQ(std::wstring(L"Hello, chrome. Your number is 10."), s); + + s = l10n_util::GetStringF(IDS_PLACEHOLDERS_2, 20); + EXPECT_EQ(std::wstring(L"You owe me $20."), s); +} +#endif // defined(OS_WIN) + +TEST_F(L10nUtilTest, TruncateString) { + std::wstring string(L"foooooey bxxxar baz"); + + // Make sure it doesn't modify the string if length > string length. + EXPECT_EQ(string, l10n_util::TruncateString(string, 100)); + + // Test no characters. + EXPECT_EQ(L"", l10n_util::TruncateString(string, 0)); + + // Test 1 character. + EXPECT_EQ(L"\x2026", l10n_util::TruncateString(string, 1)); + + // Test adds ... at right spot when there is enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", l10n_util::TruncateString(string, 14)); + + // Test adds ... at right spot when there is not enough space in first word. + EXPECT_EQ(L"f\x2026", l10n_util::TruncateString(string, 2)); + + // Test adds ... at right spot when there is not enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", l10n_util::TruncateString(string, 11)); + + // Test completely truncates string if break is on initial whitespace. + EXPECT_EQ(L"\x2026", l10n_util::TruncateString(L" ", 2)); +} + +void SetICUDefaultLocale(const std::wstring& locale_string) { + Locale locale(WideToASCII(locale_string).c_str()); + UErrorCode error_code = U_ZERO_ERROR; + Locale::setDefault(locale, error_code); + EXPECT_TRUE(U_SUCCESS(error_code)); +} + +#if defined(OS_WIN) || defined(OS_LINUX) +// We are disabling this test on MacOS because GetApplicationLocale() as an +// API isn't something that we'll easily be able to unit test in this manner. +// The meaning of that API, on the Mac, is "the locale used by Cocoa's main +// nib file", which clearly can't be stubbed by a test app that doesn't use +// Cocoa. +TEST_F(L10nUtilTest, GetAppLocale) { + // Use a temporary locale dir so we don't have to actually build the locale + // dlls for this test. + FilePath orig_locale_dir; + PathService::Get(chrome::DIR_LOCALES, &orig_locale_dir); + FilePath new_locale_dir; + EXPECT_TRUE(file_util::CreateNewTempDirectory( + FILE_PATH_LITERAL("l10n_util_test"), + &new_locale_dir)); + PathService::Override(chrome::DIR_LOCALES, new_locale_dir.ToWStringHack()); + // Make fake locale files. + std::string filenames[] = { + "en-US", + "en-GB", + "fr", + "es-419", + "es", + "zh-TW", + "zh-CN", + "he", + "fil", + "nb", + "or", + }; + +#if defined(OS_WIN) + static const char kLocaleFileExtension[] = ".dll"; +#elif defined(OS_POSIX) + static const char kLocaleFileExtension[] = ".pak"; +#endif + for (size_t i = 0; i < arraysize(filenames); ++i) { + FilePath filename = new_locale_dir.AppendASCII( + filenames[i] + kLocaleFileExtension); + file_util::WriteFile(filename, "", 0); + } + + // Keep a copy of ICU's default locale before we overwrite it. + Locale locale = Locale::getDefault(); + + SetICUDefaultLocale(L"en-US"); + EXPECT_EQ(L"en-US", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"en-GB"); + EXPECT_EQ(L"en-GB", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"fr-CA"); + EXPECT_EQ(L"fr", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"xx"); + EXPECT_EQ(L"en-US", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"en-US"); + EXPECT_EQ(L"fr", l10n_util::GetApplicationLocale(L"fr")); + EXPECT_EQ(L"fr", l10n_util::GetApplicationLocale(L"fr-CA")); + + SetICUDefaultLocale(L"en-US"); + // Aliases iw, no, tl to he, nb, fil. + EXPECT_EQ(L"he", l10n_util::GetApplicationLocale(L"iw")); + EXPECT_EQ(L"nb", l10n_util::GetApplicationLocale(L"no")); + EXPECT_EQ(L"fil", l10n_util::GetApplicationLocale(L"tl")); + // es-419 and es-XX (where XX is not Spain) should be + // mapped to es-419 (Latin American Spanish). + EXPECT_EQ(L"es-419", l10n_util::GetApplicationLocale(L"es-419")); + EXPECT_EQ(L"es", l10n_util::GetApplicationLocale(L"es-ES")); + EXPECT_EQ(L"es-419", l10n_util::GetApplicationLocale(L"es-AR")); + + SetICUDefaultLocale(L"es-MX"); + EXPECT_EQ(L"es-419", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"es-AR"); + EXPECT_EQ(L"es-419", l10n_util::GetApplicationLocale(L"")); + EXPECT_EQ(L"es", l10n_util::GetApplicationLocale(L"es")); + + SetICUDefaultLocale(L"es-ES"); + EXPECT_EQ(L"es", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"es"); + EXPECT_EQ(L"es", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"zh-HK"); + EXPECT_EQ(L"zh-TW", l10n_util::GetApplicationLocale(L"")); + EXPECT_EQ(L"zh-CN", l10n_util::GetApplicationLocale(L"zh-CN")); + + SetICUDefaultLocale(L"zh-MK"); + EXPECT_EQ(L"zh-TW", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"zh-SG"); + EXPECT_EQ(L"zh-CN", l10n_util::GetApplicationLocale(L"")); + + SetICUDefaultLocale(L"he"); + EXPECT_EQ(L"en-US", l10n_util::GetApplicationLocale(L"en")); + +#if defined(OS_WIN) + // Oriya should be blocked unless OS is Vista or newer. + if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) { + SetICUDefaultLocale(L"or"); + EXPECT_EQ(L"en-US", l10n_util::GetApplicationLocale(L"")); + SetICUDefaultLocale(L"en-GB"); + EXPECT_EQ(L"en-GB", l10n_util::GetApplicationLocale(L"or")); + } else { + SetICUDefaultLocale(L"or"); + EXPECT_EQ(L"or", l10n_util::GetApplicationLocale(L"")); + SetICUDefaultLocale(L"en-GB"); + EXPECT_EQ(L"or", l10n_util::GetApplicationLocale(L"or")); + } +#endif + + // Clean up. + PathService::Override(chrome::DIR_LOCALES, orig_locale_dir.ToWStringHack()); + file_util::Delete(new_locale_dir, true); + UErrorCode error_code = U_ZERO_ERROR; + Locale::setDefault(locale, error_code); +} +#endif + +TEST_F(L10nUtilTest, SortStringsUsingFunction) { + std::vector<StringWrapper*> strings; + strings.push_back(new StringWrapper(L"C")); + strings.push_back(new StringWrapper(L"d")); + strings.push_back(new StringWrapper(L"b")); + strings.push_back(new StringWrapper(L"a")); + l10n_util::SortStringsUsingMethod(L"en-US", &strings, &StringWrapper::string); + ASSERT_TRUE(L"a" == strings[0]->string()); + ASSERT_TRUE(L"b" == strings[1]->string()); + ASSERT_TRUE(L"C" == strings[2]->string()); + ASSERT_TRUE(L"d" == strings[3]->string()); + STLDeleteElements(&strings); +} + +TEST_F(L10nUtilTest, GetFirstStrongCharacterDirection) { + // Test pure LTR string. + std::wstring string(L"foo bar"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type L. + string.assign(L"foo \x05d0 bar"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type R. + string.assign(L"\x05d0 foo bar"); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string which starts with a character with weak directionality + // and in which the first character with strong directionality is a character + // with type L. + string.assign(L"!foo \x05d0 bar"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string which starts with a character with weak directionality + // and in which the first character with strong directionality is a character + // with type R. + string.assign(L",\x05d0 foo bar"); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type LRE. + string.assign(L"\x202a \x05d0 foo bar"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type LRO. + string.assign(L"\x202d \x05d0 foo bar"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type RLE. + string.assign(L"\x202b foo \x05d0 bar"); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type RLO. + string.assign(L"\x202e foo \x05d0 bar"); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test bidi string in which the first character with strong directionality + // is a character with type AL. + string.assign(L"\x0622 foo \x05d0 bar"); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test a string without strong directionality characters. + string.assign(L",!.{}"); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test empty string. + string.assign(L""); + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); + + // Test characters in non-BMP (e.g. Phoenician letters. Please refer to + // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more + // information). +#if defined(WCHAR_T_IS_UTF32) + string.assign(L" ! \x10910" L"abc 123"); +#elif defined(WCHAR_T_IS_UTF16) + string.assign(L" ! \xd802\xdd10" L"abc 123"); +#else +#error wchar_t should be either UTF-16 or UTF-32 +#endif + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, + l10n_util::GetFirstStrongCharacterDirection(string)); + +#if defined(WCHAR_T_IS_UTF32) + string.assign(L" ! \x10401" L"abc 123"); +#elif defined(WCHAR_T_IS_UTF16) + string.assign(L" ! \xd801\xdc01" L"abc 123"); +#else +#error wchar_t should be either UTF-16 or UTF-32 +#endif + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, + l10n_util::GetFirstStrongCharacterDirection(string)); +} + +typedef struct { + std::wstring path; + std::wstring wrapped_path; +} PathAndWrappedPath; + +TEST_F(L10nUtilTest, WrapPathWithLTRFormatting) { + std::wstring kSeparator; + kSeparator.push_back(static_cast<wchar_t>(FilePath::kSeparators[0])); + const PathAndWrappedPath test_data[] = { + // Test common path, such as "c:\foo\bar". + { L"c:" + kSeparator + L"foo" + kSeparator + L"bar", + L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + + L"bar\x202c" + }, + // Test path with file name, such as "c:\foo\bar\test.jpg". + { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + + L"test.jpg", + L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + + L"bar" + kSeparator + L"test.jpg\x202c" + }, + // Test path ending with punctuation, such as "c:\(foo)\bar.". + { L"c:" + kSeparator + L"(foo)" + kSeparator + L"bar.", + L"\x202a"L"c:" + kSeparator + L"(foo)" + kSeparator + + L"bar.\x202c" + }, + // Test path ending with separator, such as "c:\foo\bar\". + { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator, + L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + + L"bar" + kSeparator + L"\x202c", + }, + // Test path with RTL character. + { L"c:" + kSeparator + L"\x05d0", + L"\x202a"L"c:" + kSeparator + L"\x05d0\x202c", + }, + // Test path with 2 level RTL directory names. + { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622", + L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + + L"\x0622\x202c", + }, + // Test path with mixed RTL/LTR directory names and ending with punctuation. + { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622" + kSeparator + + L"(foo)" + kSeparator + L"b.a.r.", + L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + + L"\x0622" + kSeparator + L"(foo)" + kSeparator + + L"b.a.r.\x202c", + }, + // Test path without driver name, such as "/foo/bar/test/jpg". + { kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + L"test.jpg", + L"\x202a" + kSeparator + L"foo" + kSeparator + L"bar" + + kSeparator + L"test.jpg" + L"\x202c" + }, + // Test path start with current directory, such as "./foo". + { L"." + kSeparator + L"foo", + L"\x202a"L"." + kSeparator + L"foo" + L"\x202c" + }, + // Test path start with parent directory, such as "../foo/bar.jpg". + { L".." + kSeparator + L"foo" + kSeparator + L"bar.jpg", + L"\x202a"L".." + kSeparator + L"foo" + kSeparator + + L"bar.jpg" + L"\x202c" + }, + // Test absolute path, such as "//foo/bar.jpg". + { kSeparator + kSeparator + L"foo" + kSeparator + L"bar.jpg", + L"\x202a" + kSeparator + kSeparator + L"foo" + kSeparator + + L"bar.jpg" + L"\x202c" + }, + // Test path with mixed RTL/LTR directory names. + { L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + kSeparator + + L"\x0622" + kSeparator + L"\x05d1.jpg", + L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + + kSeparator + L"\x0622" + kSeparator + L"\x05d1.jpg" + L"\x202c", + }, + // Test empty path. + { L"", + L"\x202a\x202c" + } + }; + for (unsigned int i = 0; i < arraysize(test_data); ++i) { + string16 localized_file_path_string; + FilePath path = FilePath::FromWStringHack(test_data[i].path); + l10n_util::WrapPathWithLTRFormatting(path, &localized_file_path_string); + std::wstring wrapped_path = UTF16ToWide(localized_file_path_string); + EXPECT_EQ(wrapped_path, test_data[i].wrapped_path); + } +} + +TEST_F(L10nUtilTest, GetTextDirection) { + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar")); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar_EG")); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he")); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he_IL")); + // iw is an obsolete code for Hebrew. + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("iw")); +#if 0 + // Enable these when we localize to Farsi, Urdu, Azerbaijani + // written in Arabic and Dhivehi. At the moment, our copy of + // ICU data does not have entry for them. + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("fa")); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ur")); + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("az_Arab")); + // Dhivehi that uses Thaana script. + EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("dv")); +#endif + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("en")); + // Chinese in China with '-'. + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("zh-CN")); + // Filipino : 3-letter code + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("fil")); + // Russian + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ru")); + // Japanese that uses multiple scripts + EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ja")); +} diff --git a/app/l10n_util_win.cc b/app/l10n_util_win.cc new file mode 100644 index 0000000..cf12d4d --- /dev/null +++ b/app/l10n_util_win.cc @@ -0,0 +1,120 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/l10n_util.h" +#include "app/l10n_util_win.h" + +#include <algorithm> +#include <windowsx.h> + +#include "base/string_util.h" +#include "base/win_util.h" + +#include "grit/locale_settings.h" + +namespace { + +void AdjustLogFont(const std::wstring& font_family, + double font_size_scaler, + LOGFONT* logfont) { + DCHECK(font_size_scaler > 0); + font_size_scaler = std::max(std::min(font_size_scaler, 2.0), 0.7); + logfont->lfHeight = static_cast<long>(font_size_scaler * + static_cast<double>(abs(logfont->lfHeight)) + 0.5) * + (logfont->lfHeight > 0 ? 1 : -1); + + // TODO(jungshik): We may want to check the existence of the font. + // If it's not installed, we shouldn't adjust the font. + if (font_family != L"default") { + int name_len = std::min(static_cast<int>(font_family.size()), + LF_FACESIZE -1); + memcpy(logfont->lfFaceName, font_family.data(), name_len * sizeof(WORD)); + logfont->lfFaceName[name_len] = 0; + } +} + +} // namespace + +namespace l10n_util { + +int GetExtendedStyles() { + return GetTextDirection() == LEFT_TO_RIGHT ? 0 : + WS_EX_LAYOUTRTL | WS_EX_RTLREADING; +} + +int GetExtendedTooltipStyles() { + return GetTextDirection() == LEFT_TO_RIGHT ? 0 : WS_EX_LAYOUTRTL; +} + +void HWNDSetRTLLayout(HWND hwnd) { + DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); + + // We don't have to do anything if the style is already set for the HWND. + if (!(ex_style & WS_EX_LAYOUTRTL)) { + ex_style |= WS_EX_LAYOUTRTL; + ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); + + // Right-to-left layout changes are not applied to the window immediately + // so we should make sure a WM_PAINT is sent to the window by invalidating + // the entire window rect. + ::InvalidateRect(hwnd, NULL, true); + } +} + +bool IsLocaleSupportedByOS(const std::wstring& locale) { + // Block Oriya on Windows XP. + return !(LowerCaseEqualsASCII(locale, "or") && + win_util::GetWinVersion() < win_util::WINVERSION_VISTA); +} + +bool NeedOverrideDefaultUIFont(std::wstring* override_font_family, + double* font_size_scaler) { + // This is rather simple-minded to deal with the UI font size + // issue for some Indian locales (ml, bn, hi) for which + // the default Windows fonts are too small to be legible. For those + // locales, IDS_UI_FONT_FAMILY is set to an actual font family to + // use while for other locales, it's set to 'default'. + + // XP and Vista or later have different font size issues and + // we need separate ui font specifications. + int ui_font_family_id = IDS_UI_FONT_FAMILY; + int ui_font_size_scaler_id = IDS_UI_FONT_SIZE_SCALER; + if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) { + ui_font_family_id = IDS_UI_FONT_FAMILY_XP; + ui_font_size_scaler_id = IDS_UI_FONT_SIZE_SCALER_XP; + } + + std::wstring ui_font_family = GetString(ui_font_family_id); + int scaler100 = StringToInt(l10n_util::GetString(ui_font_size_scaler_id)); + if (ui_font_family == L"default" && scaler100 == 100) + return false; + if (override_font_family && font_size_scaler) { + override_font_family->swap(ui_font_family); + *font_size_scaler = scaler100 / 100.0; + } + return true; +} + +void AdjustUIFont(LOGFONT* logfont) { + std::wstring ui_font_family; + double ui_font_size_scaler; + if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler)) + AdjustLogFont(ui_font_family, ui_font_size_scaler, logfont); +} + +void AdjustUIFontForWindow(HWND hwnd) { + std::wstring ui_font_family; + double ui_font_size_scaler; + if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler)) { + LOGFONT logfont; + if (GetObject(GetWindowFont(hwnd), sizeof(logfont), &logfont)) { + AdjustLogFont(ui_font_family, ui_font_size_scaler, &logfont); + HFONT hfont = CreateFontIndirect(&logfont); + if (hfont) + SetWindowFont(hwnd, hfont, FALSE); + } + } +} + +} // namespace l10n_util diff --git a/app/l10n_util_win.h b/app/l10n_util_win.h new file mode 100644 index 0000000..f53cd63 --- /dev/null +++ b/app/l10n_util_win.h @@ -0,0 +1,51 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef APP_L10N_UTIL_WIN_H_ +#define APP_L10N_UTIL_WIN_H_ + +#include <windows.h> + +namespace l10n_util { + +// Returns the locale-dependent extended window styles. +// This function is used for adding locale-dependent extended window styles +// (e.g. WS_EX_LAYOUTRTL, WS_EX_RTLREADING, etc.) when creating a window. +// Callers should OR this value into their extended style value when creating +// a window. +int GetExtendedStyles(); + +// TODO(xji): +// This is a temporary name, it will eventually replace GetExtendedStyles +int GetExtendedTooltipStyles(); + +// Give an HWND, this function sets the WS_EX_LAYOUTRTL extended style for the +// underlying window. When this style is set, the UI for the window is going to +// be mirrored. This is generally done for the UI of right-to-left languages +// such as Hebrew. +void HWNDSetRTLLayout(HWND hwnd); + +// See http://blogs.msdn.com/oldnewthing/archive/2005/09/15/467598.aspx +// and http://blogs.msdn.com/oldnewthing/archive/2006/06/26/647365.aspx +// as to why we need these three functions. + +// Return true if the default font (we get from Windows) is not suitable +// to use in the UI of the current UI (e.g. Malayalam, Bengali). If +// override_font_family and font_size_scaler are not null, they'll be +// filled with the font family name and the size scaler. +bool NeedOverrideDefaultUIFont(std::wstring* override_font_family, + double* font_size_scaler); + +// If the default UI font stored in |logfont| is not suitable, its family +// and size are replaced with those stored in the per-locale resource. +void AdjustUIFont(LOGFONT* logfont); + +// If the font for a given window (pointed to by HWND) is not suitable for the +// UI in the current UI langauge, its family and size are replaced with those +// stored in the per-locale resource. +void AdjustUIFontForWindow(HWND hwnd); + +} // namespace l10n_util + +#endif // APP_L10N_UTIL_WIN_H_ diff --git a/app/resource_bundle_linux.cc b/app/resource_bundle_linux.cc index 9c866b2..8f0660d 100644 --- a/app/resource_bundle_linux.cc +++ b/app/resource_bundle_linux.cc @@ -6,6 +6,7 @@ #include <gtk/gtk.h> +#include "app/l10n_util.h" #include "base/base_paths.h" #include "base/data_pack.h" #include "base/file_path.h" @@ -18,7 +19,6 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/gfx/chrome_font.h" #include "chrome/common/gtk_util.h" -#include "chrome/common/l10n_util.h" #include "SkBitmap.h" namespace { diff --git a/app/resource_bundle_win.cc b/app/resource_bundle_win.cc index cf21084..4ed32c4 100644 --- a/app/resource_bundle_win.cc +++ b/app/resource_bundle_win.cc @@ -6,6 +6,7 @@ #include <atlbase.h> +#include "app/l10n_util.h" #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" @@ -14,7 +15,6 @@ #include "base/win_util.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/gfx/chrome_font.h" -#include "chrome/common/l10n_util.h" namespace { |