Find/Remove Obsolete Resource Labels
27 Jul 2012 2 Comments
This is part 3 of 4 in a series on how to improve the way we usually work with resource (resx) files. At least the way my team and I work with them.
I generally like to – and do – use resource files for all string constants that are shown to end-users, however I do feel that it is needlessly cumbersome, therefore these posts:
- A Good Way to Handle Multi Language Resource Files
- Verify that your Resource Labels Exists
- Find Obsolete Resource Labels (this one)
- Supercharge your (Resource) Efficiency with Macros
Generally these issues are generic to .NET and not specific to SharePoint, though that is where I’m spending my time writing this.
So – Near the End of the Project – are those Resource Entries Still in Use?
The issue at hand is that you have hundreds of source files of various flavors and they are sprinkled with references to a number of resource files. When code is refactored or just deleted what happens to old resource labels? Likely nothing at all.
Are you happy with a ton of useless resource entries no longer in active use? What if you had to translate it to a couple of languages?
Quite obviously this is no biggie code/quality wise, but still…
The answer is that you run the PowerShell script below, that’ll check – and optionally fix – it
I made a small script to check and remove the excess resource entries to slim down those resource files a bit.
The script will, given a starting location (i.e. the root folder for your solution)
- Go through every code file (to be safe every file, except a list of binary extensions) and look for resource labels of the form “$Resources:filename,label_key“
- Search recursively for resx files
- For every one of those resx files it will look through the resource labels in use and flag those that it cannot find
- (Optionally) Do a “safemode” check where every file is searched for the resource label, i.e. necessary if you are using multiple/other resource lookup methods then the $Resource moniker
- (Optionally) If you choose you may remove them automatically but do make a dry run first to sanity check that you got the paths right and that you have all the source files to be searched
PS> & VerifyResxLabels.ps1 “path to solution dir” [-remove] [-safemode]
(Tip: Download the script to somewhere, write an ampersand (&) and then drag the ps1 file into the PowerShell window and then drag the solution folder to the window.)
You’ll definitely want to pipe the output to a file.
There are obviously some limitations
- It will not: Check out the resx files from source control (but it will show the file error in the output)
- It will not: Respect commented out code – it’s simple pattern matching so commented out code will be treated as actual code (hardly an issue)
Safemode is very slow of necessity and will likely find false positives, i.e. it will play it safe and keep entries that exists in some files, even though the same label may be used in a completely different context
- It’s an (O(n*m) algorithm, with number of files and number of labels) – My test with 1000 unique labels, 28 resx files and 2400 files it takes a night