| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/i18n/message_formatter.h" | 5 #include "base/i18n/message_formatter.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 EXPECT_EQ("The cert for example.com expired yesterday." + second_sentence, | 131 EXPECT_EQ("The cert for example.com expired yesterday." + second_sentence, |
| 132 result); | 132 result); |
| 133 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 133 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
| 134 pattern, "example.com", 15, now)); | 134 pattern, "example.com", 15, now)); |
| 135 EXPECT_EQ("The cert for example.com expired 15 days ago." + second_sentence, | 135 EXPECT_EQ("The cert for example.com expired 15 days ago." + second_sentence, |
| 136 result); | 136 result); |
| 137 } | 137 } |
| 138 | 138 |
| 139 TEST_F(MessageFormatterTest, DateTimeAndNumber) { | 139 TEST_F(MessageFormatterTest, DateTimeAndNumber) { |
| 140 // Note that using 'mph' for all locales is not a good i18n practice. | 140 // Note that using 'mph' for all locales is not a good i18n practice. |
| 141 // For various ways to format numbers, see |
| 142 // http://icu-project.org/apiref/icu4c/classicu_1_1MessageFormat.html and |
| 143 // http://icu-project.org/apiref/icu4c/classicu_1_1DecimalFormat.html |
| 141 const string16 pattern = ASCIIToUTF16( | 144 const string16 pattern = ASCIIToUTF16( |
| 142 "At {0,time, short} on {0,date, medium}, " | 145 "At {0,time, short} on {0,date, medium}, " |
| 143 "there was {1} at building {2,number,integer}. " | 146 "there was {1} at building {2,number,integer}. " |
| 144 "The speed of the wind was {3,number,###.#} mph."); | 147 "The speed of the wind was {3,number,###.#} mph." |
| 148 "It was a new record {4,number,percent} higher than the previous." |
| 149 "It was a new record {4,number,##.#%} higher than the previous." |
| 150 // padding example: '*' followed by a padding symbol. |
| 151 "We were given {5,number,integer}:{6,number,*0##} for evacuation."); |
| 145 | 152 |
| 146 using icu::DateFormat; | 153 using icu::DateFormat; |
| 147 std::unique_ptr<DateFormat> tf( | 154 std::unique_ptr<DateFormat> tf( |
| 148 DateFormat::createTimeInstance(DateFormat::SHORT)); | 155 DateFormat::createTimeInstance(DateFormat::SHORT)); |
| 149 std::unique_ptr<DateFormat> df( | 156 std::unique_ptr<DateFormat> df( |
| 150 DateFormat::createDateInstance(DateFormat::MEDIUM)); | 157 DateFormat::createDateInstance(DateFormat::MEDIUM)); |
| 151 | 158 |
| 152 base::Time now = base::Time::Now(); | 159 base::Time now = base::Time::Now(); |
| 153 std::string expected = "At "; | 160 std::string expected = "At "; |
| 154 AppendFormattedDateTime(tf, now, &expected); | 161 AppendFormattedDateTime(tf, now, &expected); |
| 155 expected.append(" on "); | 162 expected.append(" on "); |
| 156 AppendFormattedDateTime(df, now, &expected); | 163 AppendFormattedDateTime(df, now, &expected); |
| 157 expected.append(", there was an explosion at building 3. " | 164 expected.append(", there was an explosion at building 3. " |
| 158 "The speed of the wind was 37.4 mph."); | 165 "The speed of the wind was 37.4 mph." |
| 166 "It was a new record 6% higher than the previous." |
| 167 "It was a new record 5.8% higher than the previous." |
| 168 "We were given 3:05 for evacuation."); |
| 159 | 169 |
| 160 EXPECT_EQ(expected, UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 170 EXPECT_EQ(expected, UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
| 161 pattern, now, "an explosion", 3, 37.413))); | 171 pattern, now, "an explosion", 3, 37.413, 0.0578, 3, 5))); |
| 162 } | 172 } |
| 163 | 173 |
| 164 TEST_F(MessageFormatterTest, SelectorSingleOrMultiple) { | 174 TEST_F(MessageFormatterTest, SelectorSingleOrMultiple) { |
| 165 const string16 pattern = ASCIIToUTF16( | 175 const string16 pattern = ASCIIToUTF16( |
| 166 "{0, select," | 176 "{0, select," |
| 167 "single {Select a file to upload.}" | 177 "single {Select a file to upload.}" |
| 168 "multiple {Select files to upload.}" | 178 "multiple {Select files to upload.}" |
| 169 "other {UNUSED}}"); | 179 "other {UNUSED}}"); |
| 170 | 180 |
| 171 std::string result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 181 std::string result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
| 172 pattern, "single")); | 182 pattern, "single")); |
| 173 EXPECT_EQ("Select a file to upload.", result); | 183 EXPECT_EQ("Select a file to upload.", result); |
| 174 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 184 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
| 175 pattern, "multiple")); | 185 pattern, "multiple")); |
| 176 EXPECT_EQ("Select files to upload.", result); | 186 EXPECT_EQ("Select files to upload.", result); |
| 177 | 187 |
| 178 // fallback if a parameter is not selectors specified in the message pattern. | 188 // fallback if a parameter is not selectors specified in the message pattern. |
| 179 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 189 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
| 180 pattern, "foobar")); | 190 pattern, "foobar")); |
| 181 EXPECT_EQ("UNUSED", result); | 191 EXPECT_EQ("UNUSED", result); |
| 182 } | 192 } |
| 183 | 193 |
| 184 } // namespace i18n | 194 } // namespace i18n |
| 185 } // namespace base | 195 } // namespace base |
| OLD | NEW |