A few months I had to estimate how much time/effort was involved in localizing one .NET application. As part of this task I had to search for all the hard-coded strings in the source code that should be localizable, which is a tedious and very time-consuming task.
The good thing is that Resharper has good localization support which can save you many hours or even days of work – imagine having to go through all files and search for the strings manually! In this post I’ll show you how you can use Resharper to detect localizable strings and how you can enable or disable localization for a particular project, class, method or even an individual string.
Configuring localization inspection
If you want ReSharper to find and highlight strings that are localizable you must enable the localization features for each project – just select the project in the Solution Explorer, press F4 and ensure that the Localizable property has the value Yes:
Setting the Localizable Inspector property to Pessimistic means that all possible elements will be analyzed.
Finding localization issues
After configuring the projects in the solution you can now start searching for localization issues by right-clicking the solution file and select Find Code Issues. In the Inspection Results go to Potential Code Quality Issues > Element is localizable to see the results:
Double-click any item to navigate to the source code:
Resharper will highlight all hard-coded strings but usually there are many that should not be localized. Below you can see how to disable localization for a class, method or a string.
Setting classes or methods as localizable
You can use the [Localizable] attribute to specify if a class should be localized as or not:
The same applies to methods:
Please note that you can mark a class a localizable and one or more methods of that class as non-localizable.
Setting individual strings as non-localizable
One way to mark a string as non-localizable is to convert it to a verbatim string:
As you can see the string is now being ignored by Resharper:
Constants are ignored as well:
That’s it! Even though there is some manual work involved a good tool like Resharper can make your life much easier. I do recommend spending some time to learn how to use this kind of tools, you never know when you might need them!
Happy coding 🙂