All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polaris.core.msg.MessageResource Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.core.msg;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Qt
 * @since 1.8
 */
public class MessageResource {
	private final String baseName;
	private final Map cache = new ConcurrentHashMap<>();

	public MessageResource(String... baseNames) {
		this(AggregateResourceBundles.aggregateNames(baseNames));
	}

	public MessageResource(String baseName) {
		this.baseName = baseName;
	}

	public MemoryResourceBundle getBundle(Locale locale) {
		return cache.computeIfAbsent(locale, k -> {
			MemoryResourceBundle bundle = new MemoryResourceBundle(this.baseName, locale);
			bundle.setParent(AggregateResourceBundles.getBundle(this.baseName, locale));
			return bundle;
		});
	}

	public String getMessageOrDefault(String code, String defaults, Locale locale, Object... params) {
		MemoryResourceBundle bundle = getBundle(locale);
		String val = null;
		try {
			val = bundle.getString(code);
		} catch (MissingResourceException e) {
			val = defaults;
		}
		if (val == null) {
			return val;
		}
		if (params.length == 0) {
			return val;
		}
		return MessageFormat.format(val, params);
	}

	public String getMessage(String code, Object... params) {
		return getMessageOrDefault(code, "", Locale.getDefault(), params);
	}

	public String getMessageOrDefault(String code, String defaults, Object... params) {
		return getMessageOrDefault(code, defaults, Locale.getDefault(), params);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy