diff options
Diffstat (limited to 'chromium/ui/base/l10n')
-rw-r--r-- | chromium/ui/base/l10n/l10n_util.cc | 26 | ||||
-rw-r--r-- | chromium/ui/base/l10n/l10n_util.h | 8 | ||||
-rw-r--r-- | chromium/ui/base/l10n/l10n_util_plurals.cc | 64 | ||||
-rw-r--r-- | chromium/ui/base/l10n/l10n_util_plurals.h | 30 | ||||
-rw-r--r-- | chromium/ui/base/l10n/l10n_util_win.cc | 4 | ||||
-rw-r--r-- | chromium/ui/base/l10n/l10n_util_win_unittest.cc | 2 | ||||
-rw-r--r-- | chromium/ui/base/l10n/time_format.cc | 44 |
7 files changed, 137 insertions, 41 deletions
diff --git a/chromium/ui/base/l10n/l10n_util.cc b/chromium/ui/base/l10n/l10n_util.cc index 513a615a141..96dfa8e868b 100644 --- a/chromium/ui/base/l10n/l10n_util.cc +++ b/chromium/ui/base/l10n/l10n_util.cc @@ -28,6 +28,7 @@ #include "third_party/icu/source/common/unicode/rbbi.h" #include "third_party/icu/source/common/unicode/uloc.h" #include "ui/base/l10n/l10n_util_collator.h" +#include "ui/base/l10n/l10n_util_plurals.h" #include "ui/base/resource/resource_bundle.h" #include "ui/base/ui_base_paths.h" @@ -35,7 +36,7 @@ #include "ui/base/l10n/l10n_util_android.h" #endif -#if defined(OS_LINUX) +#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) #include <glib.h> #endif @@ -452,7 +453,7 @@ std::string GetApplicationLocale(const std::string& pref_locale) { // On Android, query java.util.Locale for the default locale. candidates.push_back(GetDefaultLocale()); -#elif defined(OS_LINUX) +#elif defined(OS_POSIX) // If we're on a different Linux system, we have glib. // GLib implements correct environment variable parsing with @@ -831,6 +832,27 @@ bool StringComparator<string16>::operator()(const string16& lhs, UCOL_LESS; }; +string16 GetPluralStringFUTF16(const std::vector<int>& message_ids, + int number) { + scoped_ptr<icu::PluralFormat> format = BuildPluralFormat(message_ids); + DCHECK(format); + + UErrorCode err = U_ZERO_ERROR; + icu::UnicodeString result_files_string = format->format(number, err); + int capacity = result_files_string.length() + 1; + DCHECK_GT(capacity, 1); + string16 result; + result_files_string.extract( + static_cast<UChar*>(WriteInto(&result, capacity)), capacity, err); + DCHECK(U_SUCCESS(err)); + return result; +} + +std::string GetPluralStringFUTF8(const std::vector<int>& message_ids, + int number) { + return base::UTF16ToUTF8(GetPluralStringFUTF16(message_ids, number)); +} + void SortStrings16(const std::string& locale, std::vector<string16>* strings) { SortVectorWithStringKey(locale, strings, false); diff --git a/chromium/ui/base/l10n/l10n_util.h b/chromium/ui/base/l10n/l10n_util.h index 7c26b803467..d7753365d00 100644 --- a/chromium/ui/base/l10n/l10n_util.h +++ b/chromium/ui/base/l10n/l10n_util.h @@ -139,6 +139,14 @@ UI_EXPORT string16 GetStringFUTF16(int message_id, UI_EXPORT string16 GetStringFUTF16Int(int message_id, int a); string16 GetStringFUTF16Int(int message_id, int64 a); +// Get a resource string using |number| to decide which of |message_ids| should +// be used. |message_ids| must be size 6 and in order: default, singular, zero, +// two, few, many. +UI_EXPORT string16 GetPluralStringFUTF16(const std::vector<int>& message_ids, + int number); +UI_EXPORT std::string GetPluralStringFUTF8(const std::vector<int>& message_ids, + int number); + // In place sorting of string16 strings using collation rules for |locale|. UI_EXPORT void SortStrings16(const std::string& locale, std::vector<string16>* strings); diff --git a/chromium/ui/base/l10n/l10n_util_plurals.cc b/chromium/ui/base/l10n/l10n_util_plurals.cc new file mode 100644 index 00000000000..0dc008ff06e --- /dev/null +++ b/chromium/ui/base/l10n/l10n_util_plurals.cc @@ -0,0 +1,64 @@ +// Copyright 2013 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 "ui/base/l10n/l10n_util_plurals.h" + +#include "base/memory/scoped_ptr.h" +#include "ui/base/l10n/l10n_util.h" + +namespace l10n_util { + +scoped_ptr<icu::PluralRules> BuildPluralRules() { + UErrorCode err = U_ZERO_ERROR; + scoped_ptr<icu::PluralRules> rules( + icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); + if (U_FAILURE(err)) { + err = U_ZERO_ERROR; + icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); + rules.reset(icu::PluralRules::createRules(fallback_rules, err)); + DCHECK(U_SUCCESS(err)); + } + return rules.Pass(); +} + +scoped_ptr<icu::PluralFormat> BuildPluralFormat( + const std::vector<int>& message_ids) { + const icu::UnicodeString kKeywords[] = { + UNICODE_STRING_SIMPLE("other"), + UNICODE_STRING_SIMPLE("one"), + UNICODE_STRING_SIMPLE("zero"), + UNICODE_STRING_SIMPLE("two"), + UNICODE_STRING_SIMPLE("few"), + UNICODE_STRING_SIMPLE("many"), + }; + DCHECK_EQ(message_ids.size(), arraysize(kKeywords)); + UErrorCode err = U_ZERO_ERROR; + scoped_ptr<icu::PluralRules> rules(BuildPluralRules()); + + icu::UnicodeString pattern; + for (size_t i = 0; i < arraysize(kKeywords); ++i) { + int msg_id = message_ids[i]; + std::string sub_pattern = GetStringUTF8(msg_id); + // NA means this keyword is not used in the current locale. + // Even if a translator translated for this keyword, we do not + // use it unless it's 'other' (i=0) or it's defined in the rules + // for the current locale. Special-casing of 'other' will be removed + // once ICU's isKeyword is fixed to return true for isKeyword('other'). + if (sub_pattern.compare("NA") != 0 && + (i == 0 || rules->isKeyword(kKeywords[i]))) { + pattern += kKeywords[i]; + pattern += UNICODE_STRING_SIMPLE("{"); + pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8"); + pattern += UNICODE_STRING_SIMPLE("}"); + } + } + scoped_ptr<icu::PluralFormat> format = scoped_ptr<icu::PluralFormat>( + new icu::PluralFormat(*rules, pattern, err)); + if (!U_SUCCESS(err)) { + return scoped_ptr<icu::PluralFormat>(); + } + return format.Pass(); +} + +} // namespace l10n_util diff --git a/chromium/ui/base/l10n/l10n_util_plurals.h b/chromium/ui/base/l10n/l10n_util_plurals.h new file mode 100644 index 00000000000..203797d6c44 --- /dev/null +++ b/chromium/ui/base/l10n/l10n_util_plurals.h @@ -0,0 +1,30 @@ +// Copyright 2013 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 pluralization of +// localized content. + +#ifndef UI_BASE_L10N_L10N_UTIL_PLURALS_H_ +#define UI_BASE_L10N_L10N_UTIL_PLURALS_H_ + +#include <string> +#include <vector> + +#include "base/memory/scoped_ptr.h" +#include "third_party/icu/source/i18n/unicode/plurfmt.h" +#include "third_party/icu/source/i18n/unicode/plurrule.h" + +namespace l10n_util { + +// Returns a PluralRules for the current locale. +scoped_ptr<icu::PluralRules> BuildPluralRules(); + +// Returns a PluralFormat from |message_ids|. |message_ids| must be size 6 and +// in order: default, singular, zero, two, few, many. +scoped_ptr<icu::PluralFormat> BuildPluralFormat( + const std::vector<int>& message_ids); + +} // namespace l10n_util + +#endif // UI_BASE_L10N_L10N_UTIL_PLURALS_H_ diff --git a/chromium/ui/base/l10n/l10n_util_win.cc b/chromium/ui/base/l10n/l10n_util_win.cc index 123b74f6abf..0fa34cfbfaf 100644 --- a/chromium/ui/base/l10n/l10n_util_win.cc +++ b/chromium/ui/base/l10n/l10n_util_win.cc @@ -15,7 +15,7 @@ #include "base/win/windows_version.h" #include "grit/app_locale_settings.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/win/dpi.h" +#include "ui/gfx/win/dpi.h" namespace { @@ -151,7 +151,7 @@ bool NeedOverrideDefaultUIFont(string16* override_font_family, } void AdjustUIFont(LOGFONT* logfont) { - AdjustUIFontForDIP(ui::GetDPIScale(), logfont); + AdjustUIFontForDIP(gfx::GetDPIScale(), logfont); } void AdjustUIFontForDIP(float dpi_scale, LOGFONT* logfont) { diff --git a/chromium/ui/base/l10n/l10n_util_win_unittest.cc b/chromium/ui/base/l10n/l10n_util_win_unittest.cc index 10e496338bc..c4ae8885b7e 100644 --- a/chromium/ui/base/l10n/l10n_util_win_unittest.cc +++ b/chromium/ui/base/l10n/l10n_util_win_unittest.cc @@ -9,7 +9,7 @@ #include "base/win/win_util.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" -#include "ui/base/win/dpi.h" +#include "ui/gfx/win/dpi.h" typedef PlatformTest L10nUtilWinTest; diff --git a/chromium/ui/base/l10n/time_format.cc b/chromium/ui/base/l10n/time_format.cc index 5ba606c3dfb..aaa35a99699 100644 --- a/chromium/ui/base/l10n/time_format.cc +++ b/chromium/ui/base/l10n/time_format.cc @@ -21,6 +21,7 @@ #include "third_party/icu/source/i18n/unicode/plurrule.h" #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/l10n/l10n_util_plurals.h" using base::Time; using base::TimeDelta; @@ -240,49 +241,20 @@ static base::LazyInstance<TimeFormatter> g_time_formatter = void TimeFormatter::BuildFormats( FormatType format_type, ScopedVector<icu::PluralFormat>* time_formats) { - const icu::UnicodeString kKeywords[] = { - UNICODE_STRING_SIMPLE("other"), UNICODE_STRING_SIMPLE("one"), - UNICODE_STRING_SIMPLE("zero"), UNICODE_STRING_SIMPLE("two"), - UNICODE_STRING_SIMPLE("few"), UNICODE_STRING_SIMPLE("many") - }; - UErrorCode err = U_ZERO_ERROR; - scoped_ptr<icu::PluralRules> rules( - icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); - if (U_FAILURE(err)) { - err = U_ZERO_ERROR; - icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); - rules.reset(icu::PluralRules::createRules(fallback_rules, err)); - DCHECK(U_SUCCESS(err)); - } - const MessageIDs& message_ids = GetMessageIDs(format_type); for (int i = 0; i < 4; ++i) { icu::UnicodeString pattern; - for (size_t j = 0; j < arraysize(kKeywords); ++j) { - int msg_id = message_ids.ids[i][j]; - std::string sub_pattern = l10n_util::GetStringUTF8(msg_id); - // NA means this keyword is not used in the current locale. - // Even if a translator translated for this keyword, we do not - // use it unless it's 'other' (j=0) or it's defined in the rules - // for the current locale. Special-casing of 'other' will be removed - // once ICU's isKeyword is fixed to return true for isKeyword('other'). - if (sub_pattern.compare("NA") != 0 && - (j == 0 || rules->isKeyword(kKeywords[j]))) { - pattern += kKeywords[j]; - pattern += UNICODE_STRING_SIMPLE("{"); - pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8"); - pattern += UNICODE_STRING_SIMPLE("}"); - } + std::vector<int> ids; + for (size_t j = 0; j < arraysize(message_ids.ids[i]); ++j) { + ids.push_back(message_ids.ids[i][j]); } - icu::PluralFormat* format = new icu::PluralFormat(*rules, pattern, err); - if (U_SUCCESS(err)) { - time_formats->push_back(format); + scoped_ptr<icu::PluralFormat> format = l10n_util::BuildPluralFormat(ids); + if (format) { + time_formats->push_back(format.release()); } else { - delete format; + scoped_ptr<icu::PluralRules> rules(l10n_util::BuildPluralRules()); time_formats->push_back(createFallbackFormat(*rules, i, format_type)); - // Reset it so that next ICU call can proceed. - err = U_ZERO_ERROR; } } } |