Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(528)

Side by Side Diff: ui/base/l10n/l10n_util_plurals.cc

Issue 1049513002: Use the ICU syntax message for plural formatting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ios whitelist update Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/l10n/l10n_util_plurals.h ('k') | ui/base/l10n/time_format.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/l10n/l10n_util_plurals.h" 5 #include "ui/base/l10n/l10n_util_plurals.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "ui/base/l10n/l10n_util.h" 9 #include "ui/base/l10n/l10n_util.h"
10 10
11 namespace l10n_util { 11 namespace l10n_util {
12 12
13 scoped_ptr<icu::PluralRules> BuildPluralRules() { 13 scoped_ptr<icu::PluralRules> BuildPluralRules() {
14 UErrorCode err = U_ZERO_ERROR; 14 UErrorCode err = U_ZERO_ERROR;
15 scoped_ptr<icu::PluralRules> rules( 15 scoped_ptr<icu::PluralRules> rules(
16 icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); 16 icu::PluralRules::forLocale(icu::Locale::getDefault(), err));
17 if (U_FAILURE(err)) { 17 if (U_FAILURE(err)) {
18 err = U_ZERO_ERROR; 18 err = U_ZERO_ERROR;
19 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); 19 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV);
20 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); 20 rules.reset(icu::PluralRules::createRules(fallback_rules, err));
21 DCHECK(U_SUCCESS(err)); 21 DCHECK(U_SUCCESS(err));
22 } 22 }
23 return rules.Pass(); 23 return rules.Pass();
24 } 24 }
25 25
26 scoped_ptr<icu::PluralFormat> BuildPluralFormat( 26 void FormatNumberInPlural(const icu::MessageFormat& format, int number,
27 const std::vector<int>& message_ids) { 27 icu::UnicodeString* result, UErrorCode* err) {
28 const icu::UnicodeString kKeywords[] = { 28 if (U_FAILURE(*err)) return;
29 UNICODE_STRING_SIMPLE("other"), 29 icu::Formattable formattable(number);
30 UNICODE_STRING_SIMPLE("one"), 30 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
31 UNICODE_STRING_SIMPLE("zero"), 31 format.format(&formattable, 1, *result, ignore, *err);
32 UNICODE_STRING_SIMPLE("two"), 32 DCHECK(U_SUCCESS(*err));
33 UNICODE_STRING_SIMPLE("few"), 33 return;
34 UNICODE_STRING_SIMPLE("many"),
35 };
36 DCHECK_EQ(message_ids.size(), arraysize(kKeywords));
37 UErrorCode err = U_ZERO_ERROR;
38 scoped_ptr<icu::PluralRules> rules(BuildPluralRules());
39
40 icu::UnicodeString pattern;
41 for (size_t i = 0; i < arraysize(kKeywords); ++i) {
42 int msg_id = message_ids[i];
43 std::string sub_pattern = GetStringUTF8(msg_id);
44 // NA means this keyword is not used in the current locale.
45 // Even if a translator translated for this keyword, we do not
46 // use it unless it's 'other' (i=0) or it's defined in the rules
47 // for the current locale. Special-casing of 'other' will be removed
48 // once ICU's isKeyword is fixed to return true for isKeyword('other').
49 if (sub_pattern.compare("NA") != 0 &&
50 (i == 0 || rules->isKeyword(kKeywords[i]))) {
51 pattern += kKeywords[i];
52 pattern += UNICODE_STRING_SIMPLE("{");
53 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8");
54 pattern += UNICODE_STRING_SIMPLE("}");
55 }
56 }
57 scoped_ptr<icu::PluralFormat> format =
58 make_scoped_ptr(new icu::PluralFormat(*rules, pattern, err));
59 if (!U_SUCCESS(err)) {
60 return nullptr;
61 }
62 return format.Pass();
63 } 34 }
64 35
36
65 } // namespace l10n_util 37 } // namespace l10n_util
OLDNEW
« no previous file with comments | « ui/base/l10n/l10n_util_plurals.h ('k') | ui/base/l10n/time_format.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698