CA1303: Do not pass literals as localized parameters

TypeName

DoNotPassLiteralsAsLocalizedParameters

CheckId

CA1303

Category

Microsoft.Globalization

Breaking Change

Non Breaking

Cause

A method passes a string literal as a parameter to a constructor or method in the .NET Framework class library 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 cases is true:

  • The LocalizableAttribute attribute of the parameter or property is set to true.

  • The parameter or property name contains "Text", "Message", or "Caption".

  • The name of the string parameter that is passed to a Console.Write or Console.WriteLine method is either "value" or "format".

Rule Description

String literals that are embedded in source code are difficult to localize.

How to Fix Violations

To fix a violation of this rule, replace the string literal with a string retrieved through an instance of the ResourceManager class.

When to Suppress Warnings

It is safe to suppress a warning from this rule if the code library will not be localized, or if the string is not exposed to the end user or a developer using the code library.

Users can eliminate noise against methods which should not be passed localized strings by either renaming the parameter or property named, or by marking these items as conditional.

Example

The following example shows a method that throws an exception when either of its two arguments are out of range. For the first argument, the exception constructor is passed a literal string, which violates this rule. For the second argument, the constructor is correctly passed a string retrieved through a ResourceManager.

Imports System
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms


<assembly: System.Resources.NeutralResourcesLanguageAttribute("en-US")>
Namespace GlobalizationLibrary

    Public Class DoNotPassLiterals

        Dim stringManager As System.Resources.ResourceManager

        Sub New()
            stringManager = New System.Resources.ResourceManager( _
                "en-US", System.Reflection.Assembly.GetExecutingAssembly())
        End Sub

        Sub TimeMethod(hour As Integer, minute As Integer)

            If(hour < 0 Or hour > 23) Then
                MessageBox.Show( _
                    "The valid range is 0 - 23.") 'CA1303 fires because the parameter for method Show is Text
            End If

            If(minute < 0 Or minute > 59) Then
                 MessageBox.Show( _
                    stringManager.GetString("minuteOutOfRangeMessage", _
                        System.Globalization.CultureInfo.CurrentUICulture))
            End If

        End Sub

    End Class

End Namespace
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;



[assembly: NeutralResourcesLanguageAttribute("en-US")]
namespace GlobalizationLibrary
{
    public class DoNotPassLiterals
    {
        ResourceManager stringManager;
        public DoNotPassLiterals()
        {
            stringManager =
            new ResourceManager("en-US", Assembly.GetExecutingAssembly());
        }

        public void TimeMethod(int hour, int minute)
        {
            if (hour < 0 || hour > 23)
            {
                MessageBox.Show(
                "The valid range is 0 - 23."); //CA1303 fires because the parameter for method Show is Text
            }

            if (minute < 0 || minute > 59)
            {
                MessageBox.Show(
                stringManager.GetString(
                "minuteOutOfRangeMessage", CultureInfo.CurrentUICulture));
            }
        }
    }

}


using namespace System;
using namespace System::Globalization;
using namespace System::Reflection;
using namespace System::Resources;
using namespace System::Windows::Forms;

[assembly: NeutralResourcesLanguageAttribute("en-US")];
namespace GlobalizationLibrary
{
    public ref class DoNotPassLiterals
    {
        ResourceManager^ stringManager;

    public:
        DoNotPassLiterals()
        {
            stringManager = 
                gcnew ResourceManager("en-US", Assembly::GetExecutingAssembly());
        }

        void TimeMethod(int hour, int minute)
        {
            if(hour < 0 || hour > 23)
            {
                MessageBox::Show(
                    "The valid range is 0 - 23."); //CA1303 fires because the parameter for method Show is Text
            }

            if(minute < 0 || minute > 59)
            {
                MessageBox::Show(
                    stringManager->GetString("minuteOutOfRangeMessage", CultureInfo::CurrentUICulture));
            }
        }
    };
}

See Also

Concepts

Resources in Applications