2010-10-04 28 views
4

我們有一個silverlight項目;所有的文本消息都位於.resx資源文件中。由於該項目歷史悠久,發生了很多變化,許多字符串都是孤立的(不再使用)。資源文件中未使用的字符串(C#/ silverlight)

現在我們將把這個項目翻譯成幾種語言,我不想把錢浪費在未使用的文本翻譯上。

是否有任何簡單的方法來定位和刪除未使用的字符串資源?

謝謝!

+0

[在.NET解決方案中查找未使用的資源](http://stackoverflow.com/questions/245956/find-unused-resources-in-a-net-solution) – spender 2010-10-04 18:07:16

回答

2

有一個script會爲你做這個。

+0

感謝您的鏈接,但是該腳本無法正常工作。還有其他解決方案嗎? – Vitas 2010-10-05 16:03:17

+0

有趣,你得到什麼錯誤? – 2010-10-05 17:08:28

+0

腳本在c#中使用.xaml和.cs文件非常適合我。請參閱我2013年9月24日的網站評論。 – Brent 2013-09-24 13:57:44

2

我是該腳本的作者。如前所述,如果您使用的是C#,則需要在批處理文件中編輯對FINDSTR的調用,以符合您訪問資源的方式。該腳本將創建一個輸出文本文件,告訴您哪些資源不被使用,但不會自動從您那裏移除資源。另外,由於FINDSTR的限制,如果資源包含Unicode字符,它將無法正常工作。

1

我有類似的問題。我爲轉換表創建了幾千個資源字符串,其中很多資源字符串已不再需要或由代碼引用。大約有180個依賴代碼文件,我不會手動通過每個資源字符串。

以下代碼(在vb.net中)將通過您的項目查找孤兒資源。我的項目花了大約1分鐘。可以修改它以查找字符串,圖像或任何其他資源類型。

綜上所述,

  • 1)採用溶液項目文件來收集所有包含的代碼 模塊和將它們附加到一個單一的字符串變量;
  • 2)遍歷所有的資源對象,並創建一個列表(在我的情況下)的字符串;
  • 3)字符串搜索查找組合項目文本變量中的資源字符串代碼;
  • 4)報告未被引用的資源對象。

該函數返回windows剪貼板上的對象名稱,用於粘貼電子表格或作爲資源名稱的列表數組。

'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 
0

https://resxutils.codeplex.com/

這是我們爲同一個目的而開發的工具。

+0

我看到這是一個ClickOnce應用程序。我無法得到它的工作,它說「...該應用程序缺少所需的文件...」。需要採取哪些措施才能使其發揮作用? – kerzek 2015-04-20 15:56:49

+0

感謝您報告此問題。它看起來像CodePlex停止提供文件,可能是由於證書到期。 我們更新了軟件包並驗證了它現在可以安裝。如果您遇到任何問題,請告訴我們。 – votrubac 2015-04-22 00:04:07

相關問題