Android Studio - Include ResourceBundles in Module

和自甴很熟 提交于 2019-12-18 08:12:13

问题


I currently switched from eclipse to android studio. In eclipse I had 2 projects, one android application project and one java project which I included in the android project as library. This java project uses ResourceBundles to create internationalized error messages for it's own errors. This has been my project structure:

/MyApp
   /src
   /res
   ...
/MyLibrary
   /src
   /res (added as source folder to build path)
      /loc
         Bundle_en.properties 

This worked when loading the RessourceBundles as following:

ResourceBundle.getBundle("loc.Bundle", Locale.ENGLISH);

Now I switched to android studio and my new project structure looks like this (added the java library as module):

/MyProject
   /MyApp
      ...
   /MyLibrary
      /src
         /main
            /java
               ...
            /res
               /loc
                  Bundle_en.properties   

But I'm not able to load the ResourceBundles anymore, it's just throwing a java.util.MissingResourceException. I tried a lot of different locations for the ResourceBundles and different paths but I'm going to get crazy because nothing seems to work. Could anybody explain where to put those bundles and how to load them?

Thank you!


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/21911338/android-studio-include-resourcebundles-in-module

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