From: Damyan Ivanov Date: Thu, 9 Jan 2020 21:10:04 +0000 (+0200) Subject: machinery for tracking currency format (e.g. locale) changes X-Git-Tag: v0.12.0~101 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=208de18485b4706d133ba820101ccf04ad20e947;p=mobile-ledger.git machinery for tracking currency format (e.g. locale) changes --- diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index bec320e0..fb69138b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/App.java +++ b/app/src/main/java/net/ktnx/mobileledger/App.java @@ -33,6 +33,7 @@ import java.net.Authenticator; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.URL; +import java.util.Locale; public class App extends Application { public static App instance; @@ -48,6 +49,7 @@ public class App extends Application { instance = this; super.onCreate(); updateMonthNames(); + Data.refreshCurrencyData(Locale.getDefault()); Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 4a79095a..2cc40198 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -33,6 +33,7 @@ import net.ktnx.mobileledger.utils.MLDB; import net.ktnx.mobileledger.utils.ObservableList; import java.lang.ref.WeakReference; +import java.text.NumberFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -51,6 +52,8 @@ public final class Data { public static MutableLiveData> profiles = new MutableLiveData<>(null); public static MutableLiveData accountFilter = new MutableLiveData<>(); + public static MutableLiveData currencySymbolPosition = + new MutableLiveData<>(); private static AtomicInteger backgroundTaskCount = new AtomicInteger(0); private static Locker profilesLocker = new Locker(); private static RetrieveTransactionsTask retrieveTransactionsTask; @@ -149,4 +152,21 @@ public final class Data { public static void transactionRetrievalDone() { retrieveTransactionsTask = null; } -} + public static void refreshCurrencyData(Locale locale) { + NumberFormat formatter = NumberFormat.getCurrencyInstance(locale); + java.util.Currency currency = formatter.getCurrency(); + Logger.debug("locale", + String.format("Discovering currency symbol position for locale %s (currency is %s)", + locale.toString(), currency.toString())); + String formatted = formatter.format(1234.56f); + Logger.debug("locale", String.format("1234.56 formats as '%s'", formatted)); + String symbol = currency.getSymbol(); + if (formatted.startsWith(symbol)) + currencySymbolPosition.setValue(Currency.Position.before); + else if (formatted.endsWith(symbol)) + currencySymbolPosition.setValue(Currency.Position.after); + else + currencySymbolPosition.setValue(Currency.Position.none); + } + +} \ No newline at end of file