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. 4 5 // This file contains implementation details, the public interface is declared 6 // in time_format.h. 7 8 #ifndef UI_BASE_L10N_FORMATTER_H_ 9 #define UI_BASE_L10N_FORMATTER_H_ 10 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/plurfmt.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" 19 20 namespace ui { 21 22 struct Pluralities; 23 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. 35 }; 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. 41 }; 42 43 Formatter(const Pluralities& sec_pluralities, 44 const Pluralities& min_pluralities, 45 const Pluralities& hour_pluralities, 46 const Pluralities& day_pluralities); 47 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); 58 59 void Format(Unit unit, int value, icu::UnicodeString& formatted_string) const; 60 61 void Format(TwoUnits units, 62 int value_1, 63 int value_2, 64 icu::UnicodeString& formatted_string) const; 65 66 private: 67 // Create a hard-coded fallback plural format. This will never be called 68 // unless translators make a mistake. 69 scoped_ptr<icu::PluralFormat> CreateFallbackFormat( 70 const icu::PluralRules& rules, 71 const Pluralities& pluralities) const; 72 73 scoped_ptr<icu::PluralFormat> InitFormat(const Pluralities& pluralities); 74 75 scoped_ptr<icu::PluralFormat> simple_format_[UNIT_COUNT]; 76 scoped_ptr<icu::PluralFormat> detailed_format_[TWO_UNITS_COUNT][2]; 77 78 DISALLOW_IMPLICIT_CONSTRUCTORS(Formatter); 79 }; 80 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(); 86 87 const Formatter* Get(TimeFormat::Format format, 88 TimeFormat::Length length) const; 89 ResetForTesting()90 void ResetForTesting() { 91 Shutdown(); 92 Initialize(); 93 } 94 95 private: 96 void Initialize(); 97 void Shutdown(); 98 99 scoped_ptr<Formatter> 100 formatter_[TimeFormat::FORMAT_COUNT][TimeFormat::LENGTH_COUNT]; 101 102 DISALLOW_COPY_AND_ASSIGN(FormatterContainer); 103 }; 104 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; 108 109 // For use in unit tests only. 110 extern UI_BASE_EXPORT bool formatter_force_fallback; 111 112 } // namespace ui 113 114 #endif 115