📅  最后修改于: 2023-12-03 15:31:35.770000             🧑  作者: Mango
JavaFX Translation is a library that provides a simple and easy way to internationalize JavaFX applications. Internationalization is the process of adapting an application to different languages, regions, and cultures.
JavaFX Translation provides a set of utility classes and annotations that enable the developer to externalize the application's strings and UI components into resource bundles. Resource bundles contain key-value pairs that map the application's strings to their translations in different languages.
The library also provides a way to switch between different languages at runtime. This means that users can change the language of the application without having to restart it.
To use JavaFX Translation, you need to follow these steps:
<dependency>
<groupId>com.github.sandor-dosa</groupId>
<artifactId>javafx-translation</artifactId>
<version>1.0.0</version>
</dependency>
@TranslationKey
annotation to mark the strings that need to be externalized:public class LoginView {
@FXML
@TranslationKey("login.username")
private TextField usernameField;
@FXML
@TranslationKey("login.password")
private PasswordField passwordField;
@FXML
@TranslationKey("login.button")
private Button loginButton;
// ...
}
${baseName}_${language}.${extension}
, where ${baseName}
is the base name of your Java resource bundle, ${language}
is the language code (e.g. "en"
, "fr"
, "de"
), and ${extension}
is the file extension (e.g. "properties"
, "xml"
):src/main/resources/messages.properties
src/main/resources/messages_fr.properties
src/main/resources/messages_de.properties
public static void main(String[] args) {
Locale.setDefault(Locale.FRANCE); // Set the default locale to French
ResourceBundle bundle = ResourceBundle.getBundle("messages");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/LoginView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
Translator translator = new Translator(bundle);
translator.translate(scene.getRoot()); // Translate the UI components
stage.setScene(scene);
stage.show();
}
public class LanguageSwitcher {
private List<Locale> locales;
private int currentLocaleIndex;
public LanguageSwitcher(Locale... locales) {
this.locales = Arrays.asList(locales);
this.currentLocaleIndex = this.locales.indexOf(Locale.getDefault());
}
public Locale getCurrentLocale() {
return locales.get(currentLocaleIndex);
}
public void setCurrentLocale(Locale locale) {
currentLocaleIndex = locales.indexOf(locale);
}
public void nextLocale() {
currentLocaleIndex = (currentLocaleIndex + 1) % locales.size();
}
}
Translator
's bundle property to the LanguageSwitcher
's currentLocale
property:public class MyApp {
private Translator translator;
private LanguageSwitcher languageSwitcher;
public MyApp(ResourceBundle bundle, LanguageSwitcher languageSwitcher) {
this.translator = new Translator(bundle);
this.languageSwitcher = languageSwitcher;
translator.bundleProperty().bind(
Bindings.createObjectBinding(() -> ResourceBundle.getBundle("messages", languageSwitcher.getCurrentLocale()),
languageSwitcher.currentLocaleProperty()
)
);
}
}
nextLocale
method of the LanguageSwitcher
when clicked. This will change the language of the application:public class LanguageSwitcherButton extends Button {
private LanguageSwitcher languageSwitcher;
public LanguageSwitcherButton(LanguageSwitcher languageSwitcher) {
this.languageSwitcher = languageSwitcher;
setOnAction(event -> languageSwitcher.nextLocale());
}
}
JavaFX Translation simplifies the internationalization of JavaFX applications by externalizing strings and UI components into resource bundles. It also provides a way to switch between languages at runtime. This makes it easy to adapt JavaFX applications to different languages, regions, and cultures.