Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization with string.Format

Tags:

c#

uwp

Currently I am mostly doing the localization by putting key-value pairs into a Resources.resw file. So I wonder about how I should localize strings that need formatting or say strings with different grammar orders in different languages. It might be easier to understand what I mean with the examples below.

For example, just as this part in the official document for localization suggests, one language can have the date format of

string.Format("Every {0} {1}", monthName, dayNumber);

while the other uses

string.Format("Every {1} {0}", monthName, dayNumber);

In this situation, what is the best way to localize such a string?

Things/Grammars can be way more complicated than this example. The suggestion in the official document doesn't look good to me because a date can be unpredictable. Or may be you can enumerate the date, but that requires a lot of work. Or let's say we have a string that takes user input, like

 string.Format("Do you want to delete {name}?", name);

In another language it might have this grammar order

string.Format("You want to delete {name} do?", name);

It is impossible to localize the whole sentence as the example suggests in the document.

The only way of avoiding situation that I can think of is not to put user input....

like image 905
Seaky Avatar asked Mar 13 '26 15:03

Seaky


1 Answers

If you have access to the date you could use The Month ("M", "m") Format Specifier

From the documentation:

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("m", 
                  CultureInfo.CreateSpecificCulture("en-us")));
// Displays April 10                        
Console.WriteLine(date1.ToString("m", 
                  CultureInfo.CreateSpecificCulture("ms-MY")));
// Displays 10 April

For string.Format("Do you want to delete {name}?", name); you can

$"Do you want to delete the following user? '{name}'";
like image 109
tymtam Avatar answered Mar 15 '26 04:03

tymtam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!