Java FX resource bundle and internalization - implementation for RadioMenuItems in MenuBar












0














I am working currently on internalization functionality in my application.



I created bundle folder with 2 files, 2 languages available:



messages_pl.properties and messages_en.properties.


In my main fxml file (containing menuBar as well) I set internalization resource for each and every controls.



This piece of code works perfectly for me, I can switch from "en" to "pl"



public void start(Stage primaryStage) throws Exception {        
Locale.setDefault(new Locale("en"));
Pane borderPane = FxmlUtils.fxmlLoader(BORDER_PANE_MAIN_FXML);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.setTitle(FxmlUtils.getResourceBundle().getString("tittle.application"));
primaryStage.show();


The problem is that I don't want manipulate the code, switching from en to pl each time I need to change language.



I would like to have it dynamically, based if I choose RadioMenuItem "Polish" or RadioMenutItem "English".



Do you know how to adapt my source code consequently?










share|improve this question
























  • stackoverflow.com/questions/32464974/… Does this help you?
    – Sunflame
    Nov 12 at 10:23










  • I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
    – Tomek Młynarski
    Nov 12 at 10:31










  • I think that code does not use .properties files
    – Tomek Młynarski
    Nov 12 at 10:36
















0














I am working currently on internalization functionality in my application.



I created bundle folder with 2 files, 2 languages available:



messages_pl.properties and messages_en.properties.


In my main fxml file (containing menuBar as well) I set internalization resource for each and every controls.



This piece of code works perfectly for me, I can switch from "en" to "pl"



public void start(Stage primaryStage) throws Exception {        
Locale.setDefault(new Locale("en"));
Pane borderPane = FxmlUtils.fxmlLoader(BORDER_PANE_MAIN_FXML);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.setTitle(FxmlUtils.getResourceBundle().getString("tittle.application"));
primaryStage.show();


The problem is that I don't want manipulate the code, switching from en to pl each time I need to change language.



I would like to have it dynamically, based if I choose RadioMenuItem "Polish" or RadioMenutItem "English".



Do you know how to adapt my source code consequently?










share|improve this question
























  • stackoverflow.com/questions/32464974/… Does this help you?
    – Sunflame
    Nov 12 at 10:23










  • I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
    – Tomek Młynarski
    Nov 12 at 10:31










  • I think that code does not use .properties files
    – Tomek Młynarski
    Nov 12 at 10:36














0












0








0







I am working currently on internalization functionality in my application.



I created bundle folder with 2 files, 2 languages available:



messages_pl.properties and messages_en.properties.


In my main fxml file (containing menuBar as well) I set internalization resource for each and every controls.



This piece of code works perfectly for me, I can switch from "en" to "pl"



public void start(Stage primaryStage) throws Exception {        
Locale.setDefault(new Locale("en"));
Pane borderPane = FxmlUtils.fxmlLoader(BORDER_PANE_MAIN_FXML);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.setTitle(FxmlUtils.getResourceBundle().getString("tittle.application"));
primaryStage.show();


The problem is that I don't want manipulate the code, switching from en to pl each time I need to change language.



I would like to have it dynamically, based if I choose RadioMenuItem "Polish" or RadioMenutItem "English".



Do you know how to adapt my source code consequently?










share|improve this question















I am working currently on internalization functionality in my application.



I created bundle folder with 2 files, 2 languages available:



messages_pl.properties and messages_en.properties.


In my main fxml file (containing menuBar as well) I set internalization resource for each and every controls.



This piece of code works perfectly for me, I can switch from "en" to "pl"



public void start(Stage primaryStage) throws Exception {        
Locale.setDefault(new Locale("en"));
Pane borderPane = FxmlUtils.fxmlLoader(BORDER_PANE_MAIN_FXML);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.setTitle(FxmlUtils.getResourceBundle().getString("tittle.application"));
primaryStage.show();


The problem is that I don't want manipulate the code, switching from en to pl each time I need to change language.



I would like to have it dynamically, based if I choose RadioMenuItem "Polish" or RadioMenutItem "English".



Do you know how to adapt my source code consequently?







java javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:06









Bsquare

2,48131029




2,48131029










asked Nov 12 at 9:52









Tomek Młynarski

46




46












  • stackoverflow.com/questions/32464974/… Does this help you?
    – Sunflame
    Nov 12 at 10:23










  • I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
    – Tomek Młynarski
    Nov 12 at 10:31










  • I think that code does not use .properties files
    – Tomek Młynarski
    Nov 12 at 10:36


















  • stackoverflow.com/questions/32464974/… Does this help you?
    – Sunflame
    Nov 12 at 10:23










  • I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
    – Tomek Młynarski
    Nov 12 at 10:31










  • I think that code does not use .properties files
    – Tomek Młynarski
    Nov 12 at 10:36
















stackoverflow.com/questions/32464974/… Does this help you?
– Sunflame
Nov 12 at 10:23




stackoverflow.com/questions/32464974/… Does this help you?
– Sunflame
Nov 12 at 10:23












I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
– Tomek Młynarski
Nov 12 at 10:31




I am not quite following that code, and in my application I would like to switch language from top menu bar and RadioMenuItem
– Tomek Młynarski
Nov 12 at 10:31












I think that code does not use .properties files
– Tomek Młynarski
Nov 12 at 10:36




I think that code does not use .properties files
– Tomek Młynarski
Nov 12 at 10:36












1 Answer
1






active

oldest

votes


















0














I found a solution, it is not the prettiest but it works:



Controller:



public class Controller implements Initializable {

@FXML
private MenuItem en;
@FXML
private MenuItem de;

Runnable changeLanguage; // add setter

@Override
public void initialize(URL location, ResourceBundle resources) {
en.setOnAction(event -> {
Locale.setDefault(Locale.ENGLISH);
reload();
});
de.setOnAction(event -> {
Locale.setDefault(Locale.GERMAN);
reload();
});
}
private void reload() {
changeLanguage.run();
}
}


Main:



public class Main extends Application {

private Stage primary;

@Override
public void start(Stage primaryStage) throws Exception {
this.primary = primaryStage;
load();
primaryStage.show();
}

private void load() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
loader.setResources(ResourceBundle.getBundle("stackoverflow/language/language", Locale.getDefault())); // the main package is stackoverflow which contains language
primary.setScene(new Scene(loader.load(), 400, 600));
Controller controller = loader.getController();
controller.changeLanguage = () -> {
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
};
}

public static void main(String args) {
Locale.setDefault(Locale.ENGLISH);
launch(args);
}

}


and View.fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.language.Controller">
<MenuBar>
<Menu text="%language">
<MenuItem fx:id="en" text="%en"/>
<MenuItem fx:id="de" text="%de"/>
</Menu>
</MenuBar>
</AnchorPane>


This is how the classes are:



enter image description here






share|improve this answer

















  • 1




    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 12 at 23:15










  • @SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
    – Sunflame
    Nov 13 at 7:38










  • It only appears if all users have the chat privilege, otherwise mods have to manually create one.
    – Samuel Liew
    Nov 13 at 8:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259583%2fjava-fx-resource-bundle-and-internalization-implementation-for-radiomenuitems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I found a solution, it is not the prettiest but it works:



Controller:



public class Controller implements Initializable {

@FXML
private MenuItem en;
@FXML
private MenuItem de;

Runnable changeLanguage; // add setter

@Override
public void initialize(URL location, ResourceBundle resources) {
en.setOnAction(event -> {
Locale.setDefault(Locale.ENGLISH);
reload();
});
de.setOnAction(event -> {
Locale.setDefault(Locale.GERMAN);
reload();
});
}
private void reload() {
changeLanguage.run();
}
}


Main:



public class Main extends Application {

private Stage primary;

@Override
public void start(Stage primaryStage) throws Exception {
this.primary = primaryStage;
load();
primaryStage.show();
}

private void load() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
loader.setResources(ResourceBundle.getBundle("stackoverflow/language/language", Locale.getDefault())); // the main package is stackoverflow which contains language
primary.setScene(new Scene(loader.load(), 400, 600));
Controller controller = loader.getController();
controller.changeLanguage = () -> {
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
};
}

public static void main(String args) {
Locale.setDefault(Locale.ENGLISH);
launch(args);
}

}


and View.fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.language.Controller">
<MenuBar>
<Menu text="%language">
<MenuItem fx:id="en" text="%en"/>
<MenuItem fx:id="de" text="%de"/>
</Menu>
</MenuBar>
</AnchorPane>


This is how the classes are:



enter image description here






share|improve this answer

















  • 1




    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 12 at 23:15










  • @SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
    – Sunflame
    Nov 13 at 7:38










  • It only appears if all users have the chat privilege, otherwise mods have to manually create one.
    – Samuel Liew
    Nov 13 at 8:52
















0














I found a solution, it is not the prettiest but it works:



Controller:



public class Controller implements Initializable {

@FXML
private MenuItem en;
@FXML
private MenuItem de;

Runnable changeLanguage; // add setter

@Override
public void initialize(URL location, ResourceBundle resources) {
en.setOnAction(event -> {
Locale.setDefault(Locale.ENGLISH);
reload();
});
de.setOnAction(event -> {
Locale.setDefault(Locale.GERMAN);
reload();
});
}
private void reload() {
changeLanguage.run();
}
}


Main:



public class Main extends Application {

private Stage primary;

@Override
public void start(Stage primaryStage) throws Exception {
this.primary = primaryStage;
load();
primaryStage.show();
}

private void load() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
loader.setResources(ResourceBundle.getBundle("stackoverflow/language/language", Locale.getDefault())); // the main package is stackoverflow which contains language
primary.setScene(new Scene(loader.load(), 400, 600));
Controller controller = loader.getController();
controller.changeLanguage = () -> {
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
};
}

public static void main(String args) {
Locale.setDefault(Locale.ENGLISH);
launch(args);
}

}


and View.fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.language.Controller">
<MenuBar>
<Menu text="%language">
<MenuItem fx:id="en" text="%en"/>
<MenuItem fx:id="de" text="%de"/>
</Menu>
</MenuBar>
</AnchorPane>


This is how the classes are:



enter image description here






share|improve this answer

















  • 1




    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 12 at 23:15










  • @SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
    – Sunflame
    Nov 13 at 7:38










  • It only appears if all users have the chat privilege, otherwise mods have to manually create one.
    – Samuel Liew
    Nov 13 at 8:52














0












0








0






I found a solution, it is not the prettiest but it works:



Controller:



public class Controller implements Initializable {

@FXML
private MenuItem en;
@FXML
private MenuItem de;

Runnable changeLanguage; // add setter

@Override
public void initialize(URL location, ResourceBundle resources) {
en.setOnAction(event -> {
Locale.setDefault(Locale.ENGLISH);
reload();
});
de.setOnAction(event -> {
Locale.setDefault(Locale.GERMAN);
reload();
});
}
private void reload() {
changeLanguage.run();
}
}


Main:



public class Main extends Application {

private Stage primary;

@Override
public void start(Stage primaryStage) throws Exception {
this.primary = primaryStage;
load();
primaryStage.show();
}

private void load() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
loader.setResources(ResourceBundle.getBundle("stackoverflow/language/language", Locale.getDefault())); // the main package is stackoverflow which contains language
primary.setScene(new Scene(loader.load(), 400, 600));
Controller controller = loader.getController();
controller.changeLanguage = () -> {
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
};
}

public static void main(String args) {
Locale.setDefault(Locale.ENGLISH);
launch(args);
}

}


and View.fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.language.Controller">
<MenuBar>
<Menu text="%language">
<MenuItem fx:id="en" text="%en"/>
<MenuItem fx:id="de" text="%de"/>
</Menu>
</MenuBar>
</AnchorPane>


This is how the classes are:



enter image description here






share|improve this answer












I found a solution, it is not the prettiest but it works:



Controller:



public class Controller implements Initializable {

@FXML
private MenuItem en;
@FXML
private MenuItem de;

Runnable changeLanguage; // add setter

@Override
public void initialize(URL location, ResourceBundle resources) {
en.setOnAction(event -> {
Locale.setDefault(Locale.ENGLISH);
reload();
});
de.setOnAction(event -> {
Locale.setDefault(Locale.GERMAN);
reload();
});
}
private void reload() {
changeLanguage.run();
}
}


Main:



public class Main extends Application {

private Stage primary;

@Override
public void start(Stage primaryStage) throws Exception {
this.primary = primaryStage;
load();
primaryStage.show();
}

private void load() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
loader.setResources(ResourceBundle.getBundle("stackoverflow/language/language", Locale.getDefault())); // the main package is stackoverflow which contains language
primary.setScene(new Scene(loader.load(), 400, 600));
Controller controller = loader.getController();
controller.changeLanguage = () -> {
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
};
}

public static void main(String args) {
Locale.setDefault(Locale.ENGLISH);
launch(args);
}

}


and View.fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.language.Controller">
<MenuBar>
<Menu text="%language">
<MenuItem fx:id="en" text="%en"/>
<MenuItem fx:id="de" text="%de"/>
</Menu>
</MenuBar>
</AnchorPane>


This is how the classes are:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 10:56









Sunflame

1,4672621




1,4672621








  • 1




    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 12 at 23:15










  • @SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
    – Sunflame
    Nov 13 at 7:38










  • It only appears if all users have the chat privilege, otherwise mods have to manually create one.
    – Samuel Liew
    Nov 13 at 8:52














  • 1




    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 12 at 23:15










  • @SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
    – Sunflame
    Nov 13 at 7:38










  • It only appears if all users have the chat privilege, otherwise mods have to manually create one.
    – Samuel Liew
    Nov 13 at 8:52








1




1




Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Nov 12 at 23:15




Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Nov 12 at 23:15












@SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
– Sunflame
Nov 13 at 7:38




@SamuelLiew Yeah I know, but normally after 4-5 comments it appeared the move to chat, but now it didn't and I didn't know how to switch to there. Thanks.
– Sunflame
Nov 13 at 7:38












It only appears if all users have the chat privilege, otherwise mods have to manually create one.
– Samuel Liew
Nov 13 at 8:52




It only appears if all users have the chat privilege, otherwise mods have to manually create one.
– Samuel Liew
Nov 13 at 8:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259583%2fjava-fx-resource-bundle-and-internalization-implementation-for-radiomenuitems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values