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