Class Language
java.lang.Object
io.github.snoopy137.languagemanager.utils.Language
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
PropertiesTypePropertyDescriptionstatic javafx.beans.property.ObjectProperty
<ResourceBundle> Gets the property object for the current resource bundle. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Automatically binds the text or promptText properties of UI controls (such asLabel
andTextInputControl
) 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
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
Retrieves the translated string for the specified key from the current resource bundle.static ResourceBundle
Gets the current resource bundle being used for language translations.static void
setBaseName
(String name) Sets a custom base name for the resource bundle.static void
Sets the current locale and updates the resource bundle for the new locale.
-
Property Details
-
bundle
Gets the property object for the current resource bundle.- See Also:
-
-
Constructor Details
-
Language
public Language()
-
-
Method Details
-
getBundle
Gets the current resource bundle being used for language translations.- Returns:
- the current resource bundle.
-
setBaseName
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
Gets the property object for the current resource bundle.- Returns:
- the object property holding the resource bundle.
- See Also:
-
bind
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
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
Automatically binds the text or promptText properties of UI controls (such asLabel
andTextInputControl
) 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.
- FXML-based: Fields annotated with
-
get
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
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 itsid
set appropriately.- Parameters:
control
- the UI control to bind (e.g.,Label
,Button
,TextField
, etc.).
-