Use SupportedLocale in Config

This commit is contained in:
yushijinhun
2018-07-29 14:15:32 +08:00
parent 49e70e7195
commit d9aeb0cc2b
6 changed files with 28 additions and 24 deletions

View File

@@ -33,6 +33,8 @@ import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
import org.jackhuang.hmcl.util.EnumOrdinalDeserializer; import org.jackhuang.hmcl.util.EnumOrdinalDeserializer;
import org.jackhuang.hmcl.util.FileTypeAdapter; import org.jackhuang.hmcl.util.FileTypeAdapter;
import org.jackhuang.hmcl.util.ObservableHelper; import org.jackhuang.hmcl.util.ObservableHelper;
import org.jackhuang.hmcl.util.i18n.Locales;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@@ -67,6 +69,7 @@ public final class Config implements Cloneable, Observable {
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator()) .registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true)) .registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true))
.registerTypeAdapter(Theme.class, new Theme.TypeAdapter()) .registerTypeAdapter(Theme.class, new Theme.TypeAdapter())
.registerTypeAdapter(SupportedLocale.class, new SupportedLocale.TypeAdapter())
.registerTypeAdapter(EnumBackgroundImage.class, new EnumOrdinalDeserializer<>(EnumBackgroundImage.class)) // backward compatibility for backgroundType .registerTypeAdapter(EnumBackgroundImage.class, new EnumOrdinalDeserializer<>(EnumBackgroundImage.class)) // backward compatibility for backgroundType
.registerTypeAdapter(Proxy.Type.class, new EnumOrdinalDeserializer<>(Proxy.Type.class)) // backward compatibility for hasProxy .registerTypeAdapter(Proxy.Type.class, new EnumOrdinalDeserializer<>(Proxy.Type.class)) // backward compatibility for hasProxy
.setPrettyPrinting() .setPrettyPrinting()
@@ -120,7 +123,7 @@ public final class Config implements Cloneable, Observable {
private ObjectProperty<Theme> theme = new SimpleObjectProperty<>(Theme.BLUE); private ObjectProperty<Theme> theme = new SimpleObjectProperty<>(Theme.BLUE);
@SerializedName("localization") @SerializedName("localization")
private StringProperty localization = new SimpleStringProperty(); private ObjectProperty<SupportedLocale> localization = new SimpleObjectProperty<>(Locales.DEFAULT);
@SerializedName("downloadtype") @SerializedName("downloadtype")
private IntegerProperty downloadType = new SimpleIntegerProperty(1); private IntegerProperty downloadType = new SimpleIntegerProperty(1);
@@ -348,15 +351,15 @@ public final class Config implements Cloneable, Observable {
return theme; return theme;
} }
public String getLocalization() { public SupportedLocale getLocalization() {
return localization.get(); return localization.get();
} }
public void setLocalization(String localization) { public void setLocalization(SupportedLocale localization) {
this.localization.set(localization); this.localization.set(localization);
} }
public StringProperty localizationProperty() { public ObjectProperty<SupportedLocale> localizationProperty() {
return localization; return localization;
} }

View File

@@ -25,7 +25,6 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.event.*; import org.jackhuang.hmcl.event.*;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.i18n.Locales;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -65,17 +64,6 @@ public class Settings {
config().addListener(source -> ConfigHolder.saveConfig()); config().addListener(source -> ConfigHolder.saveConfig());
} }
private Locales.SupportedLocale locale = Locales.getLocaleByName(config().getLocalization());
public Locales.SupportedLocale getLocale() {
return locale;
}
public void setLocale(Locales.SupportedLocale locale) {
this.locale = locale;
config().setLocalization(Locales.getNameByLocale(locale));
}
public Font getFont() { public Font getFont() {
return Font.font(config().getFontFamily(), config().getFontSize()); return Font.font(config().getFontFamily(), config().getFontSize());
} }

View File

@@ -57,7 +57,6 @@ import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD;
import org.jackhuang.hmcl.setting.ConfigHolder; import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.setting.EnumBackgroundImage; import org.jackhuang.hmcl.setting.EnumBackgroundImage;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.setting.Theme; import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.ui.animation.AnimationProducer; import org.jackhuang.hmcl.ui.animation.AnimationProducer;
import org.jackhuang.hmcl.ui.animation.ContainerAnimations; import org.jackhuang.hmcl.ui.animation.ContainerAnimations;
@@ -200,7 +199,7 @@ public final class Decorator extends StackPane implements TaskExecutorDialogWiza
); );
nowAnimation.play(); nowAnimation.play();
}); });
if (!ConfigHolder.isNewlyCreated() || Settings.instance().getLocale().getLocale() != Locale.CHINA) if (!ConfigHolder.isNewlyCreated() || config().getLocalization().getLocale() != Locale.CHINA)
drawerWrapper.getChildren().remove(welcomeView); drawerWrapper.getChildren().remove(welcomeView);
if (!min) buttonsContainer.getChildren().remove(btnMin); if (!min) buttonsContainer.getChildren().remove(btnMin);

View File

@@ -39,7 +39,6 @@ import javafx.scene.paint.Color;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.setting.*; import org.jackhuang.hmcl.setting.*;
import org.jackhuang.hmcl.ui.construct.FileItem;
import org.jackhuang.hmcl.ui.construct.FontComboBox; import org.jackhuang.hmcl.ui.construct.FontComboBox;
import org.jackhuang.hmcl.ui.construct.MultiFileItem; import org.jackhuang.hmcl.ui.construct.MultiFileItem;
import org.jackhuang.hmcl.ui.construct.Validator; import org.jackhuang.hmcl.ui.construct.Validator;
@@ -135,11 +134,11 @@ public final class SettingsPage extends StackPane implements DecoratorPage {
ObservableList<Label> list = FXCollections.observableArrayList(); ObservableList<Label> list = FXCollections.observableArrayList();
for (Locales.SupportedLocale locale : Locales.LOCALES) for (Locales.SupportedLocale locale : Locales.LOCALES)
list.add(new Label(locale.getName(Settings.instance().getLocale().getResourceBundle()))); list.add(new Label(locale.getName(config().getLocalization().getResourceBundle())));
cboLanguage.setItems(list); cboLanguage.setItems(list);
cboLanguage.getSelectionModel().select(Locales.LOCALES.indexOf(Settings.instance().getLocale())); cboLanguage.getSelectionModel().select(Locales.LOCALES.indexOf(config().getLocalization()));
cboLanguage.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> Settings.instance().setLocale(Locales.getLocale(newValue.intValue()))); cboLanguage.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> config().setLocalization(Locales.getLocale(newValue.intValue())));
// ==== Proxy ==== // ==== Proxy ====
txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty()); txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty());

View File

@@ -24,7 +24,6 @@ import java.util.ResourceBundle;
import java.util.logging.Level; import java.util.logging.Level;
import org.jackhuang.hmcl.setting.ConfigHolder; import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.setting.Settings;
public final class I18n { public final class I18n {
@@ -32,7 +31,7 @@ public final class I18n {
public static ResourceBundle getResourceBundle() { public static ResourceBundle getResourceBundle() {
if (ConfigHolder.isInitialized()) { if (ConfigHolder.isInitialized()) {
return Settings.instance().getLocale().getResourceBundle(); return ConfigHolder.config().getLocalization().getResourceBundle();
} else { } else {
return Locales.DEFAULT.getResourceBundle(); return Locales.DEFAULT.getResourceBundle();
} }

View File

@@ -19,6 +19,10 @@ package org.jackhuang.hmcl.util.i18n;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@@ -107,5 +111,17 @@ public final class Locales {
if (name == null) return resourceBundle.getString("lang"); if (name == null) return resourceBundle.getString("lang");
else return newResourceBundle.getString(name); else return newResourceBundle.getString(name);
} }
public static class TypeAdapter extends com.google.gson.TypeAdapter<SupportedLocale> {
@Override
public void write(JsonWriter out, SupportedLocale value) throws IOException {
out.value(getNameByLocale(value));
}
@Override
public SupportedLocale read(JsonReader in) throws IOException {
return getLocaleByName(in.nextString());
}
}
} }
} }