com.xenoamess.p3c.pmd.I18nResources Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of p3c-pmd Show documentation
Show all versions of p3c-pmd Show documentation
Alibaba Java Coding Guidelines PMD implementations(XenoAmess
TPM)
/*
* Copyright 1999-2017 Alibaba Group.
*
* 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.xenoamess.p3c.pmd;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;
/**
* @author caikang
* @date 2017/05/24
*/
public class I18nResources {
private static final String XML_LITERAL = "xml";
private static final String LANG = System.getProperty("pmd.language", "zh");
private static Locale currentLocale;
private static ResourceBundle resourceBundle = changeLanguage(LANG);
public static ResourceBundle changeLanguage(String language) {
Locale locale = Locale.CHINESE.getLanguage().equals(language) ? Locale.CHINESE : Locale.ENGLISH;
return changeLanguage(locale);
}
public static ResourceBundle changeLanguage(Locale locale) {
if (currentLocale != null && currentLocale.equals(locale)) {
return resourceBundle;
}
currentLocale = locale;
resourceBundle = ResourceBundle.getBundle("messages", locale, new XmlControl());
return resourceBundle;
}
public static String getMessage(String key) {
if (key == null) {
// 暂时返回空字符串
return "";
}
try {
return resourceBundle.getString(key).trim();
} catch (MissingResourceException e) {
return key;
}
}
public static String getMessage(String key, Object... params) {
String value = getMessage(key);
if (params == null || params.length == 0) {
return value;
}
return String.format(value, params);
}
public static String getMessageWithExceptionHandled(String key) {
if (key == null) {
// 暂时返回空字符串
return "";
}
try {
return resourceBundle.getString(key).trim();
} catch (MissingResourceException e) {
return key;
}
}
public static class XmlResourceBundle extends ResourceBundle {
private final Properties props;
XmlResourceBundle(InputStream stream) throws IOException {
props = new Properties();
props.loadFromXML(stream);
}
@Override
@NotNull
protected Object handleGetObject(@NotNull String key) {
Object value = props.getProperty(key);
if (value == null) {
return key;
}
return value;
}
@Override
@NotNull
public Enumeration getKeys() {
List keys = new ArrayList<>();
Enumeration