📜  JavaFX Translation(1)

📅  最后修改于: 2023-12-03 15:31:35.770000             🧑  作者: Mango

JavaFX Translation

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.

Overview

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.

Usage

To use JavaFX Translation, you need to follow these steps:

  1. Add the JavaFX Translation dependency to your project:
<dependency>
    <groupId>com.github.sandor-dosa</groupId>
    <artifactId>javafx-translation</artifactId>
    <version>1.0.0</version>
</dependency>
  1. Use the @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;
    
    // ...
}
  1. Create a resource bundle for each language your application supports. The resource bundle should be named ${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
  1. Load the resource bundle for the user's selected locale and set it as the root of your application's scene:
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();
}
  1. Create a language switcher that allows the user to change the application's language at runtime:
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();
    }
}
  1. Bind the 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()
            )
        );
    }
}
  1. Add a button to your application that calls the 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());
    }
}
Conclusion

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.