2014-05-15 03:29:32 +02:00
|
|
|
package org.hwo.i18n;
|
|
|
|
|
2014-07-13 17:10:52 +02:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2014-05-15 03:29:32 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.MissingResourceException;
|
2014-07-13 17:10:52 +02:00
|
|
|
import java.util.Properties;
|
2014-05-15 03:29:32 +02:00
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
|
|
public class Messages {
|
|
|
|
|
|
|
|
protected static Messages pInstance;
|
|
|
|
protected static List<Messages> instanceList;
|
|
|
|
protected static Locale activeLocale;
|
|
|
|
|
|
|
|
protected String BUNDLE_NAME = "org.hwo.i18n.messages";
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceBundle defaultResourceBundle;
|
|
|
|
private ResourceBundle localeResourceBundle;
|
2014-07-13 17:10:52 +02:00
|
|
|
private Properties missingKeys;
|
|
|
|
private String missingKeysFileName;
|
2014-05-15 03:29:32 +02:00
|
|
|
|
|
|
|
protected Messages() {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Messages(String bundleName)
|
|
|
|
{
|
|
|
|
BUNDLE_NAME = bundleName;
|
|
|
|
initialize();
|
|
|
|
}
|
2014-07-13 17:10:52 +02:00
|
|
|
|
|
|
|
public void saveMissingStrings() {
|
|
|
|
|
|
|
|
if (missingKeys != null)
|
|
|
|
{
|
|
|
|
System.err.println("Writing missing strings...");
|
|
|
|
|
|
|
|
FileOutputStream fos;
|
|
|
|
try {
|
|
|
|
fos = new FileOutputStream(missingKeysFileName);
|
|
|
|
missingKeys.store(fos, "");
|
|
|
|
fos.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 03:29:32 +02:00
|
|
|
|
|
|
|
private void initialize()
|
|
|
|
{
|
|
|
|
System.err.println(this.getClass().getName() + ": Using Locale:" + activeLocale.getCountry() + " / " + activeLocale.getLanguage());
|
|
|
|
defaultResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
|
|
|
|
localeResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME,activeLocale);
|
|
|
|
|
|
|
|
instanceList.add(this);
|
|
|
|
}
|
|
|
|
|
2014-07-13 17:10:52 +02:00
|
|
|
public String getMissingKeysFileName()
|
|
|
|
{
|
|
|
|
return missingKeysFileName;
|
|
|
|
}
|
|
|
|
public void setMissingKeysFileName(String fileName)
|
|
|
|
{
|
|
|
|
missingKeysFileName = fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void enableMissingKeys()
|
|
|
|
{
|
|
|
|
missingKeys = new Properties();
|
|
|
|
}
|
|
|
|
|
2014-05-15 03:29:32 +02:00
|
|
|
public static String getString(String key) {
|
|
|
|
for (Messages messages: instanceList)
|
|
|
|
{
|
|
|
|
if (messages.hasKey(key))
|
|
|
|
return messages.getInstanceString(key);
|
|
|
|
}
|
2014-07-13 17:10:52 +02:00
|
|
|
if (getInstance().missingKeys != null)
|
|
|
|
{
|
|
|
|
getInstance().missingKeys.setProperty(key, "");
|
|
|
|
}
|
2014-05-15 03:29:32 +02:00
|
|
|
return '!' + key + '!';
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasKey(String key)
|
|
|
|
{
|
|
|
|
return localeResourceBundle.containsKey(key) | defaultResourceBundle.containsKey(key);
|
|
|
|
}
|
|
|
|
|
2014-07-13 17:10:52 +02:00
|
|
|
protected String getInstanceString(String key) throws MissingResourceException
|
2014-05-15 03:29:32 +02:00
|
|
|
{
|
2014-07-13 17:10:52 +02:00
|
|
|
if (localeResourceBundle.containsKey(key))
|
|
|
|
return localeResourceBundle.getString(key);
|
|
|
|
if (defaultResourceBundle.containsKey(key))
|
|
|
|
return defaultResourceBundle.getString(key);
|
|
|
|
throw new MissingResourceException(BUNDLE_NAME, key, "");
|
2014-05-15 03:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Messages getInstance()
|
|
|
|
{
|
|
|
|
return pInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
|
|
|
if (activeLocale == null)
|
|
|
|
activeLocale = Locale.getDefault();
|
|
|
|
|
|
|
|
if (instanceList == null)
|
|
|
|
instanceList = new LinkedList<Messages>();
|
|
|
|
|
|
|
|
if (pInstance == null)
|
|
|
|
pInstance = new Messages();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|