
ro.pippo.core.Messages Maven / Gradle / Ivy
/*
* Copyright (C) 2014 the original author or authors.
*
* 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 ro.pippo.core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.pippo.core.route.RouteContext;
import ro.pippo.core.util.ClasspathUtils;
import ro.pippo.core.util.StringUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
/**
* Loads and caches message resource files based on the registered languages in
* application.properties.
*
* This class is based on MessagesImpl.java from the Ninja Web Framework.
*
* @author James Moger
*/
public class Messages {
private static final Logger log = LoggerFactory.getLogger(Messages.class);
private final Map languageMessages;
private final Languages languages;
public Messages(Languages languages) {
this.languages = languages;
this.languageMessages = loadRegisteredMessageResources();
}
/**
* Gets the requested localized message.
*
*
* The current Request and Response are used to help determine the messages
* resource to use.
*
* - Exact locale match, return the registered locale message
*
- Language match, but not a locale match, return the registered
* language message
*
- Return the default resource message
*
*
* The message can be formatted with optional arguments using the
* {@link java.text.MessageFormat} syntax.
*
*
* If the key does not exist in the messages resource, then the key name is
* returned.
*
*
* @param key
* @param routeContext
* @param args
* @return the message or the key if the key does not exist
*/
public String get(String key, RouteContext routeContext, Object... args) {
String language = languages.getLanguageOrDefault(routeContext);
return get(key, language, args);
}
/**
* Gets the requested localized message.
*
*
* - Exact locale match, return the registered locale message
*
- Language match but not a locale match, return the registered language
* message
*
- Return the default resource message
*
*
* The message can be formatted with optional arguments using the
* {@link java.text.MessageFormat} syntax.
*
*
* If the key does not exist in the messages resource, then the key name is
* returned.
*
*
* @param key
* @param language
* @param args
* @return the message or the key if the key does not exist
*/
public String get(String key, String language, Object... args) {
Properties messages = getMessagesForLanguage(language);
String value = messages.getProperty(key);
if (value != null) {
String message = formatMessage(value, language, args);
return message;
} else {
log.warn("Failed to find '{}' in Messages", key);
return key;
}
}
/**
* Gets the requested localized message.
*
*
* The current Request and Response are used to help determine the messages
* resource to use.
*
* - Exact locale match, return the registered locale message
*
- Language match, but not a locale match, return the registered
* language message
*
- Return the supplied default message
*
*
* The message can be formatted with optional arguments using the
* {@link java.text.MessageFormat} syntax.
*
*
* If the key does not exist in the messages resource, then the key name is
* returned.
*
*
* @param key
* @param defaultMessage
* @param routeContext
* @param args
* @return the message or the key if the key does not exist
*/
public String getWithDefault(String key, String defaultMessage,
RouteContext routeContext, Object... args) {
String language = languages.getLanguageOrDefault(routeContext);
return getWithDefault(key, defaultMessage, language, args);
}
/**
* Gets the requested localized message.
*
*
* The current Request and Response are used to help determine the messages
* resource to use.
*
* - Exact locale match, return the registered locale message
*
- Language match, but not a locale match, return the registered
* language message
*
- Return supplied default message
*
*
* The message can be formatted with optional arguments using the
* {@link java.text.MessageFormat} syntax.
*
*
* @param key
* @param defaultMessage
* @param args
* @return the message or the key if the key does not exist
*/
public String getWithDefault(String key, String defaultMessage, String language, Object... args) {
String value = get(key, language, args);
if (value.equals(key)) {
// key does not exist, format default message
value = formatMessage(defaultMessage, language, args);
}
return value;
}
/**
* Returns all localized messages.
*
* The current Request and Response are used to help determine the messages
* resource to use.
*
* - Exact locale match, return the registered locale messages
*
- Language match but not a locale match, return the registered language
* messages
*
- Return the default messages
*
*
*
* @param routeContext
* @return all localized messages
*/
public Map getAll(RouteContext routeContext) {
String language = languages.getLanguageOrDefault(routeContext);
return getAll(language);
}
/**
* Returns all localized messages.
*
* - Exact locale match, return the registered locale messages
*
- Language match but not a locale match, return the registered language
* messages
*
- Return the default messages
*
*
* @param language
* @return all localized messages
*/
public Map getAll(String language) {
Properties messages = getMessagesForLanguage(language);
Map map = new TreeMap<>();
for (Map.Entry