io - Inquiry about ResourceBundle in java -
directly api:
otherwise, getbundle attempts locate property resource file using generated properties file name. generates path name candidate bundle name replacing "." characters "/" , appending string ".properties". attempts find "resource" name using classloader.getresource.
what mean replacing "." characters "/"
example? ps:i ok appending .properties @ end.
say have package named
com.yourgroup.bundles
containing file named
hello_en_us.properties
you have specify either of following load bundle
resourcebundle bundle = resourcebundle.getbundle("com.yourgroup.bundles.hello"); resourcebundle bundle = resourcebundle.getbundle("com/yourgroup/bundles/hello");
basically javadoc telling how translates argument pass getbundle
method find resource on classpath. me, default locale en_us
, so
com.yourgroup.bundles.hello
translates to
com/yourgroup/bundles/hello_en_us.properties
it can use classloader
find resource.
the resourcebundle
implementation returns might custom class, if map name correctly. follow javadoc that. otherwise, it's properties
resource bundle.
the magic happens in resourcebundle#newbundle(...)
string bundlename = tobundlename(basename, locale); // basename being 'com.yourgroup.bundles.hello' in example above ... final string resourcename = toresourcename(bundlename, "properties");
and simply
public final string toresourcename(string bundlename, string suffix) { stringbuilder sb = new stringbuilder(bundlename.length() + 1 + suffix.length()); sb.append(bundlename.replace('.', '/')).append('.').append(suffix); return sb.tostring(); } .... url url = classloader.getresource(resourcename); ... bundle = new propertyresourcebundle(stream); // stream comes url
Comments
Post a Comment