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
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"
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().
34 UNIT_COUNT
// Enum size counter, not a unit. Must be last.
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
,
64 icu::UnicodeString
* formatted_string
) const;
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
{
85 ~FormatterContainer();
87 const Formatter
* Get(TimeFormat::Format format
,
88 TimeFormat::Length length
) const;
90 void ResetForTesting() {
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
;
114 #endif // UI_BASE_L10N_FORMATTER_H_