rhit-hueyee / CustomTextEditor

This is a custom text editor I want to make for my own use
0 stars 0 forks source link

How to go about changing Locales? #13

Closed rhit-hueyee closed 4 months ago

rhit-hueyee commented 7 months ago

This section of code has made me concerned:

    private static Locale promptLanguage() {
        int choice = JOptionPane.showOptionDialog(
                null,
                "Language:",
                "CTE",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                LANGUAGES,
                LANGUAGES[0]
        );
        if (choice == -1) {System.exit(0);}
        return LOCALES[choice];
    }

This is from the Launcher class, which is the entry point into the software.

Currently the Locales are being stored in the Launcher class in this snippet

private static final String[] LANGUAGES ={"English", "Spanish", "Japanese"};//this is not sustainable
private static final Locale[] LOCALES ={Locale.US, new Locale("es", "ES"), new Locale("ja", "JP")};

But then comes this section within TextEditWrapper

JMenu settingsMenu = new JMenu("Settings");
        JMenu settingsLanguage = new JMenu("Language");
        //JMenuItem menuItemFile = new JMenuItem("File");
        //textEditMenu.add(menuItemFile);
        settingsMenu.add(settingsLanguage);

This gives me the question: how should Locales/Languages be stored and accessed?

rhit-hueyee commented 6 months ago

Here is the current status:

I have added Global Settings #12 and settings contains a current Locale that can be set. I also added a LocaleManager which is where the Languages and Locales are stored. My current problem is that I know how to change the current Locale in settings, I do not know how dispatch an event that calls this.setTitle(Text.getTextFromLocale("Title")); again and updates the title, for example. I have tried an implementation using observers but have been unsuccessful.

rhit-hueyee commented 4 months ago

This has been solved. The setup involved placing a Listener/Observer at each point where the text needs to be changed. I also tried have to place only 1 Observer at the top and recurse through the component, but I found that it didn't work to well, mainly due to speed/inconsistency.

It starts with implementing:

public interface UIChangeListener {
    void updateLangUI();
}

and then adding the method in your class that implements the interface:

@Override
    public void updateLangUI() {
        configureMenus();
    }

Note: idk if Override is neccesary

In general, either directly change the title/text of the thing you want to change, like this: this.setTitle(Text.getTextFromLocale("Title"));

If you want to loop through a bunch of things:

  1. Remove the things from your JMenuToolBar or other containers:
    private void removeAllMenus(JMenuBar jMenuBar) {
        int count = jMenuBar.getMenuCount();
        for (int i = count - 1; i >= 0; i--) {
            jMenuBar.remove(i);
        }
    }

    Then add what you want to add:

    JMenu textEditMenu = new JMenu(Text.getTextFromLocale("GeneralMenuEdit"));
    this.textEditMenuBar.add(textEditMenu);

Specifically for Language update, it is triggered by this code:

languageItem.addActionListener(e -> {
                System.out.println("Setting locale to " + locale.getLanguage());
                settings.setLocale(locale);
                settings.echoUpdateUI();
                this.revalidate();
                this.repaint();
            });