Class LocaleManager
java.lang.Object
com.everdro1d.libs.locale.LocaleManager
The
LocaleManager
class is responsible for managing application locales, primarily for Swing GUI applications.
It provides functionality to load, save, and manipulate locale data, enabling dynamic text updates for UI components.
Key Features:
- Manages locale files stored in JSON format.
- Supports ISO 639-3 language codes for locale identification.
- Allows dynamic reloading of locales and notifies listeners of locale changes.
- Provides methods to add, remove, and retrieve locale-specific text for UI components.
- Ensures locale data is saved on application shutdown if updated.
Usage Examples:
// Initialize the LocaleManager LocaleManager localeManager = new LocaleManager(Main.class, "ImADeveloper"); // Load a locale file localeManager.loadLocaleFromFile("locale_eng"); // Retrieve text for a specific UI component String buttonText = localeManager.getVariableSpecificMap("MainWindow", "button1", "text"); // Save updated locale data localeManager.saveLocaleToFile("locale_eng", localeManager.getLocaleMap(), true);
Note: This class is designed to work with applications that have text based UI components, GUI or CLI.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLocaleManager
(Class<?> clazz, String developerName) Initializes the LocaleManager with the specified class and developer directory name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a locale map for a specific class.void
Adds a locale map for a specific component.void
addLocaleChangeListener
(LocaleChangeListener listener) Adds a listener to be notified of locale changes.void
addVariableToComponent
(String className, String componentName, String variableName, String text) Adds a text value for a specific variable in the locale map.boolean
checkForLocaleFile
(String fileName) Checks if the specified locale file exists.getAllVariablesWithinClassSpecificMap
(String className) Retrieves all variables within a specific class's locale map.Retrieves a map of available locales that have corresponding files in the locale directory.Retrieves a list of all class names in the locale map.getClassSpecificMap
(String className) Retrieves the locale map for a specific class.getComponentsInClassMap
(String className) Retrieves a list of all component names for a specific class in the locale map.getComponentSpecificMap
(String className, String componentName) Retrieves the locale map for a specific component.Retrieves the current locale code.Retrieves the name of the current locale.String[]
Retrieves an array of ISO 639-3 codes for all available locales.Retrieves the path to the locale directory.Retrieves the entire locale map, which contains UI component text mappings.Retrieves a map of all valid locales with their ISO 639 codes and language names.getVariableInComponent
(String className, String componentName, String variableName) Retrieves the text for a specific variable in the locale map.getVariablesInComponentMap
(String className, String componentName) Retrieves a list of all variable names for a specific component in the locale map.boolean
isLocaleCodeValid
(String locale) Validates whether the provided locale code exists in the valid locale map.void
loadLocaleFromFile
(String localeFileName) Loads a locale from a file.void
Notifies all registered listeners of a locale change.void
Prints all valid locale codes and their corresponding language names to the console.void
reloadLocaleInProgram
(String newLocale) Reloads the locale in the program by loading the specified locale file and notifying all activeLocaleChangeListener
.void
removeClassSpecificMap
(String className) Removes the locale map for a specific class.void
removeComponentSpecificMap
(String className, String componentName) Removes the locale map for a specific component.void
Removes a listener from being notified of locale changes.void
removeVariableInComponent
(String className, String componentName, String variableName) Removes a text value for a specific variable in the locale map.void
saveLocaleToFile
(String localeFileName, Map<String, Map<String, Map<String, String>>> localeMap, boolean overwrite) Saves the provided locale map to a file.void
setLocaleDirectoryPath
(Path localeDirPath) Sets the path to the locale directory.void
Sets the locale map and marks it as updated.
-
Constructor Details
-
LocaleManager
Initializes the LocaleManager with the specified class and developer directory name. Sets up the locale directory path and initializes the valid locales map.- Parameters:
clazz
- the class associated with the applicationdeveloperName
- the developer's name for directory organization
-
-
Method Details
-
loadLocaleFromFile
Loads a locale from a file. Defaults to English if the file does not exist or is invalid. The file name should follow the format "locale_[id]" or "[id]" (e.g., "locale_eng" or "eng").- Parameters:
localeFileName
- the name of the locale file (without the file extension)
-
saveLocaleToFile
public void saveLocaleToFile(String localeFileName, Map<String, Map<String, Map<String, String>>> localeMap, boolean overwrite) Saves the provided locale map to a file. The file name should follow the format "locale_[id]" or "[id]".- Parameters:
localeFileName
- the name of the locale file (without the file extension)localeMap
- the locale map to saveoverwrite
- whether to overwrite an existing file with the same name
-
isLocaleCodeValid
Validates whether the provided locale code exists in the valid locale map.- Parameters:
locale
- the locale code to validate- Returns:
- true if the locale code is valid, false otherwise
-
checkForLocaleFile
Checks if the specified locale file exists. Creates the "/locales/" directory in application config if it does not exist.- Parameters:
fileName
- the name of the locale file (ex: locale_eng)- Returns:
- true if the locale file exists, false otherwise
-
printValidLocales
public void printValidLocales()Prints all valid locale codes and their corresponding language names to the console. -
getISOCodes
Retrieves an array of ISO 639-3 codes for all available locales.- Returns:
- an array of 3-letter ISO 639 codes
-
reloadLocaleInProgram
Reloads the locale in the program by loading the specified locale file and notifying all activeLocaleChangeListener
.Example Usage:
localeManager.reloadLocaleInProgram(prefs.get("currentLocale", "eng"));
- Parameters:
newLocale
- the new locale code to load
-
getValidLocaleMap
-
getLocaleMap
Retrieves the entire locale map, which contains UI component text mappings.Outermost key being the Class Name of the UI component, and the inner key being the name of the component, and the third key being the descriptive variable with the value being the text to display.
- Returns:
- the locale map
-
setLocaleMap
Sets the locale map and marks it as updated.Outermost key being the Class Name of the UI component, and the inner key being the name of the component, and the third key being the descriptive variable with the value being the text to display.
- Parameters:
LocaleMap
- the new locale map to set
-
getClassesInLocaleMap
-
getComponentsInClassMap
-
getVariablesInComponentMap
Retrieves a list of all variable names for a specific component in the locale map.- Parameters:
className
- the class name of the componentcomponentName
- the name of the component- Returns:
- a list of variable names, or an empty list if the class or component is not found
-
getClassSpecificMap
-
getAllVariablesWithinClassSpecificMap
-
addClassSpecificMap
-
removeClassSpecificMap
Removes the locale map for a specific class.- Parameters:
className
- the class name to remove
-
getComponentSpecificMap
Retrieves the locale map for a specific component.- Parameters:
className
- the class name of the componentcomponentName
- the name of the component- Returns:
- the locale map for the specified component, or an empty map if not found
-
addComponentSpecificMap
-
removeComponentSpecificMap
-
getVariableInComponent
Retrieves the text for a specific variable in the locale map.- Parameters:
className
- the class name of the componentcomponentName
- the name of the componentvariableName
- the name of the variable- Returns:
- the text for the specified variable, or null if not found
-
addVariableToComponent
public void addVariableToComponent(String className, String componentName, String variableName, String text) Adds a text value for a specific variable in the locale map.- Parameters:
className
- the class name of the componentcomponentName
- the name of the componentvariableName
- the name of the variabletext
- the text value to add
-
removeVariableInComponent
Removes a text value for a specific variable in the locale map.- Parameters:
className
- the class name of the componentcomponentName
- the name of the componentvariableName
- the name of the variable to remove
-
getLocaleDirectoryPath
Retrieves the path to the locale directory.- Returns:
- the path to the locale directory
-
setLocaleDirectoryPath
Sets the path to the locale directory.- Parameters:
localeDirPath
- the new path to the locale directory
-
getCurrentLocale
Retrieves the current locale code.- Returns:
- the current locale code
-
getCurrentLocaleName
Retrieves the name of the current locale.- Returns:
- the name of the current locale
-
getAvailableLocales
-
addLocaleChangeListener
Adds a listener to be notified of locale changes.- Parameters:
listener
- the listener to add
-
removeLocaleChangeListener
Removes a listener from being notified of locale changes.- Parameters:
listener
- the listener to remove
-
notifyLocaleChange
public void notifyLocaleChange()Notifies all registered listeners of a locale change.
-