Visual Studio – Tool to replace hard coded strings with resources

I have a big ASP.NET project full of hard coded strings, that now have to be extracted to resources. Such strings can be found almost anywhere (in .cs files, .aspx files, .ascx files, etc.) and the amount of files is huge. What I’m looking for is a tool (or any way) to find all these hard coded strings in one shot and put them all together in the resource file, instead of me going manually through each file. Does anything like this exist?

Note: I’m aware that it would have been better to put the strings in resources straight away when they were needed the first time, but this is a 2 years old project where nobody really cared (typical example of “not my problem” philosophy).

Thank you very much.

UPDATE: I tried CodeRush (I couldn’t try RGreatEx for obvious reasons), and I’m having difficulties using it. The main issue is that the strings I’m looking for are located mainly in .aspx files, and strings in those files don’t have the “Refactor to resource” command available.

For example, I can have elements like this:

<dxwgv:ASPxSummaryItem DisplayFormat="{0}" FieldName="TOTAL" ShowInColumn="Total" SummaryType="Sum" />

The part I need to change is ShowInColumn=”Total” and make it like ShowInColumn=”<%$ Resources:PortalResource, Total %>”. This is not a string assignment in a strict way, but an attribute assignment, so the “Refactor!” command of CodeRush doesn’t appear.

My target is to find all of them in one shot and change them in a specific interface (i.e. like a localization tool) instead of looking for them one by one and manually creating the corresponding resource. Refactoring one by one inside each file would be an improvement, but still very time consuming…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could take a look at the resource refactoring tool at

http://www.codeplex.com/ResourceRefactoring

It’s an instance-by-instance tool rather than a batch replacement tool. It’s free and standalone so you don’t need Resharper or Coderush.

Method 2

Check out the new open source project VisualLocalizer on CodePlex: VisualLocalizer page. If you have some ideas, post them as issues – the project is under development and we welcome feedback.

Method 3

VisualStudio lets you search and replace with RegEx. It won’t be the “fix all in one shot” solution, but it should cut back on the amount manual work significantly.

Just a thought.

Method 4

If you have a look at DevExpress’ CodeRush it has the functionaility you are looking for, you may need to automate it to do it a all in one shot.

It has a great deal more too!

Kindness,

Dan

Method 5

Try RGreatEx. This is a plugin for ReSharper:

RGreatEx is the most powerful localizer and coding helper for Visual Studio. Once installed, it lets you localize .NET applications and produce safer code, saving up to 95% of time the developer usually spends on doing the same by hand. Empower yourself with time-saving refactorings, such as “Move to resource” and “Rename resource”. The plug-in will automatically analyze string and resource usage and suggest moving strings to resources.

Method 6

Do you have ReSharper? Then you perhaps should wait for version 5.0. It will have RGreatEx (mentioned by Anton) functionality included. Read the thread from the R# forum on this topic.

Update: The feature will be in R# 5.0. See the official announcement.

Method 7

I’ve just published new tool called Jinnee.Package for string refactor. You can find it on Visual Studio gallery:
http://visualstudiogallery.msdn.microsoft.com/7ec5a225-dea4-47ae-8ebc-450d2980937f?SRC=Home


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x