From f332083e275f880573fed91a2c22de4828d854f4 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Mon, 27 Jul 2020 19:58:23 +0300 Subject: [PATCH] Rewrite i18n implementation --- pom.xml | 2 +- src/main/java/nsusbloader/AppPreferences.java | 16 +++-- .../Controllers/SettingsController.java | 51 +++++++++---- src/main/java/nsusbloader/NSLMain.java | 4 +- .../nsusbloader/UI/LocaleUiStringHolder.java | 59 +++++++++++++++ src/main/resources/SettingsTab.fxml | 2 +- ...deu.properties => locale_de_DE.properties} | 0 src/main/resources/locale_en_US.properties | 71 +++++++++++++++++++ ...spa.properties => locale_es_ES.properties} | 0 ...fra.properties => locale_fr_FR.properties} | 0 ...ita.properties => locale_it_IT.properties} | 0 ...kor.properties => locale_ko_KR.properties} | 0 ...por.properties => locale_pt_BR.properties} | 0 ...rus.properties => locale_ru_RU.properties} | 0 ...ukr.properties => locale_uk_UA.properties} | 0 ...vie.properties => locale_vi_VN.properties} | 0 ...zho.properties => locale_zh_CN.properties} | 0 17 files changed, 182 insertions(+), 23 deletions(-) create mode 100644 src/main/java/nsusbloader/UI/LocaleUiStringHolder.java rename src/main/resources/{locale_deu.properties => locale_de_DE.properties} (100%) create mode 100644 src/main/resources/locale_en_US.properties rename src/main/resources/{locale_spa.properties => locale_es_ES.properties} (100%) rename src/main/resources/{locale_fra.properties => locale_fr_FR.properties} (100%) rename src/main/resources/{locale_ita.properties => locale_it_IT.properties} (100%) rename src/main/resources/{locale_kor.properties => locale_ko_KR.properties} (100%) rename src/main/resources/{locale_por.properties => locale_pt_BR.properties} (100%) rename src/main/resources/{locale_rus.properties => locale_ru_RU.properties} (100%) rename src/main/resources/{locale_ukr.properties => locale_uk_UA.properties} (100%) rename src/main/resources/{locale_vie.properties => locale_vi_VN.properties} (100%) rename src/main/resources/{locale_zho.properties => locale_zh_CN.properties} (100%) diff --git a/pom.xml b/pom.xml index 48cdd9f..e5c313f 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ NS-USBloader ns-usbloader - 4.1-SNAPSHOT + 4.2-SNAPSHOT https://github.com/developersu/ns-usbloader/ diff --git a/src/main/java/nsusbloader/AppPreferences.java b/src/main/java/nsusbloader/AppPreferences.java index 311e48d..a08682a 100644 --- a/src/main/java/nsusbloader/AppPreferences.java +++ b/src/main/java/nsusbloader/AppPreferences.java @@ -27,9 +27,14 @@ public class AppPreferences { private static final AppPreferences INSTANCE = new AppPreferences(); public static AppPreferences getInstance() { return INSTANCE; } - private Preferences preferences; + private final Preferences preferences; + private final Locale locale; - private AppPreferences(){ preferences = Preferences.userRoot().node("NS-USBloader"); } + private AppPreferences(){ + this.preferences = Preferences.userRoot().node("NS-USBloader"); + String localeCode = preferences.get("locale", Locale.getDefault().toString()); + this.locale = new Locale(localeCode.substring(0, 2), localeCode.substring(3, 5)); + } public String getTheme(){ String theme = preferences.get("THEME", "/res/app_dark.css"); // Don't let user to change settings manually @@ -59,6 +64,10 @@ public class AppPreferences { public String getRecent(){ return preferences.get("RECENT", System.getProperty("user.home")); } public void setRecent(String path){ preferences.put("RECENT", path); } //------------ SETTINGS ------------------// + + public Locale getLocale(){ return this.locale; } + public void setLocale(String langStr){ preferences.put("locale", langStr); } + public boolean getNsIpValidationNeeded() {return preferences.getBoolean("NSIPVALIDATION", true);} public void setNsIpValidationNeeded(boolean need){preferences.putBoolean("NSIPVALIDATION", need);} @@ -96,9 +105,6 @@ public class AppPreferences { public boolean getTfXCI(){return preferences.getBoolean("TF_XCI", true);} public void setTfXCI(boolean prop){ preferences.putBoolean("TF_XCI", prop); } - public String getLanguage(){return preferences.get("USR_LANG", Locale.getDefault().getISO3Language());} - public void setLanguage(String langStr){preferences.put("USR_LANG", langStr);} - public boolean getNspFileFilterGL(){return preferences.getBoolean("GL_NSP_FILTER", false); } public void setNspFileFilterGL(boolean prop){preferences.putBoolean("GL_NSP_FILTER", prop);} diff --git a/src/main/java/nsusbloader/Controllers/SettingsController.java b/src/main/java/nsusbloader/Controllers/SettingsController.java index baa542d..e67083c 100644 --- a/src/main/java/nsusbloader/Controllers/SettingsController.java +++ b/src/main/java/nsusbloader/Controllers/SettingsController.java @@ -30,6 +30,7 @@ import javafx.scene.layout.VBox; import nsusbloader.AppPreferences; import nsusbloader.ServiceWindow; import nsusbloader.ModelControllers.UpdatesChecker; +import nsusbloader.UI.LocaleUiStringHolder; import nsusbloader.Utilities.WindowsDrivers.DriversInstall; import java.io.File; @@ -73,7 +74,7 @@ public class SettingsController implements Initializable { checkForUpdBtn, drvInstBtn; @FXML - private ChoiceBox langCB; + private ChoiceBox langCB; @FXML private ChoiceBox glVersionChoiceBox; @@ -220,8 +221,8 @@ public class SettingsController implements Initializable { tfXciSpprtCb.setSelected(AppPreferences.getInstance().getTfXCI()); // Language settings area - ObservableList langCBObsList = FXCollections.observableArrayList(); - langCBObsList.add("eng"); + ObservableList langCBObsList = FXCollections.observableArrayList(); + //langCBObsList.add(new LocaleUiStringHolder(new Locale("en", "US"))); File jarFile; try{ @@ -239,8 +240,14 @@ public class SettingsController implements Initializable { Enumeration entries = jar.entries(); //gives ALL entries in jar while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); - if (name.startsWith("locale_")) - langCBObsList.add(name.substring(7, 10)); + if (name.startsWith("locale_")){ + try{ + langCBObsList.add(new LocaleUiStringHolder(name)); + } + catch (Exception e){ + e.printStackTrace(); + } + } } jar.close(); } @@ -253,21 +260,37 @@ public class SettingsController implements Initializable { String[] filesList = new File(resourceURL.getFile()).list(); // Screw it. This WON'T produce NullPointerException for (String jarFileName : filesList) - if (jarFileName.startsWith("locale_")) - langCBObsList.add(jarFileName.substring(7, 10)); + if (jarFileName.startsWith("locale_")){ + try{ + langCBObsList.add(new LocaleUiStringHolder(jarFileName)); + } + catch (Exception e){ + e.printStackTrace(); + } + } } + langCBObsList.sort(Comparator.comparing(LocaleUiStringHolder::toString)); langCB.setItems(langCBObsList); - if (langCBObsList.contains(AppPreferences.getInstance().getLanguage())) - langCB.getSelectionModel().select(AppPreferences.getInstance().getLanguage()); - else - langCB.getSelectionModel().select("eng"); + // TODO: REFACTOR THIS SHIT; INCAPSULATE AND MOVE OUT FROM HERE + Locale localeFromPrefs = AppPreferences.getInstance().getLocale(); + boolean notExists = true; + for (LocaleUiStringHolder holderItem: langCBObsList){ + if (holderItem.getLocale().equals(localeFromPrefs)){ + langCB.getSelectionModel().select(holderItem); + notExists = false; + break; + } + } + if (notExists) + langCB.getSelectionModel().select(0); langBtn.setOnAction(e->{ - AppPreferences.getInstance().setLanguage(langCB.getSelectionModel().getSelectedItem()); + LocaleUiStringHolder localeHolder = langCB.getSelectionModel().getSelectedItem(); + AppPreferences.getInstance().setLocale(localeHolder.getLocaleCode()); + Locale newLocale = localeHolder.getLocale(); ServiceWindow.getInfoNotification("", - ResourceBundle.getBundle("locale", new Locale(langCB.getSelectionModel().getSelectedItem())) - .getString("windowBodyRestartToApplyLang")); + ResourceBundle.getBundle("locale", newLocale).getString("windowBodyRestartToApplyLang")); }); // Set supported old versions glVersionChoiceBox.getItems().addAll(glSupportedVersions); diff --git a/src/main/java/nsusbloader/NSLMain.java b/src/main/java/nsusbloader/NSLMain.java index f18f653..f5b0f93 100644 --- a/src/main/java/nsusbloader/NSLMain.java +++ b/src/main/java/nsusbloader/NSLMain.java @@ -32,14 +32,14 @@ import java.util.ResourceBundle; public class NSLMain extends Application { - public static final String appVersion = "v4.1"; + public static final String appVersion = "v4.2"; public static boolean isCli; @Override public void start(Stage primaryStage) throws Exception{ FXMLLoader loader = new FXMLLoader(getClass().getResource("/NSLMain.fxml")); - Locale userLocale = new Locale(AppPreferences.getInstance().getLanguage()); // NOTE: user locale based on ISO3 Language codes + Locale userLocale = AppPreferences.getInstance().getLocale(); ResourceBundle rb = ResourceBundle.getBundle("locale", userLocale); loader.setResources(rb); diff --git a/src/main/java/nsusbloader/UI/LocaleUiStringHolder.java b/src/main/java/nsusbloader/UI/LocaleUiStringHolder.java new file mode 100644 index 0000000..b5637c7 --- /dev/null +++ b/src/main/java/nsusbloader/UI/LocaleUiStringHolder.java @@ -0,0 +1,59 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NS-USBloader is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NS-USBloader. If not, see . +*/ +package nsusbloader.UI; + +import nsusbloader.cli.IncorrectSetupException; + +import java.util.Locale; + +public class LocaleUiStringHolder { + + private final Locale locale; + private final String localeCode; + private final String languageName; + + public LocaleUiStringHolder(Locale locale){ + this.locale = locale; + this.localeCode = locale.toString(); + this.languageName = locale.getDisplayLanguage(locale) + " (" + locale + ")"; + } + + public LocaleUiStringHolder(String localeFileName) throws Exception{ + if (localeFileName.length() < 12) + throw new IncorrectSetupException("Locale filename is incorrect: "+localeFileName); + String country = localeFileName.substring(7, 9); + String language = localeFileName.substring(10, 12); + this.locale = new Locale(country, language); + this.localeCode = locale.toString(); + this.languageName = locale.getDisplayLanguage(locale) + " (" + locale + ")"; + } + + @Override + public String toString(){ + return languageName; + } + + public String getLocaleCode(){ + return localeCode; + }; + + public Locale getLocale() { + return locale; + } +} diff --git a/src/main/resources/SettingsTab.fxml b/src/main/resources/SettingsTab.fxml index 6676a5f..d1b7e80 100644 --- a/src/main/resources/SettingsTab.fxml +++ b/src/main/resources/SettingsTab.fxml @@ -20,7 +20,7 @@