1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/util/GT.java,v 1.8 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.util;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
/**
* This class provides a wrapper around a gettext message catalog that
* can provide a localized version of error messages. The caller provides
* a message String in the standard java.text.MessageFormat syntax and any
* arguments it may need. The returned String is the localized version if
* available or the original if not.
*/
public class GT {
private final static GT _gt = new GT();
private final static Object noargs[] = new Object[0];
public final static String tr(String message) {
return _gt.translate(message, null);
}
public final static String tr(String message, Object arg) {
return _gt.translate(message, new Object[]{arg});
}
public final static String tr(String message, Object args[]) {
return _gt.translate(message, args);
}
private ResourceBundle _bundle;
private GT() {
try
{
_bundle = ResourceBundle.getBundle("org.postgresql.translation.messages");
}
catch (MissingResourceException mre)
{
// translation files have not been installed
_bundle = null;
}
}
private final String translate(String message, Object args[])
{
if (_bundle != null && message != null)
{
try
{
message = _bundle.getString(message);
}
catch (MissingResourceException mre)
{
// If we can't find a translation, just
// use the untranslated message.
}
}
// If we don't have any parameters we still need to run
// this through the MessageFormat(ter) to allow the same
// quoting and escaping rules to be used for all messages.
//
if (args == null) {
args = noargs;
}
// Replace placeholders with arguments
//
if (message != null)
{
message = MessageFormat.format(message, args);
}
return message;
}
}
|