diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-13 11:08:08 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-13 11:08:08 +0000 |
commit | 534a4ecd47a7d0000158241f2d2d28218974e73e (patch) | |
tree | 1f9a9e8e62b690c9047d557a8f4796058de4f81d | |
parent | e62d9470d7a5d824c7d2d3e712a1c67b2ce3f102 (diff) | |
download | chromium_src-534a4ecd47a7d0000158241f2d2d28218974e73e.zip chromium_src-534a4ecd47a7d0000158241f2d2d28218974e73e.tar.gz chromium_src-534a4ecd47a7d0000158241f2d2d28218974e73e.tar.bz2 |
Rework NormalizeLanguageCode() so it returns codes compatible with Chrome's application locale codes.
The change is necessary to integrate UI language selection in the language
settings dialog. In short, input languages and UI languaes (application
locale codes are used for UI languages) need to be compatible.
TEST=unit_tests
BUG=2336
Review URL: http://codereview.chromium.org/1575034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44342 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/cros/language_library.cc | 50 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/language_library.h | 14 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/language_library_test.cc | 23 |
3 files changed, 57 insertions, 30 deletions
diff --git a/chrome/browser/chromeos/cros/language_library.cc b/chrome/browser/chromeos/cros/language_library.cc index 712c1a8..10b8c23 100644 --- a/chrome/browser/chromeos/cros/language_library.cc +++ b/chrome/browser/chromeos/cros/language_library.cc @@ -42,8 +42,8 @@ bool FindAndUpdateProperty(const chromeos::ImeProperty& new_prop, // There are some differences between ISO 639-2 (T) and ISO 639-2 B, and // some language codes are not recognized by ICU (i.e. ICU cannot convert -// these codes to display names). Hence we convert these codes to ones -// that ICU recognize. +// these codes to two-letter language codes and display names). Hence we +// convert these codes to ones that ICU recognize. // // See http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for details. const char* kIso639VariantMapping[][2] = { @@ -63,26 +63,42 @@ namespace chromeos { std::string LanguageLibrary::NormalizeLanguageCode( const std::string& language_code) { - // Convert some language codes. See comments at kIso639VariantMapping. - if (language_code.size() == 3) { - for (size_t i = 0; i < arraysize(kIso639VariantMapping); ++i) { - if (language_code == kIso639VariantMapping[i][0]) { - return kIso639VariantMapping[i][1]; - } + // Some ibus engines return locale codes like "zh_CN" as language codes. + // Normalize these to like "zh-CN". + if (language_code.size() >= 5 && language_code[2] == '_') { + std::string copied_language_code = language_code; + copied_language_code[2] = '-'; + // Downcase the language code part. + for (size_t i = 0; i < 2; ++i) { + copied_language_code[i] = ToLowerASCII(copied_language_code[i]); } + // Upcase the country code part. + for (size_t i = 3; i < copied_language_code.size(); ++i) { + copied_language_code[i] = ToUpperASCII(copied_language_code[i]); + } + return copied_language_code; } - // We only handle two-letter codes from here. - // Some ibus engines return locale codes like "zh_CN" as language codes, - // and we don't want to rewrite this to "zho". - if (language_code.size() != 2) { + // We only handle three-letter codes from here. + if (language_code.size() != 3) { return language_code; } - const char* three_letter_code = uloc_getISO3Language( - language_code.c_str()); - if (three_letter_code && strlen(three_letter_code) > 0) { - return three_letter_code; + + // Convert special language codes. See comments at kIso639VariantMapping. + std::string copied_language_code = language_code; + for (size_t i = 0; i < arraysize(kIso639VariantMapping); ++i) { + if (language_code == kIso639VariantMapping[i][0]) { + copied_language_code = kIso639VariantMapping[i][1]; + } + } + // Convert the three-letter code to two letter-code. + UErrorCode error = U_ZERO_ERROR; + char two_letter_code[ULOC_LANG_CAPACITY]; + uloc_getLanguage(copied_language_code.c_str(), + two_letter_code, sizeof(two_letter_code), &error); + if (U_FAILURE(error)) { + return language_code; } - return language_code; + return two_letter_code; } bool LanguageLibrary::IsKeyboardLayout( diff --git a/chrome/browser/chromeos/cros/language_library.h b/chrome/browser/chromeos/cros/language_library.h index efc49fd..bf51a31 100644 --- a/chrome/browser/chromeos/cros/language_library.h +++ b/chrome/browser/chromeos/cros/language_library.h @@ -82,10 +82,16 @@ class LanguageLibrary { virtual const ImePropertyList& current_ime_properties() const = 0; - // Normalizes the language code and returns the normalized version. - // The function concverts a two-letter language code to its - // corresponding three-letter code like "ja" => "jpn". Otherwise, - // returns the given language code as-is. + // Normalizes the language code and returns the normalized version. The + // function normalizes the given language code to be compatible with the + // one used in Chrome's application locales. Otherwise, returns the + // given language code as-is. + // + // Examples: + // + // - "zh_CN" => "zh-CN" (Use - instead of _) + // - "jpn" => "ja" (Use two-letter code) + // - "t" => "t" (Return as-is if unknown) static std::string NormalizeLanguageCode(const std::string& language_code); // Returns true if the given input method id is for a keyboard layout. diff --git a/chrome/browser/chromeos/cros/language_library_test.cc b/chrome/browser/chromeos/cros/language_library_test.cc index ade0f34..11df138 100644 --- a/chrome/browser/chromeos/cros/language_library_test.cc +++ b/chrome/browser/chromeos/cros/language_library_test.cc @@ -10,17 +10,22 @@ namespace chromeos { TEST(LanguageLibraryTest, NormalizeLanguageCode) { // TODO(yusukes): test all language codes that IBus provides. - EXPECT_EQ("jpn", LanguageLibrary::NormalizeLanguageCode("ja")); - EXPECT_EQ("jpn", LanguageLibrary::NormalizeLanguageCode("jpn")); + EXPECT_EQ("ja", LanguageLibrary::NormalizeLanguageCode("ja")); + EXPECT_EQ("ja", LanguageLibrary::NormalizeLanguageCode("jpn")); EXPECT_EQ("t", LanguageLibrary::NormalizeLanguageCode("t")); - EXPECT_EQ("zh_CN", LanguageLibrary::NormalizeLanguageCode("zh_CN")); + EXPECT_EQ("zh-CN", LanguageLibrary::NormalizeLanguageCode("zh-CN")); + EXPECT_EQ("zh-CN", LanguageLibrary::NormalizeLanguageCode("zh_CN")); + EXPECT_EQ("en-US", LanguageLibrary::NormalizeLanguageCode("EN_us")); + // See app/l10n_util.cc for es-419. + EXPECT_EQ("es-419", LanguageLibrary::NormalizeLanguageCode("es_419")); + // Special three-letter language codes. - EXPECT_EQ("ces", LanguageLibrary::NormalizeLanguageCode("cze")); - EXPECT_EQ("deu", LanguageLibrary::NormalizeLanguageCode("ger")); - EXPECT_EQ("ell", LanguageLibrary::NormalizeLanguageCode("gre")); - EXPECT_EQ("hrv", LanguageLibrary::NormalizeLanguageCode("scr")); - EXPECT_EQ("ron", LanguageLibrary::NormalizeLanguageCode("rum")); - EXPECT_EQ("slk", LanguageLibrary::NormalizeLanguageCode("slo")); + EXPECT_EQ("cs", LanguageLibrary::NormalizeLanguageCode("cze")); + EXPECT_EQ("de", LanguageLibrary::NormalizeLanguageCode("ger")); + EXPECT_EQ("el", LanguageLibrary::NormalizeLanguageCode("gre")); + EXPECT_EQ("hr", LanguageLibrary::NormalizeLanguageCode("scr")); + EXPECT_EQ("ro", LanguageLibrary::NormalizeLanguageCode("rum")); + EXPECT_EQ("sk", LanguageLibrary::NormalizeLanguageCode("slo")); } TEST(LanguageLibraryTest, IsKeyboardLayout) { |