Do not pass literals as localized parameters
Do not pass literals as localized parameters
Microsoft docsDescription
An externally visible method passes a string literal as a parameter to a .NET constructor or method, and that string should be localizable.
Cause
A method passes a string literal as a parameter to a .NET constructor or method and that string should be localizable.
This warning is raised when a literal string is passed as a value to a parameter or property and one or more of the following situations is true:
- The System.ComponentModel.LocalizableAttribute attribute of the parameter or property is set to
true.
- The literal string is passed to the
string valueorstring formatparameter of a System.Console.Write or System.Console.WriteLine method overload.
- Rule CA1303 is configured to use the naming heuristic, and a parameter or property name contains the phrase
Text,Message, orCaption.
By default, this rule analyzes the entire codebase, but this is configurable.
How to fix violations
To fix a violation of this rule, replace the string literal with a string retrieved through an instance of the System.Resources.ResourceManager class.
For methods that don't require localized strings, you can eliminate unnecessary CA1303 warnings in the following ways:
- If the naming heuristic option is enabled, rename the parameter or property.
- Remove the System.ComponentModel.LocalizableAttribute attribute on the parameter or property, or set it to
false([Localizable(false)]).
Example
#pragma warning disable CA1303
// The code that's violating the rule is on this line.
#pragma warning restore CA1303When to suppress
It's safe to suppress a warning from this rule if either of the following statements applies:
- The code library will not be localized.
- The string is not exposed to the end user or a developer using the code library.