Re: how to use constant string in the resource file.
On 22-8-2006 18:26, Thomas Weidenfeller wrote:
Devendra wrote:
Now I want to use same XYZ string under some constant. Something as
bellow
KeyConstant=XYZ
key1=Some message + KeyConstant
key2=Some Other message + KeyConstant
I know + sign has no meaning here.
Does any body know how to achieve this?
You need to write your own parser for this. You could base it on the
normal parser and just scan for '+' in values on top of it.
However, I regard such an activity as a waste of time. Since you say you
prepare resource bundles - something typically not edited by the user -
I would generate the files, e.g. with a macro processor like m4:
% cat bundle.m4
define(`KeyConstant', `XYZ')dnl
key1=Some message KeyConstant
key2=Some Other message KeyConstant
And a build rule, e.g. in the build system, e.g in a Makefile if make is
used:
bundle.properties: bundle.m4
-rm -f $@
m4 bundle.m4 > $@ || { rm -f $@; exit 1; }
The result would be
% cat bundle.properties
key1=Some message XYZ
key2=Some Other message XYZ
/Thomas
Preprocessing seems a bad idea to me as it complicates the build
process. Furthermore Java provides a good API for localizing and
formatting messages [Example provided]
# BEGIN of MyBundle.properties
# Save this file in the same folder as Messages.java. Most IDE's
# copy this file automatically to the output folder. If not, or
# when you compile Messages.java from the commandline, then you
# should copy this file to the folder that contains the
# compiled Messages.class file.
java=Java
world=World
greeting.hello=Hello, {0} {1}!
greeting.currentTime=In your part of the {1}, the current time is
{0,time,long}
greeting.count=Greeting you from {0} for the
{1,choice,1#first|2#second|2<umpteenth} time
# END of MyBundle.properties
// BEGIN of Messages.java
package intl;
import java.text.*;
import java.util.*;
/**
* @author Roland de Ruiter
*
*/
public class Messages {
public static void main(String[] args) {
String world = Messages.getString("world");
String java = Messages.getString("java");
System.out.println(
Messages.formatString("greeting.hello", java, world));
System.out.println(
Messages.formatString("greeting.currentTime", new Date(),
world));
for (int i = 1; i <= 5; i++) {
System.out.println(
Messages.formatString("greeting.count", java, i));
}
}
private final static String BUNDLE_NAME =
(Messages.class.getPackage() == null
? ""
: Messages.class.getPackage().getName() + "."
) + "MyBundle";
private static ResourceBundle TheResourceBundle;
public static String formatString(String key, Object... values) {
try {
String message = getTheResourceBundle().getString(key);
try {
return MessageFormat.format(message, values);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return '!' + message + '!';
}
} catch (MissingResourceException e) {
e.printStackTrace();
return '!' + key + '!';
}
}
public static String getString(String key) {
try {
return getTheResourceBundle().getString(key);
} catch (MissingResourceException e) {
e.printStackTrace();
return '!' + key + '!';
}
}
private static ResourceBundle getTheResourceBundle() {
if (TheResourceBundle == null) {
TheResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
}
return TheResourceBundle;
}
private Messages() {
// No instances needed of this utility class
}
}
// END of Messages.java
--
Regards,
Roland