Java Code Examples for java.util.ResourceBundle#getLocale()

The following examples show how to use java.util.ResourceBundle#getLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Level.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: Level.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: Level.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: Level.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: Level.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 6
Source File: Level.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 7
Source File: Log4jLogger.java    From development with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: Level.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: I18n.java    From gettext-commons with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: StringManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: Level.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: StringManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: Level.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: Messages.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: DateFormatSymbols.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: Messages.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: DateFormatSymbols.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: Messages.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: DateFormatSymbols.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: Messages.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;
}