Find all source hardcoded strings

C#.NetVisual StudioLocalization

C# Problem Overview


I need to move all the hard coded strings in my source code in .resx files. Is there a tool that could help me find all the hardcoded strings within C# code?

C# Solutions


Solution 1 - C#

ReSharper 5 is obvious a choice, but many tips must be set so as to achieve your goals,

  1. Turn on solution wide analysis.
  2. Go to ReSharper|Options|Code Inspection|Inspection Severity|Potential Code Quality Issues|Element is localizable set to Show as error.
  3. Go back to Solution Explorer and click on the project (csproj).
  4. In Properties panel under ReSharper category, set Localizable to Yes, Localizable Inspector to Pessimistic.

Then you can find almost all you need in Errors in Solution panel.

Hope this helps.

Solution 2 - C#

Or do a search based upon a regular expression like discussed here:

https://vosseburchttechblog.azurewebsites.net/index.php/2014/12/16/find-all-string-literals-in-c-code-files-but-not-the-ones-in-comments/

(?=(^((?!///).)*$)).*((".+?")|('.+?')).*

Solution 3 - C#

You could always do a search for the " sign in all the .cs files. That should get you to most of them, without too much noise.

Solution 4 - C#

This tool http://visuallocalizer.codeplex.com/ allows for batch-move strings to resources, together with other features. It is FOSS so maybe you can give it a try. (I am involved)

Solution 5 - C#

Resharper 5.0 (Beta) allows you to move strings to resources (it has built in Localization feature). Give it a try. Beta works fine, i use it every day and have no problems. Best of all it's free until out of beta. I even recommend using night builds as they seem to be stable.

> Software localization and globalization have always been tough and at times unwanted tasks for developers. ReSharper 5 greatly simplifies working with resources by providing a full stack of features for resx files and resource usages in C# and VB.NET code, as well as in ASP.NET and XAML markup. > > Dedicated features include Move string to resource, Find usages of resource and other navigation actions. Combined with refactoring support, inspections and fixes, you get a convenient localization environment.

Solution 6 - C#

Some are found by FxCop. Not sure what its limits are, I think it depends on parameter and property names (eg: a property called "Text" is considered to be localized).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCornelView Question on Stackoverflow
Solution 1 - C#Lex LiView Answer on Stackoverflow
Solution 2 - C#Patrick KoorevaarView Answer on Stackoverflow
Solution 3 - C#David HedlundView Answer on Stackoverflow
Solution 4 - C#j_malyView Answer on Stackoverflow
Solution 5 - C#MadBoyView Answer on Stackoverflow
Solution 6 - C#Stefan SteineggerView Answer on Stackoverflow