Java tutorial
/* * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.kaiserpfalzEdv.commons; import de.kaiserpfalzEdv.commons.jee.Closeable; import de.kaiserpfalzEdv.commons.jee.Initializable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.io.Serializable; import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; import java.util.Properties; import java.util.ResourceBundle; import static com.google.common.base.Preconditions.checkArgument; import static org.apache.commons.lang3.StringUtils.isNotBlank; /** * This class handles the internationalization via message bundles. * * @author klenkes * @since 1.0.0 */ public class HandleI18NImpl implements Initializable, Closeable, HandleI18N, Serializable { /** * Serial UID of this class. */ private static final long serialVersionUID = -5193578993565207464L; /** * The minimum number of characters in a filename for the I18N handler. */ public static final int MIN_FILE_NAME_LENGTH = 4; /** * The maximum number of characters in a filename for the I18N handler. */ public static final int MAX_FILE_NAME_LENGTH = 500; /** * Default logger. */ private static final Logger LOG = LoggerFactory.getLogger(HandleI18NImpl.class); /** * The java resource bundle used for retrieving the messages. */ private transient ResourceBundle messageBundle = null; /** * The filename (basename) of the property file containing the translations. */ @NotNull @Size(min = MIN_FILE_NAME_LENGTH, max = MAX_FILE_NAME_LENGTH) private String i18nFileName = null; /** * The locale for this translation handler. */ @NotNull private Locale locale = null; /** * Creates a new instance of this handler. * * @param i18nFileName The filename of the jave resource bundle. * @param locale the locale for this handler. * @throws IllegalArgumentException if the filename or the locale is NULL or * the filename is empty. */ public HandleI18NImpl(final String i18nFileName, final Locale locale) { checkArgument(isNotBlank(i18nFileName), "i18nFileName must not be empty"); checkArgument(locale != null, "locale must be set (null is not allowed)"); this.i18nFileName = i18nFileName; this.locale = locale; } @Override public void init() { } @Override public void init(final Properties props) { } @Override public void close() { } /** * Sets the name of the localization resource bundle file. * * @param i18nFileName The filename of the resource bundle. * @throws IllegalArgumentException if the file name given is NULl or empty. */ public void setI18NFileName(@NotNull final String i18nFileName) { checkArgument(i18nFileName != null && !i18nFileName.isEmpty(), "i18nFileName must not be empty"); //noinspection ConstantConditions if (!i18nFileName.equals(this.i18nFileName)) { LOG.trace("Setting message bundle file name to '{}'", i18nFileName); this.i18nFileName = i18nFileName; resetMessageBundle(); } } /** * Retrieves the filename for this localization resource bundle file. * * @return The name set for this resource bundle file. */ public String getI18NFileName() { return i18nFileName; } @Override public void setLocale(@NotNull final Locale locale) { checkArgument(locale != null, "locale must be not null"); //noinspection ConstantConditions if (!locale.equals(this.locale)) { LOG.trace("Setting locale to '{}'.", locale.getDisplayName()); this.locale = locale; resetMessageBundle(); } } @Override public Locale getLocale() { return locale; } /** * Initializes the message bundle (if not already initialized). This is our lazy loading ... * * @return The message bundle. */ private ResourceBundle getMessageBundle() { if (messageBundle == null) { messageBundle = ResourceBundle.getBundle(i18nFileName, locale); } return messageBundle; } /** * Clears our resource bundle cache. */ private void resetMessageBundle() { ResourceBundle.clearCache(); messageBundle = null; } @Override public String get(final String key) { checkArgument(isNotBlank(key), "key must not be empty"); String result; try { result = getMessageBundle().getString(key); } catch (MissingResourceException e) { LOG.warn("Could not find resource string '{}' in resource bundle file '{}'. Returning '{}' instead.", key, i18nFileName, key); result = key; } return result; } @Override public String get(final String key, final Object[] parameters) { MessageFormat message = new MessageFormat(get(key)); message.setLocale(locale); LOG.debug("Translating {}='{}' with parameters: {} ...", key, get(key), parameters); return message.format(parameters); } }