tags:

views:

210

answers:

3

We are planning on adding multi-language support for our ASP .Net Website. I understand the general process of pulling strings from resource files based on the current culture, but what is the best way to deal with the following situation?

Lets say that we want to display to the user how many Stack Overflow posts they have made today. Lets say that 'i' is the post count. You would then have the following code if you were just working in english.

if(i == 1)
{
    postCountText = "You have made " + i + " post today";
} else {
    postCountText = "You have made " + i + " posts today";
}

So the text is being created based on whether we are dealing in singular or plural posts.

What if we then want to create the post count text in any other language? Obviously we can't have lots of conditional statements like above, so the strings in the resource files need to work automatically to create the desired output text. In another language it may be that it isn't just the 'post(s)' word that changes, but maybe the word 'today' as well, when you move between singular and plural.

The only idea I have had so far is to store two strings in the external resource file, one for singular and one for plural, and then apply a String.Format to these to insert the post count in the correct location.

i.e.

The resource file for English would contain the following two strings:

singlePostCount : "You have made {0} post today"

pluralPostCount : "You have made {0} posts today"

Then I would have the following code to create the output text:

if(i == 1)
{
    postCountText = String.Format(GetResourceString("singlePostCount"), i);
} else {
    postCountText = String.Format(GetResourceString("pluralPostCount"), i);
}

This would then allow the text to change correctly for any languauge. But then, what if a certain language requires different text if there are zero posts? etc etc

Hopefully I have explained this ok. Thanks in advance for any help

+3  A: 

We are dealing with lots of dynamically generated sentences as well (in 3 languages).
We always try to make the sentences as neutral as possible to avoid as many conditional statements as possible:

"Your number of posts today: {0}"

haarrrgh
A: 

Use StringTemplate.

The best way to work with this kind of situation

It is as easy as following.

Assume you have a template file as sample.st

Dear $user$ you have exceeded the usage. Your current balance is $amount$

Just add following code

StringTemplateGroup group =  new StringTemplateGroup("myGroup", "C:\\Templates", DefaultTemplateLexer.class);
StringTemplate Message = group.getInstanceOf("sample");
Message.setAttribute("user", "Sam");
Message.setAttribute("amount", "100");
Message.toString();

Edit: You can use a function for the plurals as following

Dear $user$ you have $posts$ post$plural$

StringTemplate Message = group.getInstanceOf("sample");
Message.setAttribute("user", "Sam");
Message.setAttribute("post", post);

Message.setAttribute("plural", Getplural(post));
Message.toString();

String getplural(int i) {

If i >1 Return “s“; }

More Info at http://www.antlr.org/wiki/display/ST/Five+minute+Introduction

Sachin
This is basically the same like "You made {0} posts today" from the question, I think that's not what he wanted to know. The actual question was: what if the sentence varies depending on the value of $amount$ ??
haarrrgh
+1  A: 

You should definitely treat the singular an plural sentences as different strings, and don't build them from pieces (I would vote Sachin post down, but I don't have "the power" yet :-)

haarrrgh is right about the "Your number of posts today: {0}" form. Although sounds too "computer-like", it is the most scalable solution.

There are languages with very complex plural rules (Arabic has different forms for 0, 1, 2, many; Russian uses singular if modulo 10 is 1 (1, 51, 4231 all use singular forms); Romanian uses different form of plural if modulo 100 is between 20 and 99). For more examples (and exact rules) see the supplemental\plurals.xml file in the CLDR data. (inside core.zip from ftp://ftp.unicode.org/Public/cldr/1.6.1/)

Take a look here for other considerations: http://www.mihai-nita.net/article.php?artID=20060430a

Mihai Nita