All rules
CA1303Globalization Enabled by default: No

Do not pass literals as localized parameters

Do not pass literals as localized parameters

Microsoft docs

Description

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 value or string format parameter 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, or Caption.

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 CA1303

When 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.
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0