On Mon, 13 Jul 2026 21:16:54 GMT, Nir Lisker <[email protected]> wrote:

>> Changing the `Locale` with `Locale.setDefault(..)` is actually quite common 
>> if you want to support changing the `Locale` at runtime. 
>> This is needed because everything usually uses `Locale.getDefault()` (even 
>> JavaFX for translations), so as far as I know, there is really no way around.
>
> If I want to change the locale at runtime I use my own mutable `Locale` and 
> pass it to the relevant methods as is supported with these converters. I 
> didn't know changing the default was common. One reason is that if you want 
> to reset the preferences, you can read the (system) default locale, but if 
> you change it, what do you reset to?

You need to save the default system `Locale`, otherwise you can not reset it 
correctly.

> I use my own mutable Locale and pass it to the relevant methods

This approach is reasonable with converters, but how does e.g. Java (for 
exception messages) or JavaFX Controls know which `Locale` it should use?

Hence `Locale.getDefault()` is the 'standard' approach. And 
`Locale.setDefault(..)` to change that.

In case you are interested, here is one approach I used in one application to 
support changing the `Locale` and loading/saving it as setting (properties 
file):


public final class I18N {

    static {
        String language = Settings.get().language();
        setLocale(Locale.forLanguageTag(language));
    }
    
    ...
    
    public final class Settings {
    
        private Settings() {
            language = Locale.getDefault().getLanguage();
        }
        
        ...
        void loadSettings() { 
            language = properties.getProperty("language", language); 
        }

        public String language() {
            return language;
        }
    
    }
}


There is also a mechanism to set the `Locale` at runtime. 
My example above only shows how I load the language or use the system one 
otherwise on application start.

When I change the `Locale` at runtime, I only need to reload the UI and 
everything is translated correctly in the new language, including JavaFX 
controls and, also important: Decimal and thousands separators. 
And reloading/recreating the UI is usually quite fast, so it looks nearly 
instant.

-------------

PR Review Comment: https://git.openjdk.org/jfx/pull/1880#discussion_r3665272207

Reply via email to