Java JFileChooser "New Folder" translation

91 views Asked by At

I'm trying to translate JFileChooser into French and so far I have this:

import javax.swing.JFileChooser;
import javax.swing.UIManager;

public class JFileChooserTest {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        
        UIManager.put("FileChooser.acceptAllFileFilterText", "Tous les fichiers");
        UIManager.put("FileChooser.directoryOpenButtonText", "Ouvrir");
        UIManager.put("FileChooser.openButtonText", "Ouvrir");
        UIManager.put("FileChooser.saveButtonText", "Enregitrer");
        UIManager.put("FileChooser.cancelButtonText", "Annuler");
        UIManager.put("FileChooser.lookInLabelText", "Organiser :");
        UIManager.put("FileChooser.fileNameLabelText", "Nom du fichier :");
        UIManager.put("FileChooser.filesOfTypeLabelText", "Type :");
        
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Ouvrir");
        chooser.showOpenDialog(null);
    }
}

I can't find the translation for "New Folder". How can I get it?

https://i.stack.imgur.com/Ps45U.png

2

There are 2 answers

1
JayC667 On

According to https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/macosx/classes/com/apple/laf/AquaFileChooserUI.java, it might be one of

  • "FileChooser.newFolderButtonText"
  • "FileChooser.newFolderTitleText"
  • "FileChooser.newFolderPromptText"

I haven't tried that out myself, just looked for a resource, but this example file seems to point the way.

GL & HF

1
aterai On

The following definition exists in com/sun/swing/internal/plaf/basic/resources/basic_fr.java, so changing it may work.

// com/sun/swing/internal/plaf/basic/resources/basic_fr.java:67:
{ "FileChooser.other.newFolder", "NewFolder" },
{ "FileChooser.other.newFolder.subsequent", "NewFolder.{0}" },
{ "FileChooser.win32.newFolder", "Nouveau dossier" },
{ "FileChooser.win32.newFolder.subsequent", "Nouveau dossier ({0})" },

FileChooserNewFolderTest.java

import java.awt.*;
import javax.swing.*;

public class FileChooserNewFolderTest {
  private Component makeUI() {
    JTextArea log = new JTextArea();
    log.append(UIManager.getString("FileChooser.newFolderActionLabelText") + "\n");
    log.append(UIManager.getString("FileChooser.newFolderAccessibleName") + "\n");
    log.append(UIManager.getString("FileChooser.other.newFolder") + "\n");
    log.append(UIManager.getString("FileChooser.other.newFolder.subsequent") + "\n");
    log.append(UIManager.getString("FileChooser.win32.newFolder") + "\n");
    log.append(UIManager.getString("FileChooser.win32.newFolder.subsequent") + "\n");

    UIManager.put("FileChooser.newFolderActionLabelText", "Nouveau dossier");
    UIManager.put("FileChooser.newFolderAccessibleName", "Nouveau dossier");
    UIManager.put("FileChooser.other.newFolder", "Nouveau dossier");
    UIManager.put("FileChooser.other.newFolder.subsequent", "Nouveau dossier ({0})");
    UIManager.put("FileChooser.win32.newFolder", "Nouveau dossier");
    UIManager.put("FileChooser.win32.newFolder.subsequent", "Nouveau dossier ({0})");

    JButton button = new JButton("show JFileChooser");
    button.addActionListener(e -> {
      JFileChooser fileChooser = new JFileChooser();
      int retValue = fileChooser.showOpenDialog(log.getRootPane());
      if (retValue == JFileChooser.APPROVE_OPTION) {
        log.append(fileChooser.getSelectedFile().getAbsolutePath() + "\n");
      }
    });
    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());
    box.add(button);
    box.add(Box.createHorizontalGlue());
    JPanel p = new JPanel(new BorderLayout());
    p.add(box, BorderLayout.NORTH);
    p.add(new JScrollPane(log));
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (UnsupportedLookAndFeelException ignored) {
        Toolkit.getDefaultToolkit().beep();
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        ex.printStackTrace();
        return;
      }
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.getContentPane().add(new FileChooserNewFolderTest().makeUI());
      frame.setSize(320, 240);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
    });
  }
}