ReSharper 2018.3 Help

Localization Refactorings

ReSharper provides a set of resource-related refactorings that greatly simplify internationalizing your projects.

Move to Resource

It is a common practice to store localizable strings in resource files when working on project internalization. ReSharper detects strings to be localized and highlights them, so you can easily find and move such strings from your source code to resource files. ReSharper will declare the corresponding resource entry and replace the string in the code with a resource usage.

There are two algorithms that can be applied when ReSharper detects localizable strings: Pessimistic and Optimistic. If the Pessimistic algorithm is chosen, ReSharper looks for localizable strings in all elements, except those which have the attribute Localizable(false). If the Optimistic algorithm is chosen, ReSharper analyzes only elements with the attribute Localizable(true). Depending on these settings, ReSharper defines whether a specific string is localizable, highlights localizable strings and enables the corresponding quick-fix.

By default, the Optimistic algorithm is selected. To change the algorithm for the project, right-click the project in the Solution Explorer, select Edit project item properties in the context menu, and then choose Pessimistic in the Localizable Inspector selector.

To move string to resource

  1. Place the caret at the string that should be localized.

  2. If a string is highlighted, you can use the corresponding quick-fix. Otherwise, choose ReSharper | Refactor | Move… in the main menu or press F6.

  3. In the Move to resource dialog that appears, ReSharper automatically generates the name for the resource entry and proposes a resource file. If necessary, you can change the name in the Name text box and choose other resource file in the Resource File drop-down list.

    ReSharper 'Move to Resource' refactoring

  4. Optionally, you can change the resource string value in the Value text area and add a comment in the Comment text area.

  5. Optionally, you can specify whether to search for identical strings and the search scope by selecting the value in the Find identical items in drop-down list.

  6. Click Next to apply the refactoring.

  7. If you selected the Find identical items in option and ReSharper finds any matching strings defined within the specified scope, you will be able to choose which of these strings should be replaced with usages of the new resource.

After applying the refactoring, specified occurrences of the string are replaced with corresponding resource usages and the new resource declaration appears in the specified .resx file.

After the new resource is created you can override its value for other cultures.

Move Resource

If your project contains multiple resource (.resx) files, this refactoring will help you move existing resources from one resource file to another. If there is only one .resx file in the current project, this refactoring is unavailable.

To move resource to another resource file

  1. Set the caret at the resource usage in a code file:
    Resources Refactorings Move Resource 01
    or at the resource name in a .resx file:

    Resources Refactorings Move Resource 03

  2. Press F6 or choose ReSharper | Refactor | Move… from the main menu. Alternatively, you can press Ctrl+Shift+A, start typing the command name in the pop-up, and then choose it there.

  3. In the Move resource dialog that appears, specify a target resource file where you want to move the resource.

    ReSharper: 'Move Resource' refactoring

  4. Optionally, you can change the resource name.

  5. Click Next to apply the refactoring.

Rename Resource

You can use the Rename refactoring to quickly rename existing resources. After applying the refactoring, all resource declarations in resource files and all resource usages in code files are updated according to the new name.

To rename a resource

  1. Set the caret at the resource usage in a code file:
    Resources Refactorings Move Resource 01
    or at the resource name in a .resx file:

    Resources Refactorings Move Resource 03

  2. Press F2 or choose ReSharper | Refactor | Rename… from the main menu. Alternatively, you can press Ctrl+Shift+A, start typing the command name in the pop-up, and then choose it there.

  3. In the Rename Resource dialog that appears, specify a new name for the resource.

  4. Click Next to apply the refactoring.

Inline Resource

The Inline Resource refactoring substitutes resource usages with the original string and optionally deletes the corresponding resource entries from resource files.

To inline resource

  1. Set the caret at the resource usage in a code file:
    Resources Refactorings Move Resource 01
    or at the resource name in a .resx file:

    Resources Refactorings Move Resource 03

  2. Press Ctrl+Alt+N or choose ReSharper | Refactor | Inline | Inline… from the main menu. Alternatively, you can press Ctrl+Shift+A, start typing the command name in the pop-up, and then choose it there.

  3. In the Inline Resource dialog that appears, specify refactoring options:

    • Inline all usages: if selected, replaces all resource usages in the project with the original string. If deselected, replaces only the resource usage where you invoked this refactoring. Note that this option works only if you invoked the refactoring from the code file.

    • Remove inlined resource declaration: if selected, removes resource declaration from all related resource files. If deselected, leaves declarations intact.

  4. Click Next to apply the refactoring.

Safe Delete Resource

If you are going to delete a resource, use the Safe Delete refactoring to ensure that the delete operation is safe. If there are no resource usages found, the resource will be deleted right away. Otherwise, ReSharper will show all resource usages, allowing you to edit the corresponding code. Removing usages marked with the ThemedIcon Error Screen Gray icon, will lead to compilation errors.

To safe delete resource safely

  1. Set the caret at the resource usage in a code file:
    Resources Refactorings Move Resource 01
    or at the resource name in a .resx file:

    Resources Refactorings Move Resource 03

  2. Press Alt+Delete or choose ReSharper | Refactor | Safe Delete… from the main menu. Alternatively, you can press Ctrl+Shift+A, start typing the command name in the pop-up, and then choose it there.

    • If there are no usages of this resource in code files, the refactoring is applied and resource declarations are removed from all .resx files.

    • If there are usages of this resource in code files, the Safe Delete Resource dialog opens showing all conflicts.

    Resources Refactorings Safe Delete 02
  3. If you have conflicts trying to safe-delete a resource, resolve them manually and click Refresh.

  4. When all conflicts are resolved and disappear from the dialog, click Next to apply the refactoring.

Last modified: 25 April 2019

See Also