java.lang.Object
io.github.snoopy137.languagemanager.utils.Language

public class Language extends Object
Utility class to manage language settings and automatic UI binding for JavaFX applications. This class allows setting the locale, dynamically switching languages, and binding UI elements to corresponding language keys in the resource bundle.
Author:
alan
  • Property Summary

    Properties
    Type
    Property
    Description
    static javafx.beans.property.ObjectProperty<ResourceBundle>
    Gets the property object for the current resource bundle.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    autoBind(Object controller)
    Automatically binds the text or promptText properties of UI controls (such as Label and TextInputControl) in the provided controller object to corresponding values in the current resource bundle.
    static void
    autoBindField(Object control, String key)
    Automatically binds a single UI control to the corresponding value in the current resource bundle.
    static javafx.beans.binding.StringBinding
    bind(String key, String optional)
    Binds a given field (via its ID) to the appropriate value in the current resource bundle.
    static javafx.beans.property.ObjectProperty<ResourceBundle>
    Gets the property object for the current resource bundle.
    static String
    get(String key, String fallback)
    Retrieves the translated string for the specified key from the current resource bundle.
    Gets the current resource bundle being used for language translations.
    static void
    Sets a custom base name for the resource bundle.
    static void
    setLocale(Locale locale)
    Sets the current locale and updates the resource bundle for the new locale.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Property Details

  • Constructor Details

    • Language

      public Language()
  • Method Details

    • getBundle

      public static ResourceBundle getBundle()
      Gets the current resource bundle being used for language translations.
      Returns:
      the current resource bundle.
    • setBaseName

      public static void setBaseName(String name)
      Sets a custom base name for the resource bundle. Must be called before setting the locale.
      Parameters:
      name - the new base name (e.g., "messages")
    • bundleProperty

      public static javafx.beans.property.ObjectProperty<ResourceBundle> bundleProperty()
      Gets the property object for the current resource bundle.
      Returns:
      the object property holding the resource bundle.
      See Also:
    • bind

      public static javafx.beans.binding.StringBinding bind(String key, String optional)
      Binds a given field (via its ID) to the appropriate value in the current resource bundle. This method returns a `StringBinding` that will automatically update whenever the resource bundle is changed. If the key does not exist in the resource bundle, the key itself will be returned as a fallback.
      Parameters:
      key - the field ID (or key) to look up in the resource bundle. This is typically the `fx:id` of a UI element.
      Returns:
      a `StringBinding` that can be used to bind to a UI element's text property.
    • setLocale

      public static void setLocale(Locale locale)
      Sets the current locale and updates the resource bundle for the new locale. This method loads a new resource bundle based on the provided locale and updates the `bundleProperty` to reflect the new bundle. It also logs the success or failure of setting the locale.
      Parameters:
      locale - the new `Locale` to set for the language, such as `Locale.ENGLISH` or `Locale.forLanguageTag("es")`.
    • autoBind

      public static void autoBind(Object controller)
      Automatically binds the text or promptText properties of UI controls (such as Label and TextInputControl) in the provided controller object to corresponding values in the current resource bundle.

      This method supports two usage modes:

      • FXML-based: Fields annotated with @FXML will be automatically bound using the field name as the key.
      • Code-based: Fields annotated with @Bind can be bound programmatically in non-FXML contexts. The key is derived from the field name.

      Fields annotated with @IgnoreBind will be ignored during the binding process.

      Parameters:
      controller - the controller or object containing UI controls whose properties will be bound to the resource bundle.
    • get

      public static String get(String key, String fallback)
      Retrieves the translated string for the specified key from the current resource bundle. If the key does not exist or the bundle is not loaded, the provided fallback string will be returned instead.

      This method is useful when an immediate, non-observable translation is required (e.g., for static content or initial values).

      Parameters:
      key - the key to look up in the resource bundle.
      fallback - the fallback string to return if the key is missing or the bundle is not available.
      Returns:
      the translated string if available, or the fallback value otherwise.
    • autoBindField

      public static void autoBindField(Object control, String key)
      Automatically binds a single UI control to the corresponding value in the current resource bundle.

      This method is useful when you create controls dynamically at runtime (instead of declaring them as fields in a controller) and still want to automatically bind their text or promptText properties.

      The key used for the binding will be the control's id property. Therefore, make sure the control has its id set appropriately.

      Parameters:
      control - the UI control to bind (e.g., Label, Button, TextField, etc.).