共用方式為


CA1303:不要將常值當做已當地語系化的參數傳遞

型別名稱

DoNotPassLiteralsAsLocalizedParameters

CheckId

CA1303

分類

Microsoft.Globalization

中斷變更

不中斷

原因

方法將字串常值當做參數傳遞至 .NET Framework Class Library 中的建構函式或方法,且該字串應該可以當地語系化。

將常值字串做為值傳遞至參數或屬性,而且下列一或多列個況為真時,就會引發這個警告:

  • 參數或屬性 (property) 的 LocalizableAttribute 屬性 (attribute) 設定為 true。

  • 參數或屬性名稱包含「文字」、「訊息」或「標題」。

  • 傳遞至 Console.Write 或 Console.WriteLine 方法之字串參數的名稱為 "value" 或 "format"。

規則描述

內嵌在原始程式碼中的字串常值難以當地語系化。

如何修正違規

若要修正此規則的違規情形,請以透過 ResourceManager 類別的執行個體 (Instance) 擷取的字串取代字串常值。

隱藏警告的時機

如果不會將程式碼程式庫當地語系化,或者不會使用程式碼程式庫將字串公開 (Expose) 給使用者或程式開發人員,則您可以放心地隱藏這項規則的警告。

使用者可以消除這些不應該透過更改具名之參數或屬性的名稱,或將這些項目當做條件式的方式,來傳遞已當地語系化字串的方法所造成的干擾。

範例

下列範例會顯示當方法的兩個引數的其中一個超出範圍時,擲回例外狀況的方法。 針對第一個引數,例外狀況建構函式會傳遞常值字串,因此會違反此規則。 針對第二個引數,建構函式會正確地傳遞透過 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));
            }
        }
    };
}

請參閱

概念

應用程式中的資源