Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(465)

Side by Side Diff: base/i18n/number_formatting.cc

Issue 1989563002: i18n of Zoom % to use locally correct numeric glyphs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/number_formatting.h" 5 #include "base/i18n/number_formatting.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/format_macros.h" 11 #include "base/format_macros.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "third_party/icu/source/common/unicode/ustring.h" 17 #include "third_party/icu/source/common/unicode/ustring.h"
18 #include "third_party/icu/source/i18n/unicode/numfmt.h" 18 #include "third_party/icu/source/i18n/unicode/numfmt.h"
19 19
20 namespace base { 20 namespace base {
21 21
22 namespace { 22 namespace {
23 23
24 // A simple wrapper around icu::NumberFormat that allows for resetting it 24 // A simple wrapper around icu::NumberFormat that allows for resetting it
25 // (as LazyInstance does not). 25 // (as LazyInstance does not).
26 struct NumberFormatWrapper { 26 struct FormatWrapper {
27 NumberFormatWrapper() { 27 void Reset(UNumberFormatStyle kind) {
28 Reset();
29 }
30
31 void Reset() {
32 // There's no ICU call to destroy a NumberFormat object other than 28 // There's no ICU call to destroy a NumberFormat object other than
33 // operator delete, so use the default Delete, which calls operator delete. 29 // operator delete, so use the default Delete, which calls operator delete.
34 // This can cause problems if a different allocator is used by this file 30 // This can cause problems if a different allocator is used by this file
35 // than by ICU. 31 // than by ICU.
36 UErrorCode status = U_ZERO_ERROR; 32 UErrorCode status = U_ZERO_ERROR;
37 number_format.reset(icu::NumberFormat::createInstance(status)); 33 number_format.reset(icu::NumberFormat::createInstance(
34 icu::Locale::getDefault(), kind, status));
38 DCHECK(U_SUCCESS(status)); 35 DCHECK(U_SUCCESS(status));
39 } 36 }
40 37
41 std::unique_ptr<icu::NumberFormat> number_format; 38 std::unique_ptr<icu::NumberFormat> number_format;
42 }; 39 };
43 40
41 struct NumberFormatWrapper : FormatWrapper {
42 NumberFormatWrapper() { Reset(UNUM_DECIMAL); }
43 };
44
45 struct PercentFormatWrapper : FormatWrapper {
46 PercentFormatWrapper() { Reset(UNUM_PERCENT); }
47 };
48
44 LazyInstance<NumberFormatWrapper> g_number_format_int = 49 LazyInstance<NumberFormatWrapper> g_number_format_int =
45 LAZY_INSTANCE_INITIALIZER; 50 LAZY_INSTANCE_INITIALIZER;
46 LazyInstance<NumberFormatWrapper> g_number_format_float = 51 LazyInstance<NumberFormatWrapper> g_number_format_float =
47 LAZY_INSTANCE_INITIALIZER; 52 LAZY_INSTANCE_INITIALIZER;
53 LazyInstance<PercentFormatWrapper> g_number_format_percent =
54 LAZY_INSTANCE_INITIALIZER;
48 55
49 } // namespace 56 } // namespace
50 57
51 string16 FormatNumber(int64_t number) { 58 string16 FormatNumber(int64_t number) {
52 icu::NumberFormat* number_format = 59 icu::NumberFormat* number_format =
53 g_number_format_int.Get().number_format.get(); 60 g_number_format_int.Get().number_format.get();
54 61
55 if (!number_format) { 62 if (!number_format) {
56 // As a fallback, just return the raw number in a string. 63 // As a fallback, just return the raw number in a string.
57 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); 64 return UTF8ToUTF16(StringPrintf("%" PRId64, number));
(...skipping 13 matching lines...) Expand all
71 return UTF8ToUTF16(StringPrintf("%f", number)); 78 return UTF8ToUTF16(StringPrintf("%f", number));
72 } 79 }
73 number_format->setMaximumFractionDigits(fractional_digits); 80 number_format->setMaximumFractionDigits(fractional_digits);
74 number_format->setMinimumFractionDigits(fractional_digits); 81 number_format->setMinimumFractionDigits(fractional_digits);
75 icu::UnicodeString ustr; 82 icu::UnicodeString ustr;
76 number_format->format(number, ustr); 83 number_format->format(number, ustr);
77 84
78 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); 85 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
79 } 86 }
80 87
88 string16 FormatPercent(int number) {
89 return FormatPercentFromDouble(static_cast<double>(number) / 100.0, 0);
Greg Levin 2016/05/17 18:17:44 I don't totally understand why the NumberFormat ob
jungshik at Google 2016/05/17 23:58:42 Well, none of percent format used in our UI has fr
Greg Levin 2016/05/18 21:33:44 Sure. This function can just be return i18n::Mes
90 }
91
92 string16 FormatPercentFromDouble(double number, int fractional_digits) {
93 icu::NumberFormat* percent_format =
94 g_number_format_percent.Get().number_format.get();
95
96 if (!percent_format) {
97 // As a fallback, just return the default percent formatting in a string.
98 return UTF8ToUTF16(
99 StringPrintf("%.*f%%", fractional_digits, number * 100.0));
100 }
101 percent_format->setMaximumFractionDigits(fractional_digits);
102 percent_format->setMinimumFractionDigits(fractional_digits);
103 icu::UnicodeString ustr;
104 percent_format->format(number, ustr);
105
106 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
107 }
108
81 namespace testing { 109 namespace testing {
82 110
83 void ResetFormatters() { 111 void ResetFormatters() {
84 g_number_format_int.Get().Reset(); 112 g_number_format_int.Get().Reset(UNUM_DECIMAL);
85 g_number_format_float.Get().Reset(); 113 g_number_format_float.Get().Reset(UNUM_DECIMAL);
114 g_number_format_percent.Get().Reset(UNUM_PERCENT);
86 } 115 }
87 116
88 } // namespace testing 117 } // namespace testing
89 118
90 } // namespace base 119 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698