Localization inspections and quick-fixes
ReSharper provides a number of code inspections that help you localize your application by detecting localizable strings, problems with resources and so on, in resource files and resource usages in code files. These inspections detect localization issues in design time in the open documents and allow you to find localization issues in specific scope up to the entire solution.
In design time, detected issues are highlighted to draw your attention. To resolve the highlighted issues, ReSharper provides a number of resource-specific quick-fixes.
This inspection detects hard-coded string literals and suggests moving them to resource files. If the same string is already defined in resources, ReSharper will suggest replacing the literal with the existing resource entry.
tip
Even if the string literal is not highlighted by this inspection, you can still move it to the resource file with the Move to Resource refactoring.
As not every string needs to be moved to resources and localized, ReSharper provides several ways to configure this inspection. When the 'Element is localizable' inspection is enabled, it works according to the flow chart below. Each step in the algorithm is explained under the chart.
![ReSharper: How the 'Element is localizable' inspection works ReSharper: How the 'Element is localizable' inspection works](https://resources.jetbrains.com/help/img/dotnet/2024.3/localizable_inspection_flowchart.png)
Verbatim strings are prefixed with
@
, for example@"Hello world"
By default, verbatim strings are ignored by the localization inspection, but you can change it using the Don't analyze verbatim strings option on the Code Editing | Inspection Severity page of ReSharper options Alt+R, O.
The Localizable project property defines how to treat strings in a project. To change this property, right-click the project in the Solution Explorer, select Edit project item properties, and then choose Localizable > [Default | Yes | No].
If the current project or any other project that it references contains at least one resource file (.resx), ReSharper will consider the project as localizable by default.
Further configuration is possible for specific functions and properties that can consume string values by marking them with
LocalizableAttribute
, which lets you explicitly enable[Localizable(true)]
or disable[Localizable(false)]
the localization inspection on the consumed strings.Note that symbols could be marked with this attribute via external annotations. For example, external annotations are used to mark
Console.WriteLine
with[Localizable(true)]
.The Localizable Inspector project property defines how to treat string literals that are not consumed by any member marked with
LocalizableAttribute
— Optimistic algorithm will ignore such strings, Pessimistic will flag them as localizable.To change this project property, right-click the project in the Solution Explorer, select Edit project item properties, and then choose Localizable Inspector > [Optimistic | Pessimistic].
The easiest way to disable localization inspection for the current project is to press AltEnter over a highlighted string in the editor and use the corresponding command in the action list:
![ReSharper: Disabling localization inspection for project ReSharper: Disabling localization inspection for project](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_04.png)
This command sets the Localizable property in the current project to No.
If a quick-fix is available for a code issue detected with localization inspection, ReSharper displays the corresponding action indicator on the left of your caret. You can press AltEnter or click the indicator icon to see the list of available actions, To apply a desired action, click it or select it in the list and press Enter.
Here are some quick-fixes for localization inspections:
This quick-fix invokes Move to Resource refactoring to move localizable string to a resource file. It is available when a string is highlighted with a curly underline.
![ReSharper: A quick-fix to move string literal to resource ReSharper: A quick-fix to move string literal to resource](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_05.png)
If ReSharper detects that some string literal is already defined in resources, it suggests to replace the string with the corresponding resource usage:
![ReSharper: A quick-fix to replace a string literal with an existing resource entry ReSharper: A quick-fix to replace a string literal with an existing resource entry](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_15.png)
After applying this quick-fix, the string is replaced with the resource usage.
This group of quick-fixes lets you add [Localizable(false)]
attribute to the selected type or member to suppress the 'Element is localizable' inspection for all usages of this symbol.
If a resource filename is used in a code file, but you haven't created the resource file yet, ReSharper can do it for you. It highlights the resource filename in red and suggests the corresponding quick-fix:
![ReSharper: Create resource file quick-fix ReSharper: Create resource file quick-fix](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_07.png)
After applying the quick-fix, the missing resource file is created.
If a resource name is used in a code file, but you haven't declared this resource yet, ReSharper can do it for you. It highlights the resource name in red and suggests the corresponding quick-fix:
![ReSharper: Create resource item quick-fix ReSharper: Create resource item quick-fix](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_08.png)
After applying the quick-fix, the missing resource declaration is added in the resource file.
If there are several cultures organised in a culture hierarchy, and there is a resource entry that is declared in a culture-specific resource file but is not declared in the default culture, ReSharper highlights the resource declaration and suggests the corresponding quick-fix:
![ReSharper: Resource value is not declared in the base culture ReSharper: Resource value is not declared in the base culture](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_01.png)
After applying the quick-fix, resource declaration is inserted into the default culture where you can replace its value with a new one.
If there are several cultures organised in the culture hierarchy and there is a resource entry, which is declared in the default culture but is not overridden in one or more other cultures, ReSharper highlights the name of resource and suggests the corresponding quick-fix(es):
![ReSharper: Resource value is not overridden in specific cultures ReSharper: Resource value is not overridden in specific cultures](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_09.png)
After applying the quick-fix, resource declaration(s) is inserted into the culture-specific resource file(s) where you can specify correct values.
ReSharper suggests this quick-fix in the following cases:
A resource entry has equal values in neutral and specific cultures;
There are two or more declarations with the same resource name;
There are several cultures organised in the culture hierarchy and there is resource entry that is declared in specific culture, but is not declared in default culture.
![ReSharper: A quick-fix to remove a resource entry ReSharper: A quick-fix to remove a resource entry](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_03.png)
After applying the quick-fix, resource declaration is removed from the current resource file.
Specifying the string type for a resource entry is redundant because it is the default resource entry type. ReSharper will suggest removing the redundant attribute:
![ReSharper: A quick-fix to remove redundant resource entry type ReSharper: A quick-fix to remove redundant resource entry type](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_06.png)
If there are several cultures organised in the culture hierarchy and there is a resource entry, which is declared in the default culture but is not overridden in culture-specific resource files, ReSharper highlights the name of the resource suggests this quick-fix:
![ReSharper: A quick-fix to remove a resource entry, which has no culture-specific overrides ReSharper: A quick-fix to remove a resource entry, which has no culture-specific overrides](https://resources.jetbrains.com/help/img/dotnet/2024.3/Resources__Quick-Fixes_10.png)
After applying this quick-fix, follow the steps of the Safe Delete Resource refactoring.