Export (0) Print
Expand All

Do not pass literals as localized parameters

TypeName

DoNotPassLiteralsAsLocalizedParameters

CheckId

CA1303

Category

Microsoft.Globalization

Breaking Change

NonBreaking

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.

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

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

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.

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.

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));
            }
        }
    };
}

Community Additions

ADD
Show:
© 2014 Microsoft