Android Studio - Include ResourceBundles in Module

怎甘沉沦 提交于 2019-11-29 14:13:21

Faced exactly the same problem. To make it work I finally had to create a resorces folder in my project module's main folder.

here multiple files starting with the same name (as messages in this picture) gets bundled as a resource bundle.

Finally had to call it using ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.logcat") or ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.messages") to get the required resource.

If you include the second project as a library, you might not want to create a new resource folder as suggested in a previous answer (which does work). Instead, you can simply add the library's resource folder to your resource directories in your module's build.gradle: to the android section add

sourceSets {
    main.resources.srcDirs += 'path/to/your/libs/res'
}

If now the added res folder contains org/mypackage/Bundle.properties you can refer to it using

ResourceBundle.getBundle("org.mypackage.Bundle")

Actually adding a new resource folder does nothing more then adding it as a resource directory in build.gradle.

I never tried but Intellij comes with very good integration of Resource Bundles.

Refer this link

http://www.jetbrains.com/idea/webhelp/resource-bundle.html

From the link above

Resource bundle is a set of properties files that have same base name with different language-specific suffixes. A resource bundle contains at least two properties files with similar base name, for example file_en.properties and file_de.properties.

IntelliJ IDEA recognizes properties files, and if two or more properties files with the names that differ only in suffix, are encountered, joins them into a resource bundle. The new node Resource Bundle '(base name)' appears in the Project Tool Window:

You can have these files inside your module or on root as well.

MG Developer

First please ensure your resource folder (where the property file is localted) is in the classpath and you can easily find that by calling the following.

 URLClassLoader ldr = (URLClassLoader)ClassLoader.getSystemClassLoader();
 URL[] urls = ldr.getURLs();
 for(URL url : urls)
   {
      System.out.println(url.getPath());
    }

Now if you find your resources folder in the classpath then you can simply call the bundle base name, in your case ResourceBundle.getBundle("Bundle"), no need for a fully qualified path. Assuming you are using English locale, it should find it. You can further add en_US, en_NZ, en_GB etc if needed.

If you do not find your property folder then make sure it is in the classpath and if you need to add it dynamically follow this thread.

How do you change the CLASSPATH within Java?

Remember the only addition for loading property files dynamically is that you MUST call findResource or findResources API on the class loader to load the property file. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!