Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / l10n / formatter.h
blobfcd410f39e036a4f8dc9f82bbc33f72be904c83a
1 // Copyright 2014 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 // This file contains implementation details, the public interface is declared
6 // in time_format.h.
8 #ifndef UI_BASE_L10N_FORMATTER_H_
9 #define UI_BASE_L10N_FORMATTER_H_
11 #include "base/basictypes.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "third_party/icu/source/common/unicode/unistr.h"
15 #include "third_party/icu/source/i18n/unicode/msgfmt.h"
16 #include "third_party/icu/source/i18n/unicode/plurrule.h"
17 #include "ui/base/l10n/time_format.h"
18 #include "ui/base/ui_base_export.h"
20 namespace ui {
22 struct Pluralities;
24 // Formatter for a (format, length) combination. May either be instantiated
25 // with four parameters for use in TimeFormat::Simple() or with ten parameters
26 // for use in TimeFormat::Detailed().
27 class Formatter {
28 public:
29 enum Unit {
30 UNIT_SEC,
31 UNIT_MIN,
32 UNIT_HOUR,
33 UNIT_DAY,
34 UNIT_COUNT // Enum size counter, not a unit. Must be last.
36 enum TwoUnits {
37 TWO_UNITS_MIN_SEC,
38 TWO_UNITS_HOUR_MIN,
39 TWO_UNITS_DAY_HOUR,
40 TWO_UNITS_COUNT // Enum size counter, not a unit pair. Must be last.
43 Formatter(const Pluralities& sec_pluralities,
44 const Pluralities& min_pluralities,
45 const Pluralities& hour_pluralities,
46 const Pluralities& day_pluralities);
48 Formatter(const Pluralities& sec_pluralities,
49 const Pluralities& min_pluralities,
50 const Pluralities& hour_pluralities,
51 const Pluralities& day_pluralities,
52 const Pluralities& min_sec_pluralities1,
53 const Pluralities& min_sec_pluralities2,
54 const Pluralities& hour_min_pluralities1,
55 const Pluralities& hour_min_pluralities2,
56 const Pluralities& day_hour_pluralities1,
57 const Pluralities& day_hour_pluralities2);
59 void Format(Unit unit, int value, icu::UnicodeString* formatted_string) const;
61 void Format(TwoUnits units,
62 int value_1,
63 int value_2,
64 icu::UnicodeString* formatted_string) const;
66 private:
67 // Create a hard-coded fallback message format for plural formatting.
68 // This will never be called unless translators make a mistake.
69 scoped_ptr<icu::MessageFormat> CreateFallbackFormat(
70 const icu::PluralRules& rules,
71 const Pluralities& pluralities) const;
73 scoped_ptr<icu::MessageFormat> InitFormat(const Pluralities& pluralities);
75 scoped_ptr<icu::MessageFormat> simple_format_[UNIT_COUNT];
76 scoped_ptr<icu::MessageFormat> detailed_format_[TWO_UNITS_COUNT][2];
78 DISALLOW_IMPLICIT_CONSTRUCTORS(Formatter);
81 // Class to hold all Formatters, intended to be used in a global LazyInstance.
82 class UI_BASE_EXPORT FormatterContainer {
83 public:
84 FormatterContainer();
85 ~FormatterContainer();
87 const Formatter* Get(TimeFormat::Format format,
88 TimeFormat::Length length) const;
90 void ResetForTesting() {
91 Shutdown();
92 Initialize();
95 private:
96 void Initialize();
97 void Shutdown();
99 scoped_ptr<Formatter>
100 formatter_[TimeFormat::FORMAT_COUNT][TimeFormat::LENGTH_COUNT];
102 DISALLOW_COPY_AND_ASSIGN(FormatterContainer);
105 // Windows compilation requires full definition of FormatterContainer before
106 // LazyInstance<FormatterContainter> may be declared.
107 extern UI_BASE_EXPORT base::LazyInstance<FormatterContainer> g_container;
109 // For use in unit tests only.
110 extern UI_BASE_EXPORT bool formatter_force_fallback;
112 } // namespace ui
114 #endif // UI_BASE_L10N_FORMATTER_H_