下面列出了java.util.ResourceBundle#getLocale ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String getLogMessageText(LogMessageIdentifier identifier,
String... params) {
String text;
try {
String msgId = identifier.getMsgId();
ResourceBundle bundle = getResourceBundle();
text = bundle.getString(msgId);
if (params != null) {
MessageFormat mf = new MessageFormat(text, bundle.getLocale());
params = escapeParams(params);
text = mf.format(params, new StringBuffer(), null).toString();
}
text = addMsgIdToMsg(identifier, text);
} catch (MissingResourceException e) {
text = "?? key '" + identifier.name() + "' not found ??";
}
return text;
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
/**
* Creates a new StringManager for a given package. This is a
* private method and all access to it is arbitrated by the
* static getManager method call so that only one StringManager
* per package will be created.
*
* @param packageName Name of package to create StringManager for.
*/
private StringManager(String packageName) {
String bundleName = packageName + ".LocalStrings";
ResourceBundle tempBundle = null;
try {
tempBundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
} catch( MissingResourceException ex ) {
// Try from the current loader (that's the case for trusted apps)
// Should only be required if using a TC5 style classloader structure
// where common != shared != server
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if( cl != null ) {
try {
tempBundle = ResourceBundle.getBundle(
bundleName, Locale.getDefault(), cl);
} catch(MissingResourceException ex2) {
// Ignore
}
}
}
// Get the actual locale, which may be different from the requested one
if (tempBundle != null) {
locale = tempBundle.getLocale();
} else {
locale = null;
}
bundle = tempBundle;
}
/**
* Sets a resource bundle to be used for message translations.
* <p>
* If this is called, the possibly previously specified class loader and
* baseName are invalidated, since the bundle might be from a different
* context. Subsequent calls to {@link #setLocale(Locale)} won't have any
* effect.
*
* @since 0.9
*/
public synchronized void setResources(ResourceBundle bundle)
{
if (bundle == null) {
throw new NullPointerException();
}
this.bundle = bundle;
this.baseName = null;
this.locale = bundle.getLocale();
this.loader = null;
}
private String computeLocalizedLevelName(Locale newLocale) {
// If this is a custom Level, load resource bundles on the
// classpath and return.
if (!defaultBundle.equals(resourceBundleName)) {
return ResourceBundle.getBundle(resourceBundleName, newLocale,
ClassLoader.getSystemClassLoader()).getString(name);
}
// The default bundle "sun.util.logging.resources.logging" should only
// be loaded from the runtime; so use the extension class loader;
final ResourceBundle rb = ResourceBundle.getBundle(defaultBundle, newLocale);
final String localizedName = rb.getString(name);
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
private String computeLocalizedLevelName(Locale newLocale) {
// If this is a custom Level, load resource bundles on the
// classpath and return.
if (!defaultBundle.equals(resourceBundleName)) {
return ResourceBundle.getBundle(resourceBundleName, newLocale,
ClassLoader.getSystemClassLoader()).getString(name);
}
// The default bundle "sun.util.logging.resources.logging" should only
// be loaded from the runtime; so use the extension class loader;
final ResourceBundle rb = ResourceBundle.getBundle(defaultBundle, newLocale);
final String localizedName = rb.getString(name);
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
/**
* Creates a new StringManager for a given package. This is a
* private method and all access to it is arbitrated by the
* static getManager method call so that only one StringManager
* per package will be created.
*
* @param packageName Name of package to create StringManager for.
*/
private StringManager(String packageName) {
String bundleName = packageName + ".LocalStrings";
ResourceBundle tempBundle = null;
try {
tempBundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
} catch( MissingResourceException ex ) {
// Try from the current loader (that's the case for trusted apps)
// Should only be required if using a TC5 style classloader structure
// where common != shared != server
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if( cl != null ) {
try {
tempBundle = ResourceBundle.getBundle(
bundleName, Locale.getDefault(), cl);
} catch(MissingResourceException ex2) {
// Ignore
}
}
}
// Get the actual locale, which may be different from the requested one
if (tempBundle != null) {
locale = tempBundle.getLocale();
} else {
locale = null;
}
bundle = tempBundle;
}
private String computeLocalizedLevelName(Locale newLocale) {
// Resource bundle should be loaded from the defining module
// or its defining class loader, if it's unnamed module,
// of this Level instance that can be a custom Level subclass;
Module module = this.getClass().getModule();
ResourceBundle rb = RbAccess.RB_ACCESS.getBundle(resourceBundleName,
newLocale, module);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
/**
* Initializes this DateFormatSymbols with the locale data. This method uses
* a cached DateFormatSymbols instance for the given locale if available. If
* there's no cached one, this method creates an uninitialized instance and
* populates its fields from the resource bundle for the locale, and caches
* the instance. Note: zoneStrings isn't initialized in this method.
*/
private void initializeData(Locale locale) {
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
DateFormatSymbols dfs;
if (ref == null || (dfs = ref.get()) == null) {
if (ref != null) {
// Remove the empty SoftReference
cachedInstances.remove(locale, ref);
}
dfs = new DateFormatSymbols(false);
// Initialize the fields from the ResourceBundle for locale.
LocaleProviderAdapter adapter
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
// Avoid any potential recursions
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
adapter = LocaleProviderAdapter.getResourceBundleBased();
}
ResourceBundle resource
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
dfs.locale = locale;
// JRE and CLDR use different keys
// JRE: Eras, short.Eras and narrow.Eras
// CLDR: long.Eras, Eras and narrow.Eras
if (resource.containsKey("Eras")) {
dfs.eras = resource.getStringArray("Eras");
} else if (resource.containsKey("long.Eras")) {
dfs.eras = resource.getStringArray("long.Eras");
} else if (resource.containsKey("short.Eras")) {
dfs.eras = resource.getStringArray("short.Eras");
}
dfs.months = resource.getStringArray("MonthNames");
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
dfs.ampms = resource.getStringArray("AmPmMarkers");
dfs.localPatternChars = resource.getString("DateTimePatternChars");
// Day of week names are stored in a 1-based array.
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
// Put dfs in the cache
ref = new SoftReference<>(dfs);
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
if (x != null) {
DateFormatSymbols y = x.get();
if (y == null) {
// Replace the empty SoftReference with ref.
cachedInstances.replace(locale, x, ref);
} else {
ref = x;
dfs = y;
}
}
// If the bundle's locale isn't the target locale, put another cache
// entry for the bundle's locale.
Locale bundleLocale = resource.getLocale();
if (!bundleLocale.equals(locale)) {
SoftReference<DateFormatSymbols> z
= cachedInstances.putIfAbsent(bundleLocale, ref);
if (z != null && z.get() == null) {
cachedInstances.replace(bundleLocale, z, ref);
}
}
}
// Copy the field values from dfs to this instance.
copyMembers(dfs, this);
}
/**
* Returns a resource bundle which is most match to specified locale.
* <p>
* As expected, if specified locale hasn't defined valid resource file, we
* want to load English(default) resource file instead of the resource file
* of default locale.
*
* @param locale
* specified locale.
* @param baseName
* the path of resource.
* @param clazz
* the class whose class loader will be used by loading resource
* bundle.
* @return instance of resource bundle.
*/
@SuppressWarnings("rawtypes")
private static ResourceBundle getMatchedResourceBundle( ULocale locale,
String baseName, Class clazz )
{
ResourceBundle bundle;
bundle = UResourceBundle.getBundleInstance( baseName,
locale,
SecurityUtil.getClassLoader( clazz ) );
if ( bundle != null )
{
// Bundle could be in default locale instead of English
// if resource for the locale cannot be found.
String language = locale.getLanguage( );
String country = locale.getCountry( );
boolean useDefaultResource = true;
if ( language.length( ) == 0 && country.length( ) == 0 )
{
// it is definitely the match, no need to get the
// default resource file again.
useDefaultResource = false;
}
else
{
Locale bundleLocale = bundle.getLocale( );
if ( bundleLocale.getLanguage( ).length( ) == 0
&& bundleLocale.getCountry( ).length( ) == 0 )
{
// it is the match, no need to get the default
// resource file again.
useDefaultResource = false;
}
else if ( language.equals( bundleLocale.getLanguage( ) ) )
{
// Language matched
String bundleCountry = bundleLocale.getCountry( );
if ( country.equals( bundleCountry )
|| bundleCountry.length( ) == 0 )
{
// Country matched or Bundle has no Country
// specified.
useDefaultResource = false;
}
}
}
if ( useDefaultResource )
{
bundle = ResourceBundle.getBundle( baseName,
new Locale( "", "" ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return bundle;
}
/**
* Initializes this DateFormatSymbols with the locale data. This method uses
* a cached DateFormatSymbols instance for the given locale if available. If
* there's no cached one, this method creates an uninitialized instance and
* populates its fields from the resource bundle for the locale, and caches
* the instance. Note: zoneStrings isn't initialized in this method.
*/
private void initializeData(Locale locale) {
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
DateFormatSymbols dfs;
if (ref == null || (dfs = ref.get()) == null) {
if (ref != null) {
// Remove the empty SoftReference
cachedInstances.remove(locale, ref);
}
dfs = new DateFormatSymbols(false);
// Initialize the fields from the ResourceBundle for locale.
LocaleProviderAdapter adapter
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
// Avoid any potential recursions
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
adapter = LocaleProviderAdapter.getResourceBundleBased();
}
ResourceBundle resource
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
dfs.locale = locale;
// JRE and CLDR use different keys
// JRE: Eras, short.Eras and narrow.Eras
// CLDR: long.Eras, Eras and narrow.Eras
if (resource.containsKey("Eras")) {
dfs.eras = resource.getStringArray("Eras");
} else if (resource.containsKey("long.Eras")) {
dfs.eras = resource.getStringArray("long.Eras");
} else if (resource.containsKey("short.Eras")) {
dfs.eras = resource.getStringArray("short.Eras");
}
dfs.months = resource.getStringArray("MonthNames");
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
dfs.ampms = resource.getStringArray("AmPmMarkers");
dfs.localPatternChars = resource.getString("DateTimePatternChars");
// Day of week names are stored in a 1-based array.
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
// Put dfs in the cache
ref = new SoftReference<>(dfs);
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
if (x != null) {
DateFormatSymbols y = x.get();
if (y == null) {
// Replace the empty SoftReference with ref.
cachedInstances.replace(locale, x, ref);
} else {
ref = x;
dfs = y;
}
}
// If the bundle's locale isn't the target locale, put another cache
// entry for the bundle's locale.
Locale bundleLocale = resource.getLocale();
if (!bundleLocale.equals(locale)) {
SoftReference<DateFormatSymbols> z
= cachedInstances.putIfAbsent(bundleLocale, ref);
if (z != null && z.get() == null) {
cachedInstances.replace(bundleLocale, z, ref);
}
}
}
// Copy the field values from dfs to this instance.
copyMembers(dfs, this);
}
/**
* Returns a resource bundle which is most match to specified locale.
* <p>
* As expected, if specified locale hasn't defined valid resource file, we
* want to load English(default) resource file instead of the resource file
* of default locale.
*
* @param locale
* specified locale.
* @param baseName
* the path of resource.
* @param clazz
* the class whose class loader will be used by loading resource
* bundle.
* @return instance of resource bundle.
*/
@SuppressWarnings("rawtypes")
private static ResourceBundle getMatchedResourceBundle( ULocale locale,
String baseName, Class clazz )
{
ResourceBundle bundle;
bundle = UResourceBundle.getBundleInstance( baseName,
locale,
clazz.getClassLoader( ) );
if ( bundle != null )
{
// Bundle could be in default locale instead of English
// if resource for the locale cannot be found.
String language = locale.getLanguage( );
String country = locale.getCountry( );
boolean useDefaultResource = true;
if ( language.length( ) == 0 && country.length( ) == 0 )
{
// it is definitely the match, no need to get the
// default resource file again.
useDefaultResource = false;
}
else
{
Locale bundleLocale = bundle.getLocale( );
if ( bundleLocale.getLanguage( ).length( ) == 0
&& bundleLocale.getCountry( ).length( ) == 0 )
{
// it is the match, no need to get the default
// resource file again.
useDefaultResource = false;
}
else if ( language.equals( bundleLocale.getLanguage( ) ) )
{
// Language matched
String bundleCountry = bundleLocale.getCountry( );
if ( country.equals( bundleCountry )
|| bundleCountry.length( ) == 0 )
{
// Country matched or Bundle has no Country
// specified.
useDefaultResource = false;
}
}
}
if ( useDefaultResource )
{
bundle = ResourceBundle.getBundle( baseName,
new Locale( "", "" ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return bundle;
}
/**
* Returns a resource bundle which is most match to specified locale.
* <p>
* As expected, if specified locale hasn't defined valid resource file, we
* want to load English(default) resource file instead of the resource file
* of default locale.
*
* @param locale
* specified locale.
* @param baseName
* the path of resource.
* @param clazz
* the class whose class loader will be used by loading resource
* bundle.
* @return instance of resource bundle.
*/
@SuppressWarnings("rawtypes")
private static ResourceBundle getMatchedResourceBundle( ULocale locale,
String baseName, Class clazz )
{
ResourceBundle bundle;
bundle = UResourceBundle.getBundleInstance( baseName,
locale,
SecurityUtil.getClassLoader( clazz ) );
if ( bundle != null )
{
// Bundle could be in default locale instead of English
// if resource for the locale cannot be found.
String language = locale.getLanguage( );
String country = locale.getCountry( );
boolean useDefaultResource = true;
if ( language.length( ) == 0 && country.length( ) == 0 )
{
// it is definitely the match, no need to get the
// default resource file again.
useDefaultResource = false;
}
else
{
Locale bundleLocale = bundle.getLocale( );
if ( bundleLocale.getLanguage( ).length( ) == 0
&& bundleLocale.getCountry( ).length( ) == 0 )
{
// it is the match, no need to get the default
// resource file again.
useDefaultResource = false;
}
else if ( language.equals( bundleLocale.getLanguage( ) ) )
{
// Language matched
String bundleCountry = bundleLocale.getCountry( );
if ( country.equals( bundleCountry )
|| bundleCountry.length( ) == 0 )
{
// Country matched or Bundle has no Country
// specified.
useDefaultResource = false;
}
}
}
if ( useDefaultResource )
{
bundle = ResourceBundle.getBundle( baseName,
new Locale( "", "" ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return bundle;
}
/**
* Initializes this DateFormatSymbols with the locale data. This method uses
* a cached DateFormatSymbols instance for the given locale if available. If
* there's no cached one, this method creates an uninitialized instance and
* populates its fields from the resource bundle for the locale, and caches
* the instance. Note: zoneStrings isn't initialized in this method.
*/
private void initializeData(Locale locale) {
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
DateFormatSymbols dfs;
if (ref == null || (dfs = ref.get()) == null) {
if (ref != null) {
// Remove the empty SoftReference
cachedInstances.remove(locale, ref);
}
dfs = new DateFormatSymbols(false);
// Initialize the fields from the ResourceBundle for locale.
LocaleProviderAdapter adapter
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
// Avoid any potential recursions
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
adapter = LocaleProviderAdapter.getResourceBundleBased();
}
ResourceBundle resource
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
dfs.locale = locale;
// JRE and CLDR use different keys
// JRE: Eras, short.Eras and narrow.Eras
// CLDR: long.Eras, Eras and narrow.Eras
if (resource.containsKey("Eras")) {
dfs.eras = resource.getStringArray("Eras");
} else if (resource.containsKey("long.Eras")) {
dfs.eras = resource.getStringArray("long.Eras");
} else if (resource.containsKey("short.Eras")) {
dfs.eras = resource.getStringArray("short.Eras");
}
dfs.months = resource.getStringArray("MonthNames");
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
dfs.ampms = resource.getStringArray("AmPmMarkers");
dfs.localPatternChars = resource.getString("DateTimePatternChars");
// Day of week names are stored in a 1-based array.
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
// Put dfs in the cache
ref = new SoftReference<>(dfs);
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
if (x != null) {
DateFormatSymbols y = x.get();
if (y == null) {
// Replace the empty SoftReference with ref.
cachedInstances.replace(locale, x, ref);
} else {
ref = x;
dfs = y;
}
}
// If the bundle's locale isn't the target locale, put another cache
// entry for the bundle's locale.
Locale bundleLocale = resource.getLocale();
if (!bundleLocale.equals(locale)) {
SoftReference<DateFormatSymbols> z
= cachedInstances.putIfAbsent(bundleLocale, ref);
if (z != null && z.get() == null) {
cachedInstances.replace(bundleLocale, z, ref);
}
}
}
// Copy the field values from dfs to this instance.
copyMembers(dfs, this);
}
/**
* Returns a resource bundle which is most match to specified locale.
* <p>
* As expected, if specified locale hasn't defined valid resource file, we
* want to load English(default) resource file instead of the resource file
* of default locale.
*
* @param locale
* specified locale.
* @param baseName
* the path of resource.
* @param clazz
* the class whose class loader will be used by loading resource
* bundle.
* @return instance of resource bundle.
*/
@SuppressWarnings("rawtypes")
private static ResourceBundle getMatchedResourceBundle( ULocale locale,
String baseName, Class clazz )
{
ResourceBundle bundle;
bundle = UResourceBundle.getBundleInstance( baseName,
locale,
SecurityUtil.getClassLoader( clazz ) );
if ( bundle != null )
{
// Bundle could be in default locale instead of English
// if resource for the locale cannot be found.
String language = locale.getLanguage( );
String country = locale.getCountry( );
boolean useDefaultResource = true;
if ( language.length( ) == 0 && country.length( ) == 0 )
{
// it is definitely the match, no need to get the
// default resource file again.
useDefaultResource = false;
}
else
{
Locale bundleLocale = bundle.getLocale( );
if ( bundleLocale.getLanguage( ).length( ) == 0
&& bundleLocale.getCountry( ).length( ) == 0 )
{
// it is the match, no need to get the default
// resource file again.
useDefaultResource = false;
}
else if ( language.equals( bundleLocale.getLanguage( ) ) )
{
// Language matched
String bundleCountry = bundleLocale.getCountry( );
if ( country.equals( bundleCountry )
|| bundleCountry.length( ) == 0 )
{
// Country matched or Bundle has no Country
// specified.
useDefaultResource = false;
}
}
}
if ( useDefaultResource )
{
bundle = ResourceBundle.getBundle( baseName,
new Locale( "", "" ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return bundle;
}