i18n: Add support for collecting missing strings
parent
e1d39b5a0c
commit
4fefdc28b6
|
@ -1,9 +1,12 @@
|
|||
package org.hwo.i18n;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class Messages {
|
||||
|
@ -17,6 +20,8 @@ public class Messages {
|
|||
|
||||
private ResourceBundle defaultResourceBundle;
|
||||
private ResourceBundle localeResourceBundle;
|
||||
private Properties missingKeys;
|
||||
private String missingKeysFileName;
|
||||
|
||||
protected Messages() {
|
||||
initialize();
|
||||
|
@ -28,6 +33,23 @@ public class Messages {
|
|||
initialize();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void initialize()
|
||||
{
|
||||
System.err.println(this.getClass().getName() + ": Using Locale:" + activeLocale.getCountry() + " / " + activeLocale.getLanguage());
|
||||
|
@ -37,12 +59,30 @@ public class Messages {
|
|||
instanceList.add(this);
|
||||
}
|
||||
|
||||
public String getMissingKeysFileName()
|
||||
{
|
||||
return missingKeysFileName;
|
||||
}
|
||||
public void setMissingKeysFileName(String fileName)
|
||||
{
|
||||
missingKeysFileName = fileName;
|
||||
}
|
||||
|
||||
public void enableMissingKeys()
|
||||
{
|
||||
missingKeys = new Properties();
|
||||
}
|
||||
|
||||
public static String getString(String key) {
|
||||
for (Messages messages: instanceList)
|
||||
{
|
||||
if (messages.hasKey(key))
|
||||
return messages.getInstanceString(key);
|
||||
}
|
||||
if (getInstance().missingKeys != null)
|
||||
{
|
||||
getInstance().missingKeys.setProperty(key, "");
|
||||
}
|
||||
return '!' + key + '!';
|
||||
}
|
||||
|
||||
|
@ -51,18 +91,13 @@ public class Messages {
|
|||
return localeResourceBundle.containsKey(key) | defaultResourceBundle.containsKey(key);
|
||||
}
|
||||
|
||||
public String getInstanceString(String key)
|
||||
protected String getInstanceString(String key) throws MissingResourceException
|
||||
{
|
||||
try {
|
||||
if (localeResourceBundle.containsKey(key))
|
||||
return localeResourceBundle.getString(key);
|
||||
if (defaultResourceBundle.containsKey(key))
|
||||
return defaultResourceBundle.getString(key);
|
||||
return '!' + key + '!';
|
||||
} catch (MissingResourceException e) {
|
||||
return '!' + key + '!';
|
||||
}
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue