
com.stormpath.sdk.servlet.i18n.DefaultMessageSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stormpath-sdk-servlet Show documentation
Show all versions of stormpath-sdk-servlet Show documentation
Servlet-specific additions allowing one to more easily deploy the Stormpath SDK in a servlet-container-based
web application.
The newest version!
/*
* Copyright 2015 Stormpath, Inc.
*
* 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 com.stormpath.sdk.servlet.i18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* @since 1.0.RC3
*/
public class DefaultMessageSource implements MessageSource {
private static final Logger log = LoggerFactory.getLogger(DefaultMessageSource.class);
public static final String BUNDLE_BASE_NAME = "com.stormpath.sdk.servlet.i18n";
@Override
public String getMessage(String key, Locale locale) {
return getMessage(key, locale, new Object[]{});
}
@Override
public String getMessage(String key, String defaultMessage, Locale locale) {
ResourceBundle bundle = getBundle(locale);
try {
String msg = new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF-8");
return MessageFormat.format(msg, new Object[]{});
} catch (MissingResourceException e) {
log.debug("Couldn't load the property: {} ", key);
return defaultMessage;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Couldn't load property from resource bundle", e);
}
}
@Override
public String getMessage(String key, String defaultMessage, Locale locale, Object... args) {
ResourceBundle bundle = getBundle(locale);
try {
String msg = new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF-8");
return MessageFormat.format(msg, args);
} catch (MissingResourceException e) {
log.debug("Couldn't load the property: {}", key);
return defaultMessage;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Couldn't load property from resource bundle", e);
}
}
@Override
public String getMessage(String key, Locale locale, Object... args) {
ResourceBundle bundle = getBundle(locale);
try {
/* To enable accents and other UTF-8 characters inside properties file we need to read the value in ISO-8858-1
which is the default encoding for properties files according to the Java documentation and re-encode it in UTF-8 */
String msg = new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF-8");
return MessageFormat.format(msg, args);
} catch (MissingResourceException e) {
log.debug("Couldn't load the property: {}", key);
return '!' + key + '!';
} catch (UnsupportedEncodingException e) {
/* Should not happen since properties are always encoded in ISO-8850-1 thus is supported and UTF-8 is supported
by the JVM in all platforms */
throw new IllegalStateException("Couldn't load property from resource bundle", e);
}
}
protected ResourceBundle getBundle(Locale locale) {
return ResourceBundle.getBundle(BUNDLE_BASE_NAME, locale);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy