summaryrefslogtreecommitdiff
path: root/chromium/ui/base/l10n/l10n_util_plurals.cc
blob: 0dc008ff06edb5b10355e2b95e57fb32a97ed8ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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