diff options
Diffstat (limited to 'ui/base/l10n/l10n_util_plurals.cc')
-rw-r--r-- | ui/base/l10n/l10n_util_plurals.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/ui/base/l10n/l10n_util_plurals.cc b/ui/base/l10n/l10n_util_plurals.cc new file mode 100644 index 0000000..0dc008f --- /dev/null +++ b/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 |