java-org.hwo.i18n/src/org/hwo/i18n/Messages.java

257 lines
6.1 KiB
Java
Executable File

package org.hwo.i18n;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import static bootstrapper.logging.LogLevel.*;
import static bootstrapper.logging.Logging.*;
public class Messages {
protected static Messages pInstance;
protected static List<Messages> instanceList;
protected static Locale activeLocale;
protected static List<ResourceBundle>
resourceBundles;
protected static List<Properties>
properties;
protected String
BUNDLE_NAME = "org.hwo.i18n.messages";
protected Class<?>
BUNDLE_CLASS = null;
private ResourceBundle defaultResourceBundle;
private ResourceBundle localeResourceBundle;
private Properties missingKeys;
private String missingKeysFileName;
protected Messages() {
BUNDLE_NAME = String.format("%s", getClass().getCanonicalName());
initialize();
}
protected Messages(String bundleName)
{
BUNDLE_NAME = bundleName;
initialize();
}
public void saveMissingStrings() {
if (missingKeys != null)
{
FileOutputStream fos;
try {
fos = new FileOutputStream(missingKeysFileName);
log("Writing missing strings to %s",missingKeysFileName);
missingKeys.storeToXML(fos, "missing translations");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void initialize()
{
log(DEBUG,this.getClass().getName() + ": Using Locale:" + activeLocale.getCountry() + " / " + activeLocale.getLanguage());
defaultResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
localeResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME,activeLocale);
instanceList.add(this);
}
public String getMissingKeysFileName()
{
return missingKeysFileName;
}
public void setMissingKeysFileName(String fileName)
{
if (missingKeys == null){
enableMissingKeys();
}
missingKeysFileName = fileName;
try {
missingKeys.loadFromXML(new FileInputStream(fileName));
} catch (Exception e){
log(e);
}
}
public void enableMissingKeys()
{
if (missingKeys == null)
missingKeys = new Properties();
}
public static String getString(String key) {
for (ResourceBundle bundle: resourceBundles){
if (bundle.containsKey(key))
return bundle.getString(key);
}
for (Messages messages: instanceList)
{
if (messages.hasKey(key))
return messages.getInstanceString(key);
}
for (Properties p: properties){
if (p.containsKey(key))
return p.getProperty(key);
}
if (getInstance().missingKeys != null)
{
if (getInstance().missingKeys.containsKey(key))
return getInstance().missingKeys.getProperty(key);
getInstance().missingKeys.setProperty(key, key);
}
return key;
}
public boolean hasKey(String key)
{
return localeResourceBundle.containsKey(key) | defaultResourceBundle.containsKey(key);
}
protected String getInstanceString(String key) throws MissingResourceException
{
if (localeResourceBundle.containsKey(key))
return localeResourceBundle.getString(key);
if (defaultResourceBundle.containsKey(key))
return defaultResourceBundle.getString(key);
throw new MissingResourceException(BUNDLE_NAME, key, "");
}
public static Messages getInstance()
{
return pInstance;
}
private static String[] generateFileNamesForBase(Class<?> clazz,Locale locale){
if (locale == null)
locale = activeLocale;
return new String[]{
String.format("%s.msg_%s_%s", clazz.getCanonicalName(), locale.getLanguage(), locale.getCountry()),
String.format("%s.msg_%s_%s", clazz.getSimpleName(), locale.getLanguage(), locale.getCountry()),
String.format("%s.msg_%s", clazz.getCanonicalName(), locale.getLanguage()),
String.format("%s.msg_%s", clazz.getSimpleName(), locale.getLanguage()),
String.format("%s.msg", clazz.getCanonicalName()),
String.format("%s.msg", clazz.getSimpleName())
};
}
private static File[] possibleFilesFor(Class<?> clazz){
LinkedList<File> files = new LinkedList<>();
for (String fn: generateFileNamesForBase(clazz, null)){
File f = new File(fn);
if (f.exists())
files.add(f);
}
return files.toArray(new File[0]);
}
private static ResourceBundle tryLoad(InputStream is){
try {
return new PropertyResourceBundle(is);
} catch (Exception e){
log(e);
}
return null;
}
public static boolean loadMessages(Class<?> clazz){
// boolean leastOne = false;
// String[] fileNames = generateFileNamesForBase(clazz, null);
try {
ResourceBundle bundle = ResourceBundle.getBundle(clazz.getCanonicalName(),activeLocale);
if (bundle != null) {
resourceBundles.add(bundle);
log(DEBUG,"ResourceBundle found for %s",clazz.getCanonicalName());
return true;
}
} catch (RuntimeException ex) {
log(ex);
}
return false;
// for (String fn: fileNames){
// InputStream is = clazz.getClassLoader().getResourceAsStream(fn);
// if (is == null){
// if (new File(fn).exists())
// try {
// is = new FileInputStream(fn);
// } catch (FileNotFoundException e) {
// log(e);
// }
// }
// if (is != null){
// ResourceBundle rb = tryLoad(is);
// if (rb != null){
// log(INFO,"I18N: loaded %s",fn);
// resourceBundles.add(rb);
// continue;
// }
// }
//
// Properties prop = new Properties();
// try {
// String xfn = String.format("%s.xml", fn);
// prop.loadFromXML(new FileInputStream(xfn));
// properties.add(prop);
// log(INFO,"I18N: loaded %s",xfn);
// continue;
// } catch (Exception e) {
// }
//
// log(INFO,"I18N: does not exist: %s",fn);
// }
//
// return leastOne;
}
static {
resourceBundles = new ArrayList<>();
properties = new ArrayList<>();
if (activeLocale == null)
activeLocale = Locale.getDefault();
if (instanceList == null)
instanceList = new LinkedList<Messages>();
if (pInstance == null)
pInstance = new Messages();
}
}