Move local to common, refactor setting of default locale

This commit is contained in:
Manfred Karrer 2017-03-16 16:56:47 -05:00
parent 63456ac255
commit f174024ac7
10 changed files with 56 additions and 22 deletions

View File

@ -19,7 +19,6 @@ package io.bisq.locale;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import io.bisq.user.Preferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,6 +27,7 @@ import java.util.stream.Collectors;
public class CountryUtil {
private static final Logger log = LoggerFactory.getLogger(CountryUtil.class);
private static Locale defaultLocale;
public static List<Country> getAllSepaEuroCountries(Locale locale) {
List<Country> list = new ArrayList<>();
@ -82,7 +82,7 @@ public class CountryUtil {
}
public static String getNameByCode(String countryCode) {
return getNameByCode(countryCode, Preferences.getDefaultLocale());
return new Locale(LanguageUtil.getDefaultLanguage(defaultLocale), countryCode).getDisplayCountry();
}
public static String getNameByCode(String countryCode, Locale locale) {
@ -90,7 +90,7 @@ public class CountryUtil {
}
public static String getNameAndCode(String countryCode) {
return getNameByCode(countryCode, Preferences.getDefaultLocale());
return getNameByCode(countryCode, defaultLocale);
}
public static String getNameAndCode(String countryCode, Locale locale) {
@ -217,4 +217,12 @@ public class CountryUtil {
// might be set later in pref or config, so not use Preferences.getDefaultLocale() anywhere in the code
return locale.getCountry();
}
public static void setDefaultLocale(Locale defaultLocale) {
CountryUtil.defaultLocale = defaultLocale;
}
public static Locale getDefaultLocale() {
return defaultLocale;
}
}

View File

@ -17,7 +17,6 @@
package io.bisq.locale;
import io.bisq.user.Preferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,9 +27,19 @@ public class CurrencyUtil {
private static final Logger log = LoggerFactory.getLogger(CurrencyUtil.class);
private static List<FiatCurrency> allSortedFiatCurrencies;
private static Locale defaultLocale;
private static FiatCurrency defaultTradeCurrency;
public static void setDefaultLocale(Locale defaultLocale) {
CurrencyUtil.defaultLocale = defaultLocale;
}
public static void setDefaultTradeCurrency(FiatCurrency defaultTradeCurrency) {
CurrencyUtil.defaultTradeCurrency = defaultTradeCurrency;
}
private static List<FiatCurrency> createAllSortedFiatCurrenciesList() {
return createAllSortedFiatCurrenciesList(Preferences.getDefaultLocale());
return createAllSortedFiatCurrenciesList(defaultLocale);
}
private static List<FiatCurrency> createAllSortedFiatCurrenciesList(Locale locale) {
@ -43,7 +52,7 @@ public class CurrencyUtil {
}
public static List<FiatCurrency> getAllSortedFiatCurrencies() {
return getAllSortedFiatCurrencies(Preferences.getDefaultLocale());
return getAllSortedFiatCurrencies(defaultLocale);
}
public static List<FiatCurrency> getAllSortedFiatCurrencies(Locale locale) {
@ -54,7 +63,7 @@ public class CurrencyUtil {
}
public static List<FiatCurrency> getAllMainFiatCurrencies() {
return getAllMainFiatCurrencies(Preferences.getDefaultLocale(), Preferences.getDefaultTradeCurrency());
return getAllMainFiatCurrencies(defaultLocale, defaultTradeCurrency);
}
public static List<FiatCurrency> getAllMainFiatCurrencies(Locale locale, TradeCurrency defaultTradeCurrency) {
@ -326,7 +335,7 @@ public class CurrencyUtil {
}
public static FiatCurrency getCurrencyByCountryCode(String countryCode) {
return getCurrencyByCountryCode(countryCode, Preferences.getDefaultLocale());
return getCurrencyByCountryCode(countryCode, defaultLocale);
}
public static FiatCurrency getCurrencyByCountryCode(String countryCode, Locale locale) {
@ -335,7 +344,7 @@ public class CurrencyUtil {
public static String getNameByCode(String currencyCode) {
return getNameAndCode(currencyCode, Preferences.getDefaultLocale());
return getNameAndCode(currencyCode, defaultLocale);
}
public static String getNameByCode(String currencyCode, Locale locale) {
@ -351,7 +360,7 @@ public class CurrencyUtil {
}
public static String getNameAndCode(String currencyCode) {
return getNameAndCode(currencyCode, Preferences.getDefaultLocale());
return getNameAndCode(currencyCode, defaultLocale);
}
public static String getNameAndCode(String currencyCode, Locale locale) {

View File

@ -18,7 +18,6 @@
package io.bisq.locale;
import io.bisq.app.Version;
import io.bisq.user.Preferences;
import java.util.Currency;
import java.util.Locale;
@ -30,10 +29,16 @@ public final class FiatCurrency extends TradeCurrency {
// http://boschista.deviantart.com/journal/Cool-ASCII-Symbols-214218618
private final static String PREFIX = "";
private static Locale defaultLocale;
public static void setDefaultLocale(Locale defaultLocale) {
FiatCurrency.defaultLocale = defaultLocale;
}
private final Currency currency;
public FiatCurrency(String currencyCode) {
this(currencyCode, Preferences.getDefaultLocale());
this(currencyCode, defaultLocale);
}
public FiatCurrency(String currencyCode, Locale locale) {
@ -42,7 +47,7 @@ public final class FiatCurrency extends TradeCurrency {
@SuppressWarnings("WeakerAccess")
public FiatCurrency(Currency currency) {
this(currency, Preferences.getDefaultLocale());
this(currency, defaultLocale);
}
@SuppressWarnings("WeakerAccess")

View File

@ -17,7 +17,6 @@
package io.bisq.locale;
import io.bisq.user.Preferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -80,6 +79,7 @@ public class LanguageUtil {
"mt" // Maltese
*/
);
private static Locale defaultLocale;
public static List<String> getAllLanguageCodes() {
List<Locale> allLocales = LocaleUtil.getAllLocales();
@ -97,12 +97,12 @@ public class LanguageUtil {
}
public static String getDefaultLanguage(Locale locale) {
// might be set later in pref or config, so not use Preferences.getDefaultLocale() anywhere in the code
// might be set later in pref or config, so not use defaultLocale anywhere in the code
return locale.getLanguage();
}
public static String getDefaultLanguageLocaleAsCode() {
return getDefaultLanguageLocaleAsCode(Preferences.getDefaultLocale());
return getDefaultLanguageLocaleAsCode(defaultLocale);
}
public static String getDefaultLanguageLocaleAsCode(Locale locale) {
@ -121,4 +121,8 @@ public class LanguageUtil {
public static List<String> getUserLanguageCodes() {
return userLanguageCodes;
}
public static void setDefaultLocale(Locale defaultLocale) {
LanguageUtil.defaultLocale = defaultLocale;
}
}

View File

@ -50,10 +50,19 @@ public final class Preferences implements Persistable {
public static Preferences INSTANCE;
static {
defaultLocale = Locale.getDefault();
Locale locale = Locale.getDefault();
Preferences.defaultLocale = locale;
Res.applyLocaleToResourceBundle(getDefaultLocale());
CountryUtil.setDefaultLocale(locale);
CurrencyUtil.setDefaultLocale(locale);
LanguageUtil.setDefaultLocale(locale);
FiatCurrency.setDefaultLocale(locale);
FiatCurrency currencyByCountryCode = CurrencyUtil.getCurrencyByCountryCode(CountryUtil.getDefaultCountryCode(locale), locale);
Preferences.defaultTradeCurrency = currencyByCountryCode;
CurrencyUtil.setDefaultTradeCurrency(currencyByCountryCode);
}
@ -94,7 +103,7 @@ public final class Preferences implements Persistable {
return defaultLocale;
}
private static TradeCurrency defaultTradeCurrency = CurrencyUtil.getCurrencyByCountryCode(CountryUtil.getDefaultCountryCode(defaultLocale), defaultLocale);
private static TradeCurrency defaultTradeCurrency;
public static TradeCurrency getDefaultTradeCurrency() {
return defaultTradeCurrency;

View File

@ -42,7 +42,6 @@ import io.bisq.network_messages.trade.offer.payload.OfferPayload;
import io.bisq.network_messages.trade.payload.Contract;
import io.bisq.network_messages.trade.protocol.trade.messages.*;
import io.bisq.network_messages.trade.statistics.payload.TradeStatistics;
import io.bisq.user.Preferences;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.output.WriterOutputStream;
import org.bitcoinj.core.Coin;
@ -345,7 +344,7 @@ public class ProtoBufferUtilities {
break;
case SEPA_ACCOUNT_CONTRACT_DATA:
SepaAccountContractData sepaAccountContractData = new SepaAccountContractData(protoEntry.getPaymentMethodName(), protoEntry.getId(),
protoEntry.getMaxTradePeriod(), CountryUtil.getAllSepaCountries(Preferences.getDefaultLocale()));
protoEntry.getMaxTradePeriod(), CountryUtil.getAllSepaCountries(CountryUtil.getDefaultLocale()));
fillInCountryBasedPaymentAccountContractData(protoEntry, sepaAccountContractData);
result = sepaAccountContractData;
break;