Language Scripting.

I have build a script for english version where all messages in game will be read from the script. Corresponding files will be built for each language, say messages_eng.txt, messages_fra.txt, …

So, here is the structure in the text file.

I created a Messages class than when game starts reads the script messages into a vector and I can use getMessage(int index) to read the message below.

/here is what the message looks like in the txt file/
INDEX-9|
“blah blah blah will cost %d production points and %d diplomacy points”

So, is this the best way to do it?
I am currently planning to read the string between the " charaters and replace the %d’s like this

public String replaceNumbers(String s, int[] ints)

where it loops through the int array and replace each occurrence of “%d”, for instance would the first %d be replaced by ints[0], the second one by ints[1], …

example of usage would be
this.replaceNumbers(this.messages.getMessage(9), 2, 8 );
In the example I inserted the numbers 2 and 8 into the message but ofcourse I will use variables for it.

Comments?

Heh, I’m afraid thats the typical n00b translation mistake - assuming the substitution params always occur in the same order. Eg. a phrase like “X will cost Y points” (where X and Y need replacing) may be phrased like “Y points needed for X” in another language. You need to distinguish between them somehow - one way would be to have your translated strings look like: “[1] will cost [2] points” and “[2] points needed for [1]”, which correspond to the order of the params passed in to the replacement method.

I’d also look at replacing your ‘9’ with something human readable, but thats a minor point.

So, the method implementation should prolly be something like

public String replaceNumbers(String s, int[] ints)
{

for(int i=0;i<ints.length;i++)
{
String next="["+i+"]";
String.replaceAll(next, ints[ i ] ) ;
}
}

I do not see the problem with the"9".

Well I’d much rather read:

getMessage(Messages.ITEM_COST);
// or
getMessage("ItemCost");

that an arbitrary ‘9’.

Also, an array of Objects would be more useful than an array of ints, I think it’s pretty likely you’ll want to substiture in values other than ints at some point.

And you’ll have to consider the case where you might want to print the token you’re using to do replacement (ie. you actually want to display “[1]”).

http://java.sun.com/docs/books/tutorial/i18n/index.html

Java covers all of that already.

try also having a look at ‘externalise strings’ in your fav ide.

it works and this is how the method looks. I use strings for everything now, just convert the numbers to string before i use this method. I found out that using the %s notation is best after all, if I were to use [var] then it could be mixed up with someting where you actually WANT to type [10] and not the value itself. %s is never used in any sentences that make sense so better use them.

public static String replaceAll(String s, String[] var)
{
for(int i=0;i<var.length;i++)
{
String next = “%s” +(i+1) ;
s=s.replaceAll(next, var [ i ] );
}
return s;
}

.

Token order does change when localising text, so your mechanism is fundamentally flawed to begin with.
Also assuming the tokeniser special character will never be required is a poor limitation - you would normally allow escaping of the token identifier character to circumvent this limitation.
e.g. the String “%%” in the input String is substituted with the String “%” in the output.

As oNyx has pointed out - If you are using J2SE, all this functionality is provided for you already - why reinvent a substandard wheel, when a very good one has already been provided?
If this is for J2ME, then it is still a valid problem.

His code does take token-order into account…

Your other points are valid though.

Just noted the thread - sorry for my late post :-\

First of all, Java already has the capability to allow for multiple languages!!! There is no need to implement it yourself. It even includes the parameter substitution you mentioned above. Here is the link to the tutorial:
http://java.sun.com/docs/books/tutorial/i18n/index.html

It’s the Java Internationalization. I managed to get my game translated to 10+ languages. All I need to give translators is the original messages bundle file with the English texts. A tool (I use attesoro) easy allows people to translate text to the desired language. It shows missing translations. Also, the messages bundle files are encoded, so I don’t need to worry about translations people are sending back. Just plug them into your game = copy the file into the right directory and you are done!