Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translations with parameters

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
One challange in globalizing/localizing an application is in finding the way to place dynamic values from the application into the tranlations received from the resource manager. Just appending a dynamic value to this static translation is NOT state-of-art. So while the following code outputs a simple translation ...

ResourceManager rm = new ResourceManager("WinClient.Settings.FormLanguageAndLocale", Assembly.GetExecutingAssembly());
Console.Out.WriteLine(rm.GetString("MSG_ApplySimilarCulture"));

... I ask myself how can you put params INTO that text?!
The Console.Out.WriteLine method has got a neat way to integrate params:
Console.Out.WriteLine("My Brother {0} is {} years old.", "Henry", "12");

Is there a way how you can do the same with those translations?
 
For everyone that's looking for paramaeterized translations I build this:

simply call this line to insert params into a translated string. Just ensure that the replacements numbers are within curly brackes e.g. "My Brothers {0} plays for {1} FC"

new StringHandler().insertParam(strToFill, arrayOfStrParams)

---STATIC CLASS------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace WinClient.Misc
{
/// <summary>
/// This Class extends the String class with extra functionality
/// </summary>
public class StringHandler
{
//static, no constructor needed to program

//field
private String str;
//create Property
public String Str
{
get { return str; }
set { str = value; }
}
public String insertParam(ref String strRef, String str0)
{
Regex reg;
//matches every single or more digit within curly brakets
reg = new Regex("\\{0\\}");
String modString = reg.Replace(Str, str0);
Console.Out.WriteLine(modString);
return modString;
}

public String insertParam(String strRef, String[] strings)
{
Str = strRef;
Regex reg;
for(int i=0; i<strings.Length; i++){
String placeHolder = "\\{"+i+"\\}";
reg = new Regex(placeHolder);
Str = reg.Replace(Str, strings);
Console.Out.WriteLine(Str);
}
return Str;
}
}
}
 
It's even easier than that.
:)

Use the resource file to store your format string (as before), but then use the static String.Format() method to populate it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top