Search code examples
javaswinglocale

How can my default locale be taken into account by the Swing components?


My default locale is French, but the components in my app are still in English.

System.out.println(UIManager.getLookAndFeelDefaults().getDefaultLocale()); // => fr_FR
System.out.println(System.getProperty("user.language")); // => fr

For example, my JFileChooser:

JFileChooser

What's the problem? Is there a way to force the components language value?

I'm using Java 11, Eclipse, Windows 7.


Solution

  • JFileChooser uses as a content pane sun.swing.FilePane. If you take a look at installDefaults() method of this class you will find the following comment:

     // TODO: On windows, get the following localized strings from the OS
    

    Which means it is not implemented yet (and I don't think it will ever be).

    So, the only thing you can do (unfortunately) is to change them manually using UIManager keys:

    FileChooser.viewMenuLabelText
    FileChooser.refreshActionLabelText
    FileChooser.newFolderActionLabelText
    FileChooser.listViewActionLabelText
    FileChooser.detailsViewActionLabelText
    FileChooser.fileSizeKiloBytes
    FileChooser.fileSizeMegaBytes
    FileChooser.fileSizeGigaBytes
    FileChooser.renameErrorTitleText
    FileChooser.renameErrorText
    FileChooser.renameErrorFileExistsText
    FileChooser.lookInLabelText
    FileChooser.fileNameLabelText
    FileChooser.filesOfTypeLabelText
    FileChooser.upFolderToolTipText
    FileChooser.newFolderToolTipText
    FileChooser.viewMenuButtonToolTipText
    FileChooser.saveButtonText
    FileChooser.openButtonText
    FileChooser.cancelButtonText
    FileChooser.updateButtonText
    FileChooser.helpButtonText
    FileChooser.directoryOpenButtonText
    FileChooser.saveButtonToolTipText
    FileChooser.openButtonToolTipText
    FileChooser.cancelButtonToolTipText
    FileChooser.updateButtonToolTipText
    FileChooser.helpButtonToolTipText
    FileChooser.directoryOpenButtonToolTipText
    FileChooser.saveDialogTitleText
    FileChooser.openDialogTitleText
    

    For example:

    //google translate :)
    UIManager.put("FileChooser.newFolderActionLabelText", "créer un nouveau dossier"); 
    

    And there is also the method JFileChoose#setApproveButtonText, but I do not think this covers your needs.