Do not pass literals as localized parameters

Switch View :
ScriptFree
Visual Studio Team System
Do not pass literals as localized parameters

TypeName

DoNotPassLiteralsAsLocalizedParameters

CheckId

CA1303

Category

Microsoft.Globalization

Breaking Change

NonBreaking

Cause

An externally visible 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.

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 System.Resources.ResourceManager class.

When to Exclude Warnings

It is safe to exclude 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.

All string parameters or properties named 'text' or 'message' are flagged. Users can eliminate noise against methods which shouldn't 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.

Visual Basic
Imports System

<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
                Throw New ArgumentOutOfRangeException( _
                    "hour", hour, _
                    "The valid range is 0 - 23.")
            End If

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

        End Sub

    End Class

End Namespace

C#
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

[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)
            {
                throw new ArgumentOutOfRangeException(
                    "hour", hour, 
                    "The valid range is 0 - 23.");
            }

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

C++
using namespace System;
using namespace System::Globalization;
using namespace System::Reflection;
using namespace System::Resources;

[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)
            {
                throw gcnew ArgumentOutOfRangeException(
                    "hour", hour, 
                    "The valid range is 0 - 23.");
            }

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

See Also

Concepts

Resources in Applications

Community Content

Rahul Techie
Parameter doesn't exist in FxCop v1.36
Hi John, $0$0 $0 $0Can u please tell me in which version I will get such a rule?$0 $0Is there any similar rule in new version?$0 $0Any link to write custom rule for localized parameters?$0

John T. Angle
Parameter doesn't exist in FxCop v1.36
This parameter appears to have been removed in v1.36 - probably because the existing implementation is somewhat clunky (checking the parameter name to be 'text' or 'message', or being able to assign the literal to a variable and then pass that variable as a parameter).

kzu_old_profile
Beware of performance when turning on Localizable=true for Component, Control or Form
The generated code will lookup *every* [Localizable] property from the resource manager, and set the retrieved value using reflection. So beware of the potential performance impact if you're not really going to localize your application. It may be better to evaluate and consciously ignore the places (like InitializeComponent) where it makes sense.

Wernerr
Retrieve Resource String and format as a wrapped string.