Read value from Resource Bundle defined in IntelliJ dynamically

24 views Asked by At

I have created a resource bundle with multiple languages in Intellij. I am using IntelliJ's GUI designer and it could use resources bundle to correctly i18n my texts. However, I want to read from this resource bundle inside my code.

I tried to use the read bundle code generated by GUI designer but failed. What should I do to read from this resource bundle?

1

There are 1 answers

0
Jacky Liu On

You may tell IDEA to generate GUI into Java source code in the Settings | Editor | GUI Designer , however as the https://www.jetbrains.com/help/idea/2023.3/gui-designer.html said, it does not encourage the users to use the generated code, as it somehow relies on the IDEA code library com.intellij.DynamicBundle:

    private static Method $$$cachedGetBundleMethod$$$ = null;

    private String $$$getMessageFromBundle$$$(String path, String key) {
        ResourceBundle bundle;
        try {
            Class<?> thisClass = this.getClass();
            if ($$$cachedGetBundleMethod$$$ == null) {
                Class<?> dynamicBundleClass = thisClass.getClassLoader().loadClass("com.intellij.DynamicBundle");
                $$$cachedGetBundleMethod$$$ = dynamicBundleClass.getMethod("getBundle", String.class, Class.class);
            }
            bundle = (ResourceBundle) $$$cachedGetBundleMethod$$$.invoke(null, path, thisClass);
        } catch (Exception e) {
            bundle = ResourceBundle.getBundle(path);
        }
        return bundle.getString(key);
    }

Instead, I suggest you to try follow these guides: https://www.baeldung.com/java-resourcebundle