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

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

Issue 23591040: Use ICU for string pluralization in the extension permission dialog. (Closed) Base URL: http://git.chromium.org/chromium/src.git@gtk-extension-install-dialog
Patch Set: Created 7 years, 3 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
OLDNEW
(Empty)
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.
4
5 #include "ui/base/l10n/l10n_util_internal.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "ui/base/l10n/l10n_util.h"
9
10 namespace l10n_util {
11
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));
16 if (U_FAILURE(err)) {
17 err = U_ZERO_ERROR;
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));
21 }
22 return rules.Pass();
23 }
24
25 scoped_ptr<icu::PluralFormat> BuildPluralFormat(std::vector<int> message_ids) {
26 const icu::UnicodeString kKeywords[] = {
27 UNICODE_STRING_SIMPLE("other"),
28 UNICODE_STRING_SIMPLE("one"),
29 UNICODE_STRING_SIMPLE("zero"),
30 UNICODE_STRING_SIMPLE("two"),
31 UNICODE_STRING_SIMPLE("few"),
32 UNICODE_STRING_SIMPLE("many"),
33 };
34 DCHECK_EQ(message_ids.size(), arraysize(kKeywords));
35 UErrorCode err = U_ZERO_ERROR;
36 scoped_ptr<icu::PluralRules> rules(BuildPluralRules());
37
38 icu::UnicodeString pattern;
39 for (size_t i = 0; i < arraysize(kKeywords); ++i) {
40 int msg_id = message_ids[i];
41 std::string sub_pattern = GetStringUTF8(msg_id);
42 // NA means this keyword is not used in the current locale.
43 // Even if a translator translated for this keyword, we do not
44 // use it unless it's 'other' (i=0) or it's defined in the rules
45 // for the current locale. Special-casing of 'other' will be removed
46 // once ICU's isKeyword is fixed to return true for isKeyword('other').
47 if (sub_pattern.compare("NA") != 0 &&
48 (i == 0 || rules->isKeyword(kKeywords[i]))) {
49 pattern += kKeywords[i];
50 pattern += UNICODE_STRING_SIMPLE("{");
51 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8");
52 pattern += UNICODE_STRING_SIMPLE("}");
53 }
54 }
55 scoped_ptr<icu::PluralFormat> format = scoped_ptr<icu::PluralFormat>(
56 new icu::PluralFormat(*rules, pattern, err));
57 if (!U_SUCCESS(err)) {
58 return scoped_ptr<icu::PluralFormat>();
59 }
60 return format.Pass();
61 }
62
63 } // namespace l10n_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698