Vitas
Vitas

Reputation: 253

Unused strings in resource file (C#/silverlight)

We have a silverlight project; all the text messages are located in a .resx resource file. Since the project has a long history and there were a lot of changes, many strings are orphaned (no longer in use).

Now we are going to translate the project into several languages and I do not want to waste the money on unused text translation.

Is there any simple way to locate and remove unused string resources?

Thanks!

Upvotes: 4

Views: 2453

Answers (5)

Erik
Erik

Reputation: 954

Here is a quick-and-... solution that makes my C# life easier: FindUnused.sln

It gets the strings to check from RESOURCES.RESX and counts the times a string is used in .cs and .xaml fies. Because I use localization, the .cs file use quoted strings ("hello_world"), but the .xaml files do not use the quotes. So I treat the extensions in a separate directory walk.

The distillation of the resource strings is a bit...ummmm well... have a look. It works for me. Let me know if you have something neater.

Upvotes: 0

votrubac
votrubac

Reputation: 141

https://resxutils.codeplex.com/

This is the tool we developed for the same purpose.

Upvotes: 0

Craig Stewart
Craig Stewart

Reputation: 41

I had a similar problem. Several thousand resource strings that I'd created for a translation table, many of which were no longer required or reference by code. With around 180 dependent code files, there was no way I was going to manually go through each resource string.

The following code (in vb.net) will go through your project finding orphaned resources. It took around 1 minute for my project. It can be modified to find strings, images or any other resource type.

In summary it;

  • 1) Uses the solution project file to gather all the included code modules and appends them into a single string variable;
  • 2) Loops through all the resource objects, and creates a list (in my case) of those which are strings;
  • 3) Does a string search finding resource string codes in the combined project text variable;
  • 4) Reports resource objects that are not referenced.

The function returns the object names on the windows clipboard for pasting in a spreadsheet or as a list array of the resource names.

'project file is the vbproj file for my solution
Public Function GetUnusedResources(projectFile As String, useClipboard As Boolean, strict As Boolean) As List(Of String)


    Dim myProjectFiles As New List(Of String)
    Dim baseFolder = System.IO.Path.GetDirectoryName(projectFile) + "\"

    'get list of project files 
    Dim reader As XmlTextReader = New XmlTextReader(projectFile)
    Do While (reader.Read())
        Select Case reader.NodeType
            Case XmlNodeType.Element 'Display beginning of element.
                If reader.Name.ToLowerInvariant() = "compile" Then ' only get compile included files
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            If reader.Name.ToLowerInvariant() = "include" Then myProjectFiles.Add((reader.Value))
                        End While
                    End If
                End If
        End Select
    Loop

    'now collect files into a single string
    Dim fileText As New System.Text.StringBuilder
    For Each fileItem As String In myProjectFiles
        Dim textFileStream As System.IO.TextReader
        textFileStream = System.IO.File.OpenText(baseFolder + fileItem)
        fileText.Append(textFileStream.ReadToEnd)
        textFileStream.Close()
    Next

    ' Create a ResXResourceReader for the file items.resx.
    Dim rsxr As New System.Resources.ResXResourceReader(baseFolder + "My Project\Resources.resx")
    rsxr.BasePath = baseFolder + "Resources"
    Dim resourceList As New List(Of String)
    ' Iterate through the resources and display the contents to the console.
    For Each resourceValue As DictionaryEntry In rsxr
        If TypeOf resourceValue.Value Is String Then ' or bitmap or other type if required
            resourceList.Add(resourceValue.Key.ToString())
        End If
    Next
    rsxr.Close()  'Close the reader.

    'finally search file string for occurances of each resource string
    Dim unusedResources As New List(Of String)
    Dim clipBoardText As New System.Text.StringBuilder
    Dim searchText = fileText.ToString()
    For Each resourceString As String In resourceList
        Dim resourceCall = "My.Resources." + resourceString ' find code reference to the resource name
        Dim resourceAttribute = "(""" + resourceString + """)" ' find attribute reference to the resource name
        Dim searchResult As Boolean = False
        searchResult = searchResult Or searchText.Contains(resourceCall)
        searchResult = searchResult Or searchText.Contains(resourceAttribute)
        If Not strict Then searchResult = searchResult Or searchText.Contains(resourceString)
        If Not searchResult Then ' resource name no found so add to list
            unusedResources.Add(resourceString)
            clipBoardText.Append(resourceString + vbCrLf)
        End If
    Next

    'make clipboard object
    If useClipboard Then
        Dim dataObject As New DataObject ' Make a DataObject clipboard
        dataObject.SetData(DataFormats.Text, clipBoardText.ToString())        ' Add the data in string format.
        Clipboard.SetDataObject(dataObject) ' Copy data to the clipboard.
    End If

    Return unusedResources

End Function

Upvotes: 1

MD2002
MD2002

Reputation: 21

I am the author of the script. As mentioned, if you're using C# you will need to edit the call to FINDSTR in the batch file to match the way you are accessing the resources. The script will create an output text file telling you which resources it thinks are not being used, but will not automatically remove the resources from you. Also, it will not work properly if the resources contain Unicode characters, due to a limitation of FINDSTR.

Upvotes: 2

Robert Greiner
Robert Greiner

Reputation: 29722

There's a script that will do this for you.

Upvotes: 2

Related Questions