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

Side by Side Diff: components/payments/currency_formatter.cc

Issue 2621033003: [Payments] Currency formatter for order amounts. (Closed)
Patch Set: Addressed comments Created 3 years, 11 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
(Empty)
1 // Copyright 2017 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 #include "components/payments/currency_formatter.h"
6
7 #include <memory>
8
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "third_party/icu/source/common/unicode/stringpiece.h"
12 #include "third_party/icu/source/common/unicode/uchar.h"
13 #include "third_party/icu/source/common/unicode/unistr.h"
14 #include "third_party/icu/source/common/unicode/utypes.h"
15
16 namespace payments {
17
18 const char kIso4217CurrencySystem[] = "urn:iso:std:iso:4217";
19 namespace {
20
21 // Support a maximum of 10 fractional digits, similar to the ISO20022 standard.
22 // https://www.iso20022.org/standardsrepository/public/wqt/Description/mx/dico/
23 // datatypes/_L8ZcEp0gEeOo48XfssNw8w
24 const int kMaximumNumFractionalDigits = 10;
25
26 // Max currency code length. Length of currency code can be at most 2048.
27 const static size_t kMaxCurrencyCodeLength = 2048;
28
29 // Returns whether the |currency_code| is valid to be used in ICU.
30 bool ShouldUseCurrencyCode(const std::string& currency_code,
31 const base::Optional<std::string> currency_system) {
32 return currency_system.value_or(kIso4217CurrencySystem) ==
33 kIso4217CurrencySystem &&
34 !currency_code.empty() &&
35 currency_code.size() <= kMaxCurrencyCodeLength;
36 }
37
38 } // namespace
39
40 CurrencyFormatter::CurrencyFormatter(
41 const std::string& currency_code,
42 const base::Optional<std::string> currency_system,
43 const std::string& locale_name)
44 : locale_(locale_name.c_str()) {
45 UErrorCode error_code = U_ZERO_ERROR;
46 icu_formatter_.reset(
47 icu::NumberFormat::createCurrencyInstance(locale_, error_code));
48 if (U_FAILURE(error_code)) {
49 icu::UnicodeString name;
50 std::string locale_str;
51 locale_.getDisplayName(name).toUTF8String(locale_str);
52 LOG(ERROR) << "Failed to initialize the currency formatter for "
53 << locale_str;
54 return;
55 }
56
57 if (ShouldUseCurrencyCode(currency_code, currency_system)) {
58 currency_code_.reset(
59 new icu::UnicodeString(currency_code.c_str(), currency_code.length()));
60 } else {
61 // For non-ISO4217 currency system/code, we use a dummy code which is not
62 // going to appear in the output (stripped in Format()). This is because ICU
63 // NumberFormat will not accept an empty currency code. Under these
64 // circumstances, the number amount will be formatted according to locale,
65 // which is desirable (e.g. "55.00" -> "55,00" in fr_FR).
66 currency_code_.reset(new icu::UnicodeString("DUM", 3));
67 }
68
69 icu_formatter_->setCurrency(currency_code_->getBuffer(), error_code);
70 if (U_FAILURE(error_code)) {
71 std::string currency_code_str;
72 currency_code_->toUTF8String(currency_code_str);
73 LOG(ERROR) << "Could not set currency code on currency formatter: "
74 << currency_code_str;
75 return;
76 }
77
78 icu_formatter_->setMaximumFractionDigits(kMaximumNumFractionalDigits);
79 }
80
81 CurrencyFormatter::~CurrencyFormatter() {}
82
83 base::string16 CurrencyFormatter::Format(const std::string& amount) {
84 // It's possible that the ICU formatter didn't initialize properly.
85 if (!icu_formatter_ || !icu_formatter_->getCurrency())
86 return base::UTF8ToUTF16(amount);
87
88 icu::UnicodeString output;
89 UErrorCode error_code = U_ZERO_ERROR;
90 icu_formatter_->format(icu::StringPiece(amount.c_str()), output, nullptr,
91 error_code);
92
93 if (output.isEmpty())
94 return base::UTF8ToUTF16(amount);
95
96 // Explicitly removes the currency code (truncated to its 3-letter and
97 // 2-letter versions) from the output, because callers are expected to
98 // display the currency code alongside this result.
99 //
100 // 3+ letters: If currency code is "ABCDEF" or "BTX", this code will
101 // transform "ABC55.00"/"BTX55.00" to "55.00".
102 // 2 letters: If currency code is "CAD", this code will transform "CA$55.00"
103 // to "$55.00" (en_US) or "55,00 $ CA" to "55,00 $" (fr_FR).
104 icu::UnicodeString tmp_currency_code(*currency_code_);
105 tmp_currency_code.truncate(3);
106 output.findAndReplace(tmp_currency_code, "");
107 tmp_currency_code.truncate(2);
108 output.findAndReplace(tmp_currency_code, "");
109 // Trims any unicode whitespace (including non-breaking space).
110 if (u_isUWhiteSpace(output[0])) {
111 output.remove(0, 1);
112 }
113 if (u_isUWhiteSpace(output[output.length() - 1])) {
114 output.remove(output.length() - 1, 1);
115 }
116
117 std::string output_str;
118 output.toUTF8String(output_str);
119 return base::UTF8ToUTF16(output_str);
120 }
121
122 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698