Guillaume
Guillaume

Reputation: 1792

Resource files, string.Format and placeholders in .NET

As far as I know, the best way to handle dynamic data in a localized string using resource files in .NET is to have your localized string in the resources.resx file with a placeholder in it: Lorem {0} ipsum. and in your code, to handle it this way: string.Format(Resources.My_Localized_Key, myValue);.

My concerns are the following:

1/ How to be sure that the placeholder will be replaced by an actual value? For example, a new developer in your team may need to use this localized string in some new piece of code he's writing but not being aware he has to feed it with some data. Nor what kind of data.

2/ If later, for some reasons, the localized string changes to Lorem {0} ipsum {1}., how can we ensure that all uses of this string across the application will be updated?

Is there any better way to handle this? For example a way to handle these placeholders in a strongly-typed manner without having to use reflection or having to parse the content of the localized string...? Or is it just the way to do it?

Upvotes: 4

Views: 3700

Answers (2)

Clafou
Clafou

Reputation: 15400

In practice, for issue 1 developers would not be likely to reuse a string without checking its content, and if they did they would probably notice the placeholder when they run their code (or QA would). So it's unlikely and wouldn't be the end of the world.

For issue 2 you could use "Find Usages" in Visual Studio for the auto-generated property that it creates for the resource to find every single place it's used and make sure the right number (and order) of parameters is there everywhere it's used. But generally speaking resource strings don't get reused that much anyway (it's actually not recommended to reuse localizable text in different contexts, as the translations may need to be changed in different contexts, due to linguistic rules or space restrictions for example).

Another risk I would see is that a translator may omit or mess up the placeholders, which would be harder to detect. But this would be just one example of a localization bug... There are plenty of other things that can go wrong when localizing an app and there is often no sure way to guard against them.

Upvotes: 1

Nikolay
Nikolay

Reputation: 3828

I think you can write you own Custom Tool for Visual Studio (http://www.codeproject.com/Articles/31257/Custom-Tools-Explained) that generates Resources class from resx-file and use it instead of standard one. In your tool you can parse localized string and if it has placeholders then you can generate method instead of property Resources.My_Localized_Key. Method can look like Resources.My_Localized_KeyFormat(object param) with appropriate number of parameters according to number of placeholders.

But this is just idea and I haven't implemented it myself (but I did implemented custom tool for code generation for resx-files and it works ok).

Or you can use F# which checks params to its version of string.Format at compile-time.

Upvotes: 0

Related Questions