Here you can find the source of getLocaleProperties(Locale locale)
Parameter | Description |
---|---|
locale | the locale |
private static Map<String, String> getLocaleProperties(Locale locale)
//package com.java2s; //License from project: Open Source License import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; import java.util.Set; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class Main { /**/*from ww w. j a v a 2s. co m*/ * List of registered bundles */ private static Set<String> resouceBundleBaseNames = new HashSet<String>(); /** * Map of loaded bundles by Locale */ private static Map<Locale, Set<String>> loadedResourceBundles = new HashMap<Locale, Set<String>>(); /** * Map of cached messaged by Locale */ private static Map<Locale, Map<String, String>> cachedMessages = new HashMap<Locale, Map<String, String>>(); /** * Lock objects */ private static ReadWriteLock lock = new ReentrantReadWriteLock(); private static Lock readLock = lock.readLock(); private static Lock writeLock = lock.writeLock(); /** * Get the messages for a locale. * <p> * Will use cache where available otherwise will load into cache from bundles. * * @param locale the locale * @return message map */ private static Map<String, String> getLocaleProperties(Locale locale) { Set<String> loadedBundles = null; Map<String, String> props = null; int loadedBundleCount = 0; try { readLock.lock(); loadedBundles = loadedResourceBundles.get(locale); props = cachedMessages.get(locale); loadedBundleCount = resouceBundleBaseNames.size(); } finally { readLock.unlock(); } if (loadedBundles == null) { try { writeLock.lock(); loadedBundles = new HashSet<String>(); loadedResourceBundles.put(locale, loadedBundles); } finally { writeLock.unlock(); } } if (props == null) { try { writeLock.lock(); props = new HashMap<String, String>(); cachedMessages.put(locale, props); } finally { writeLock.unlock(); } } if (loadedBundles.size() != loadedBundleCount) { try { writeLock.lock(); for (String resourceBundleBaseName : resouceBundleBaseNames) { if (loadedBundles.contains(resourceBundleBaseName) == false) { ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale); Enumeration<String> enumKeys = resourcebundle.getKeys(); while (enumKeys.hasMoreElements() == true) { String key = enumKeys.nextElement(); props.put(key, resourcebundle.getString(key)); } loadedBundles.add(resourceBundleBaseName); } } } finally { writeLock.unlock(); } } return props; } }