1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/l10n/l10n_util_plurals.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "ui/base/l10n/l10n_util.h"
12 scoped_ptr
<icu::PluralRules
> BuildPluralRules() {
13 UErrorCode err
= U_ZERO_ERROR
;
14 scoped_ptr
<icu::PluralRules
> rules(
15 icu::PluralRules::forLocale(icu::Locale::getDefault(), err
));
18 icu::UnicodeString
fallback_rules("one: n is 1", -1, US_INV
);
19 rules
.reset(icu::PluralRules::createRules(fallback_rules
, err
));
20 DCHECK(U_SUCCESS(err
));
25 scoped_ptr
<icu::PluralFormat
> BuildPluralFormat(
26 const std::vector
<int>& message_ids
) {
27 const icu::UnicodeString kKeywords
[] = {
28 UNICODE_STRING_SIMPLE("other"),
29 UNICODE_STRING_SIMPLE("one"),
30 UNICODE_STRING_SIMPLE("zero"),
31 UNICODE_STRING_SIMPLE("two"),
32 UNICODE_STRING_SIMPLE("few"),
33 UNICODE_STRING_SIMPLE("many"),
35 DCHECK_EQ(message_ids
.size(), arraysize(kKeywords
));
36 UErrorCode err
= U_ZERO_ERROR
;
37 scoped_ptr
<icu::PluralRules
> rules(BuildPluralRules());
39 icu::UnicodeString pattern
;
40 for (size_t i
= 0; i
< arraysize(kKeywords
); ++i
) {
41 int msg_id
= message_ids
[i
];
42 std::string sub_pattern
= GetStringUTF8(msg_id
);
43 // NA means this keyword is not used in the current locale.
44 // Even if a translator translated for this keyword, we do not
45 // use it unless it's 'other' (i=0) or it's defined in the rules
46 // for the current locale. Special-casing of 'other' will be removed
47 // once ICU's isKeyword is fixed to return true for isKeyword('other').
48 if (sub_pattern
.compare("NA") != 0 &&
49 (i
== 0 || rules
->isKeyword(kKeywords
[i
]))) {
50 pattern
+= kKeywords
[i
];
51 pattern
+= UNICODE_STRING_SIMPLE("{");
52 pattern
+= icu::UnicodeString(sub_pattern
.c_str(), "UTF-8");
53 pattern
+= UNICODE_STRING_SIMPLE("}");
56 scoped_ptr
<icu::PluralFormat
> format
= scoped_ptr
<icu::PluralFormat
>(
57 new icu::PluralFormat(*rules
, pattern
, err
));
58 if (!U_SUCCESS(err
)) {
59 return scoped_ptr
<icu::PluralFormat
>();
64 } // namespace l10n_util