Javafx/Scalafx i18n simple switch (using fxml)

时间:2017-06-20 12:40:05

标签: java scala javafx internationalization scalafx

I'm developing scalafx application (I mentioned javafx because what I'm facing is, basically, javafx problem - scalafx is just shell around it) and I want to switch i18n during the program execution at will. Here is my (working) solution for no-fxml case (I'll try explain any scala-specific code):

val curLocale = new ObjectProperty(new SimpleObjectProperty[Locale](Locale.ENGLISH))
val curBundle: ObjectBinding[ResourceBundle] = Bindings.createObjectBinding[ResourceBundle](() => { ResourceBundle.getBundle("i18n.text", curLocale.get, control) }, curLocale)
//Creating and object property of locale (changing stored locale will change language 
//in all related places of application) and object binding for resource bundle
def i18nLoad(s: String): StringBinding = {
    Bindings.createStringBinding(() => { curBundle.get.getString(s) }, curBundle)
}
//method that provides binding that is used in definition of text for each element
//Then, to set text for, let's say, label:
val smth = new Label()
smth.text <== i18nLoad("window.smth")
//<== is scalafx operation for one-directional binding, 
//"window. smth" is record name in properties file

But I don't have similar solution for fxml. I looked into standard approach for i18n and it includes passing resource bundle into FXML loader class. Is there a way (by using some external libraries, extending FXMLLoader manually or re-writing it completely) to pass the binding that holds resource bundle into the constructor, and make it to bind for appropriate value (like in a given example) instead of just reading it from properties? I tried to look through FXMLLoader source code, but I did not found the point a wich such change could be possible.

0 个答案:

没有答案