简体   繁体   English

[i18n]从类路径中加载资源包

[英][i18n]Load Resource Bundle out of classpath

I am trying to load a .properties file from some folder other than the classpath of my project for my ResourceBundle loading for internationalization. 我正在尝试从我的项目的类路径之外的某个文件夹中加载.properties文件,以进行ResourceBundle加载以实现国际化。 I have a structure like this: 我有这样的结构:

MyProject
    |
     - src
       |
        -com
         |
          -company
             |
              -UI
              -launcher
              -resources
                   |
                    -i18n.properties
                    -i18n_en_US.properties
                    -i18n_es_ES.properties
                    -i18n_zh_CN.properties

Obviously, it's clearer than putting all i18n files under the same level as src folder. 显然,这比将所有i18n文件放在与src文件夹相同的级别下更为清晰。 So, how can I load them? 那么,如何加载它们? More precisely, in the line of loading my resource bundle, how shall I put the baseName part? 更准确地说,在加载资源包的那一行中,应如何放置baseName部分?

bundle = ResourceBundle.getBundle(baseName,localeInSettings);

I have seen answers around, but not so clear. 我看到了周围的答案,但不清楚。 As I find something, I come to share it with all and make it clearer. 当我发现某些东西时,便开始与所有人共享并使其更加清晰。

It's still in the classpath. 它仍然在类路径中。 Just not in the default package. 只是不在默认软件包中。 So the base name should be 所以基本名称应该是

com.company.resources.i18n

As the experiments proved, the both forms work: 实验证明,这两种形式均有效:

com/WindThunderStudio/resources/i18n

And, 和,

com.WindThunderStudio.resources.i18n

First, it is different from loading normal properties files, you don't note your absolute path with "/" before complete path. 首先,它与加载普通属性文件不同,在完整路径之前,您不必用“ /”标记绝对路径。

Second, the first form works as we all expected, because baseName expected "full qualified names", that is, path with ".". 其次,第一种形式可以像我们所有人所期望的那样工作,因为baseName期望使用“全限定名”,即带有“。”的路径。 As the second form, we can quote from JavaDoc page of ResourceBundle : 作为第二种形式,我们可以引用ResourceBundle JavaDoc页面:

Note:The baseName argument should be a fully qualified class name. 注意:baseName参数应该是完全限定的类名。 However, for compatibility with earlier versions, Sun's Java SE Runtime Environments do not verify this, and so it is possible to access PropertyResourceBundles by specifying a path name (using "/") instead of a fully qualified class name (using "."). 但是,为了与早期版本兼容,Sun的Java SE运行时环境不对此进行验证,因此可以通过指定路径名(使用“ /”)而不是标准类名(使用“。”)来访问PropertyResourceBundles。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM