Remember language between instances.

This commit is contained in:
Chris Cromer 2016-07-22 18:47:16 -04:00
parent eca164a190
commit f284088a7f
2 changed files with 19 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.prefs.Preferences;
/** /**
* Estructuras de Datos * Estructuras de Datos
@ -37,7 +38,11 @@ public class Main extends Application {
*/ */
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
Locale locale = new Locale("es", "ES"); Preferences preferences = Preferences.userRoot().node(this.getClass().getName());
String idoma = preferences.get("idioma", "en");
String idoma2 = preferences.get("idioma2", "EN");
Locale locale = new Locale(idoma, idoma2);
ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale); ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale);
try { try {
@ -53,10 +58,11 @@ public class Main extends Application {
stage.close(); stage.close();
} }
stage.setMaximized(true); //stage.setMaximized(true);
stage.setMinHeight(640); stage.setMinHeight(640);
stage.setMinWidth(768); stage.setMinWidth(768);
stage.getIcons().add(new Image(getClass().getResourceAsStream("/cl/cromer/estructuras/images/icon.png"))); stage.getIcons().add(new Image(getClass().getResourceAsStream("/cl/cromer/estructuras/images/icon.png")));
stage.setUserData(preferences);
stage.show(); stage.show();
} }

View File

@ -18,6 +18,7 @@ import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.prefs.Preferences;
/** /**
* Controlar las acciones cuando una opción es elegido en el menu. * Controlar las acciones cuando una opción es elegido en el menu.
@ -284,6 +285,9 @@ public class MenuController extends VBox implements Initializable {
*/ */
@FXML @FXML
protected void menuIngles() { protected void menuIngles() {
Stage stage = (Stage) menuBar.getScene().getWindow();
Preferences preferences = (Preferences) stage.getUserData();
ButtonType botonCambiar = new ButtonType(resourceBundle.getString("cambiar"), ButtonBar.ButtonData.OK_DONE); ButtonType botonCambiar = new ButtonType(resourceBundle.getString("cambiar"), ButtonBar.ButtonData.OK_DONE);
ButtonType botonCancelar = new ButtonType(resourceBundle.getString("cancelar"), ButtonBar.ButtonData.CANCEL_CLOSE); ButtonType botonCancelar = new ButtonType(resourceBundle.getString("cancelar"), ButtonBar.ButtonData.CANCEL_CLOSE);
Dialog<ButtonType> dialog = new Dialog<>(); Dialog<ButtonType> dialog = new Dialog<>();
@ -299,6 +303,8 @@ public class MenuController extends VBox implements Initializable {
Optional<ButtonType> result = dialog.showAndWait(); Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == botonCambiar) { if (result.isPresent() && result.get() == botonCambiar) {
// Si hace click en cambiar, cambiar el idioma y reiniciar. // Si hace click en cambiar, cambiar el idioma y reiniciar.
preferences.put("idioma", "en");
preferences.put("idioma2", "EN");
Locale locale = new Locale("en", "EN"); Locale locale = new Locale("en", "EN");
ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale); ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale);
@ -311,6 +317,9 @@ public class MenuController extends VBox implements Initializable {
*/ */
@FXML @FXML
protected void menuEspanol() { protected void menuEspanol() {
Stage stage = (Stage) menuBar.getScene().getWindow();
Preferences preferences = (Preferences) stage.getUserData();
ButtonType botonCambiar = new ButtonType(resourceBundle.getString("cambiar"), ButtonBar.ButtonData.OK_DONE); ButtonType botonCambiar = new ButtonType(resourceBundle.getString("cambiar"), ButtonBar.ButtonData.OK_DONE);
ButtonType botonCancelar = new ButtonType(resourceBundle.getString("cancelar"), ButtonBar.ButtonData.CANCEL_CLOSE); ButtonType botonCancelar = new ButtonType(resourceBundle.getString("cancelar"), ButtonBar.ButtonData.CANCEL_CLOSE);
Dialog<ButtonType> dialog = new Dialog<>(); Dialog<ButtonType> dialog = new Dialog<>();
@ -326,6 +335,8 @@ public class MenuController extends VBox implements Initializable {
Optional<ButtonType> result = dialog.showAndWait(); Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == botonCambiar) { if (result.isPresent() && result.get() == botonCambiar) {
// Si hace click en cambiar, cambiar el idioma y reiniciar. // Si hace click en cambiar, cambiar el idioma y reiniciar.
preferences.put("idioma", "es");
preferences.put("idioma2", "ES");
Locale locale = new Locale("es", "ES"); Locale locale = new Locale("es", "ES");
ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale); ResourceBundle resourceBundle = ResourceBundle.getBundle("cl.cromer.estructuras.bundles.Idioma", locale);