Custom localize refactoring with context action in C# using ReSharper 8.2 SDK

196 Views Asked by At

Localization in our .NET 4.0 C# project is basically the following:

  1. MyCompany.Resources.dll contains two RESX files: A.resx and B.resx with their localized counterparts (A_de.resx, B_hu.resx, etc.). These files consist of the usual RESX xml elemens, like <data name="key" xml:space="preserve"><value>value</value></data>

  2. MyCompany.Common.dll contains a public (singleton) class MyCompanyResourceManager with two methods for accessing the localized values in A.resx and B.resx by key, like GetA(string key) and GetB(string key).

  3. Wherever there is a string in the C# code that needs to be localized, we manually open either A.resx or B.resx with XML editor (the Visual Studio resource editor screws up something in the xml), add a new <data> tag to the XML document with a new key, and in the C# code we manually replace the string with a call to MyCompanyResourceManager.Instance.GetA(...) (or GetB(...)).

This work can be speeded up with code snippets and live templates in ReSharper, but I feel a need for a better solution for this "workflow" (and not for the localization mechanism, that cannot be changed :) ). That's why a want to write a ReSharper plugin that does the repetitive parts. What I would like to achieve:

  • When the caret is on a string literal in the C# code, offer context actions Localize in A and Localize in B
  • When I choose either of them, popup a dialog to prompt the key to be used in the RESX from the user
  • Place the new <data> tag in the corresponding RESX file and replace the string literal with the call the appropriate method. Import the namespace of the MyCompanyResourceManager class if needed, and reference its assembly, if needed. (Referencing the assembly is not a must, because it can be assumed that MyCompany.Common is referenced everywhere)
  • open the chosen RESX file to see the changes

Optional features (to make plugin more widely usable):

  • settings page to specify the RESX files (even more than two), the corresponding method calls, and the class that contains these methods
  • before prompting the key, search in the chosen RESX file for the string literal, just in case it is already localized

My problems / questions:

  • I'm kind of lost in the ReSharper SDK reference and in its incomplete documentation, so a comprehensive material / tutorial on the available classes / interfaces would be great (if there is any)
  • How to open a project / solution file in Visual Studio with the ReSharper API?
  • How to manipulate XML files with the ReSharper API? The sample context action is a reverse string thing, so accessing the literal under the caret is OK.
  • Should I use a context action or a refactoring for this scenario?
  • How to start a refactoring from a context action?
  • Where to start, what should be well understood of the API for this task?

Thanks in advance!

0

There are 0 best solutions below