This source file includes following definitions.
- BuildPluralRules
- BuildPluralFormat
#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);
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();
}
}