panda.io.resource.Resources Maven / Gradle / Ivy
Show all versions of panda-core Show documentation
package panda.io.resource;
import java.util.Locale;
import panda.lang.Strings;
public class Resources {
public static Locale getParentLocale(Locale locale) {
if (Strings.isNotEmpty(locale.getVariant())) {
return new Locale(locale.getLanguage(), locale.getCountry());
}
if (Strings.isNotEmpty(locale.getCountry())) {
return new Locale(locale.getLanguage());
}
return null;
}
/**
* Converts the given baseName
and locale
to the bundle name.
*
* This implementation returns the following value:
*
*
* baseName + "_" + language + "_" + country + "_" + variant
*
*
* where language
, country
and variant
are the language,
* country and variant values of locale
, respectively. Final component values that
* are empty Strings are omitted along with the preceding '_'. If all of the values are empty
* strings, then baseName
is returned.
*
* For example, if baseName
is "baseName"
and locale
is
* Locale("ja", "", "XX")
, then "baseName_ja_ _XX"
is
* returned. If the given locale is Locale("en")
, then "baseName_en"
* is returned.
*
* Overriding this method allows applications to use different conventions in the organization
* and packaging of localized resources.
*
* @param base the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle should be loaded
* @return the bundle name for the resource bundle
* @exception NullPointerException if baseName
or locale
is
* null
*/
public static String toBundleName(String base, Locale locale) {
if (locale == null || locale == Locale.ROOT) {
return base;
}
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
return toBundleName(base, language, country, variant);
}
public static String toBundleName(String base, String language, String country, String variant) {
if (Strings.isEmpty(language) && Strings.isEmpty(country) && Strings.isEmpty(variant)) {
return base;
}
StringBuilder sb = new StringBuilder(base);
sb.append('_');
if (Strings.isNotEmpty(variant)) {
sb.append(language).append('_').append(country).append('_').append(variant);
}
else if (Strings.isNotEmpty(country)) {
sb.append(language).append('_').append(country);
}
else {
sb.append(language);
}
return sb.toString();
}
/**
* Converts the given bundleName
to the form required by the
* {@link ClassLoader#getResource ClassLoader.getResource} method by replacing all occurrences
* of '.'
in bundleName
with '/'
and appending a
* '.'
and the given file suffix
. For example, if
* bundleName
is "foo.bar.MyResources_ja_JP"
and suffix
* is "properties"
, then "foo/bar/MyResources_ja_JP.properties"
is
* returned.
*
* @param bundleName the bundle name
* @param suffix the file type suffix
* @return the converted resource name
* @exception NullPointerException if bundleName
or suffix
is
* null
*/
public static 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();
}
}