Commit 67d941ce authored by David Maunder's avatar David Maunder Committed by Commit Bot

Internationalize price drop formatted currency

The price formatting was originally hard coded for USD. It is now
internationalized. UX/PM requirements are not yet available for exact
formatting (e.g. commas, decimal places) so this change does not
finalize our price formatting but rather gives us the ability to
do some initial experimentation in other locales.

TBR=yusufo@chromium.org

Bug: 1130068
Change-Id: I075b035a560ad651ad166bc47a94031fc44859e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2556454
Commit-Queue: David Maunder <davidjm@chromium.org>
Reviewed-by: default avatarSahel Sharify <sahel@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#831129}
parent 18de5889
......@@ -60,6 +60,7 @@ android_library("java") {
"//components/external_intents/android:java",
"//components/find_in_page/android:java",
"//components/navigation_interception/android:navigation_interception_java",
"//components/payments/content/android:java",
"//content/public/android:content_java",
"//net/android:net_java",
"//third_party/android_deps:androidx_annotation_annotation_java",
......
include_rules = [
"+chrome/browser/tabpersistence",
"+components/payments/content/android",
]
......@@ -4,7 +4,6 @@
package org.chromium.chrome.browser.tab.state;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
......@@ -15,11 +14,13 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.chromium.base.Callback;
import org.chromium.base.LocaleUtils;
import org.chromium.base.Log;
import org.chromium.chrome.browser.endpoint_fetcher.EndpointFetcher;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.proto.ShoppingPersistedTabData.ShoppingPersistedTabDataProto;
import org.chromium.components.payments.CurrencyFormatter;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
......@@ -42,10 +43,12 @@ public class ShoppingPersistedTabData extends PersistedTabData {
private static final String BUYABLE_PRODUCT_ANNOTATION_KEY = "BUYABLE_PRODUCT";
private static final String BUYABLE_PRODUCT_KEY = "buyableProduct";
private static final String CURRENT_PRICE_KEY = "currentPrice";
private static final String CURRENCY_CODE_KEY = "currencyCode";
private static final String AMOUNT_MICROS_KEY = "amountMicros";
private static final String ACCEPT_LANGUAGE_KEY = "Accept-Language";
// TODO(crbug.com/1130068) support all locales
private static final String ACCEPT_LANGUAGE_VALUE = "en-US";
private static final int FRACTIONAL_DIGITS_LESS_THAN_TEN_UNITS = 2;
private static final int FRACTIONAL_DIGITS_GREATER_THAN_TEN_UNITS = 0;
private static final Class<ShoppingPersistedTabData> USER_DATA_KEY =
ShoppingPersistedTabData.class;
......@@ -69,6 +72,8 @@ public class ShoppingPersistedTabData extends PersistedTabData {
private long mPriceMicros = NO_PRICE_KNOWN;
private long mPreviousPriceMicros = NO_PRICE_KNOWN;
private String mCurrencyCode;
/**
* A price drop for the offer {@link ShoppingPersistedTabData}
* refers to
......@@ -124,7 +129,8 @@ public class ShoppingPersistedTabData extends PersistedTabData {
Profile.getLastUsedRegularProfile(),
String.format(ENDPOINT, tab.getUrlString()), HTTPS_METHOD, CONTENT_TYPE,
EMPTY_POST_DATA, TIMEOUT_MS,
new String[] {ACCEPT_LANGUAGE_KEY, ACCEPT_LANGUAGE_VALUE});
new String[] {
ACCEPT_LANGUAGE_KEY, LocaleUtils.getDefaultLocaleListString()});
},
ShoppingPersistedTabData.class, callback);
}
......@@ -142,6 +148,7 @@ public class ShoppingPersistedTabData extends PersistedTabData {
JSONObject priceMetadata = metadata.getJSONObject(CURRENT_PRICE_KEY);
res.setPriceMicros(Long.parseLong(priceMetadata.getString(AMOUNT_MICROS_KEY)),
previousShoppingPersistedTabData);
res.setCurrencyCode(priceMetadata.getString(CURRENCY_CODE_KEY));
break;
}
}
......@@ -178,6 +185,15 @@ public class ShoppingPersistedTabData extends PersistedTabData {
save();
}
protected void setCurrencyCode(String currencyCode) {
mCurrencyCode = currencyCode;
}
@VisibleForTesting
protected String getCurrencyCode() {
return mCurrencyCode;
}
@VisibleForTesting
protected void setPreviousPriceMicros(long previousPriceMicros) {
mPreviousPriceMicros = previousPriceMicros;
......@@ -249,12 +265,21 @@ public class ShoppingPersistedTabData extends PersistedTabData {
}
// TODO(crbug.com/1130068) support all currencies
private static String formatPrice(long priceMicros) {
private String formatPrice(long priceMicros) {
CurrencyFormatter currencyFormatter =
new CurrencyFormatter(mCurrencyCode, Locale.getDefault());
String formattedPrice;
if (priceMicros < TEN_UNITS) {
return String.format(Locale.US, "$%.2f", (100 * priceMicros / MICROS_TO_UNITS) / 100.0);
currencyFormatter.setMaximumFractionalDigits(FRACTIONAL_DIGITS_LESS_THAN_TEN_UNITS);
formattedPrice = String.format(
Locale.getDefault(), "%.2f", (100 * priceMicros / MICROS_TO_UNITS) / 100.0);
} else {
currencyFormatter.setMaximumFractionalDigits(FRACTIONAL_DIGITS_GREATER_THAN_TEN_UNITS);
formattedPrice = String.format(Locale.getDefault(), "%d",
(long) Math.floor(
(double) (priceMicros + MICROS_TO_UNITS / 2) / MICROS_TO_UNITS));
}
return String.format(Locale.US, "$%d",
(int) Math.floor((double) (priceMicros + MICROS_TO_UNITS / 2) / MICROS_TO_UNITS));
return currencyFormatter.format(formattedPrice);
}
@Override
......
......@@ -34,6 +34,12 @@ void CurrencyFormatterAndroid::Destroy(JNIEnv* env,
delete this;
}
void CurrencyFormatterAndroid::SetMaxFractionalDigits(
JNIEnv* env,
jint jmax_fractional_digits) {
currency_formatter_->SetMaxFractionalDigits(jmax_fractional_digits);
}
base::android::ScopedJavaLocalRef<jstring> CurrencyFormatterAndroid::Format(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
......
......@@ -29,6 +29,9 @@ class CurrencyFormatterAndroid {
void Destroy(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller);
// Set the maximum number of fractional digits.
void SetMaxFractionalDigits(JNIEnv* env, jint jnum_fractional_digits);
// Refer to CurrencyFormatter::Format documentation.
base::android::ScopedJavaLocalRef<jstring> Format(
JNIEnv* env,
......
......@@ -46,6 +46,15 @@ public class CurrencyFormatter {
}
}
/**
* Set the maximum number of fractional digits in the formatted price.
* @param maxFractionalDigits maximum number of fractional digits
*/
public void setMaximumFractionalDigits(int maxFractionalDigits) {
CurrencyFormatterJni.get().setMaxFractionalDigits(
mCurrencyFormatterAndroid, maxFractionalDigits);
}
/** @return The currency code formatted for display. */
public String getFormattedCurrencyCode() {
return CurrencyFormatterJni.get().getFormattedCurrencyCode(
......@@ -76,6 +85,7 @@ public class CurrencyFormatter {
void destroy(long nativeCurrencyFormatterAndroid, CurrencyFormatter caller);
String format(
long nativeCurrencyFormatterAndroid, CurrencyFormatter caller, String amountValue);
void setMaxFractionalDigits(long nativeCurrencyFormatterAndroid, int maxFractionalDigits);
String getFormattedCurrencyCode(
long nativeCurrencyFormatterAndroid, CurrencyFormatter caller);
}
......
......@@ -85,6 +85,10 @@ CurrencyFormatter::CurrencyFormatter(const std::string& currency_code,
CurrencyFormatter::~CurrencyFormatter() {}
void CurrencyFormatter::SetMaxFractionalDigits(const int maxFractionalDigits) {
icu_formatter_->setMaximumFractionDigits(maxFractionalDigits);
}
base::string16 CurrencyFormatter::Format(const std::string& amount) {
// It's possible that the ICU formatter didn't initialize properly.
if (!icu_formatter_ || !icu_formatter_->getCurrency())
......
......@@ -27,6 +27,10 @@ class CurrencyFormatter {
const std::string& locale_name);
~CurrencyFormatter();
// Set the maximum number of fractional digits. (kMaximumNumFractionalDigits
// is the default if unset)
void SetMaxFractionalDigits(const int maxFractionalDigits);
// Formats the |amount| according to the currency code that was set. The
// result will NOT contain the currency code, nor a subset of it. Rather, the
// caller of this function should display the currency code separately. The
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment