次の方法で共有


Web パーツをローカライズする

最終更新日: 2010年12月7日

適用対象: SharePoint Foundation 2010

この記事の内容
SharePoint プロジェクトの作成
Web パーツの作成
言語リソースの追加
フィーチャーおよび Web パーツ コントロール記述ファイルのローカライズ
コード記述による、ローカライズされた文字列の取得
Web パーツ プロパティのローカライズ
属性のローカライズ
Web パーツのテスト

このチュートリアルでは、Microsoft SharePoint Foundation 2010 に展開する Web パーツをローカライズする方法について説明します。チュートリアルのこの手法は SharePoint Foundation 2010 に固有です。Microsoft ASP.NET に展開されている Web パーツでは使用できません。

このチュートリアルでは、"Hello, world" というテキストを複数の言語で表示できる簡単な Web パーツを作成します。さらに、Web パーツに関連付けられている表示テキストのローカライズの準備を行う方法についても説明します。たとえば、次のアイテムの表示テキストを準備します。

  • Web パーツを準備するフィーチャー。

  • Web パーツ ギャラリーの Web パーツのエントリ。

  • Web パーツの枠のタイトル。

  • [Web パーツ] メニューのカスタム アクション。

  • Web パーツ エディターのカスタム プロパティ。

  • Web パーツがホストする子コントロール。

前提条件

このチュートリアルの手順は、Microsoft Visual Studio 2010 を使用している開発者を対象としています。前提条件については、「SharePoint ソリューションの開発要件 (英語)」を参照してください。

チュートリアルを完了するには、次のアイテムが必要です。

SharePoint Foundation 2010 におけるローカライズ ソリューションの重要な概念の概要については、「多言語ユーザー インターフェイス (MUI) について」を参照してください。

SharePoint プロジェクトの作成

最初に Visual Studio 2010 で空の SharePoint プロジェクトを作成し、Wingtip.WebParts という名前を付けます。次に、そのプロジェクトをファーム ソリューションとして展開できるように構成します。

注意

このチュートリアルで作成する Web パーツはサンドボックス ソリューションとして展開できます。このソリューションでは Web パーツのすべてのコードを部分的な信頼に基づいて実行できます。ただし、このチュートリアルでは、Web パーツのほかに、言語リソース ファイルも SharePoint インストールのルート フォルダーに展開され、プロジェクトのこの部分では完全な信頼を必要とします。本番環境では、Web パーツを展開するサンドボックス ソリューションとリソース ファイルを展開するファーム ソリューションの 2 つのソリューションを使用できますが、このチュートリアルでは、簡単にするために、ファーム ソリューションのみを使用します。

空の SharePoint プロジェクトを作成するには

  1. 管理者として Visual Studio 2010 を開始します。

  2. [ファイル] メニューで [新規作成] をポイントし、[新しいプロジェクト] をクリックして、[新しいプロジェクト] ダイアログ ボックスを開きます。

  3. [C#] または [Visual Basic] で [SharePoint] ノードを展開して、[2010] をクリックします。

  4. [テンプレート] ウィンドウで [空の SharePoint プロジェクト] をクリックし、プロジェクトの名前を Wingtip.WebParts に変更して、[OK] をクリックします。

    SharePoint カスタマイズ ウィザードが表示されます。

  5. [デバッグのサイトとセキュリティ レベルの指定] ページで、新しい Web パーツを追加する SharePoint サイトの URL を入力するか、既定の場所 (http://< system name>/) を使用します。

  6. [この SharePoint ソリューションの信頼レベル] セクションで [ファーム ソリューションとして配置する] を選択します。

  7. [完了] をクリックします。

    プロジェクトがソリューション エクスプローラーに表示されます。

Web パーツの作成

次は、Web パーツをプロジェクトに追加します。それには、System.Web.UI.WebControls.WebParts 名前空間の WebPart クラスから派生する新しいクラスを作成します。このサンプル Web パーツは、CreateChildControls メソッドをオーバーライドし、子コントロールである Label クラスのインスタンスを追加します。このチュートリアルの後半では、このコントロールに表示されるテキストをローカライズする方法について説明します。このコード例では、Web パーツのすべての種類のユーザー インターフェイス (UI) をローカライズできるように、WebBrowsable 属性および Personalizable 属性で修飾されたカスタム プロパティを作成します。これらの属性により、ユーザーが Web パーツを編集するときに、プロパティがツール ウィンドウに表示されます。そして最後に、カスタム WebPartVerb を作成し、Web パーツ オプション メニューにアイテムを追加します。

Web パーツをプロジェクトに追加するには

  1. ソリューション エクスプローラーで [Wingtip.WebParts] ノードを選択します。[プロジェクト] メニューで [新しいアイテムの追加] をクリックします。

  2. [新しい項目の追加] ダイアログ ボックスで [Web パーツ] を選択します。[名前] フィールドに「LocalizedWebPart」と入力し、[追加] をクリックします。

    LocalizedWebPart クラスがコード エディターで開きます。

  3. コード エディターで、Ctrl + A キーを押してすべて選択します。次に、以下のコードをコピーして貼り付けます。

    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint.WebControls;
    
    namespace Wingtip.WebParts.LocalizedWebPart
    {
        [ToolboxItemAttribute(false)]
        public class LocalizedWebPart : WebPart
        {
    
            // Constants for display strings.
            const string CATEGORY_TEXT = "Text";
            const string LABEL_TEXT = "Hello, world!";
            const string TOOLPANE_BOLD = "Bold";
            const string TOOLPANE_BOLD_DESC = "Make the text bold";
            const string VERB_UNDERLINE = "Underline";
            const string VERB_UNDERLINE_DESC = "Draw a line under the text";
            const string WEB_PART_TITLE = "Localized Web Part";
            const string WEB_PART_DESC = "An example localized Web Part";
    
            // Member variable for the child control.
            private Label m_textLabel;
    
            // Instantiates the Web Part.
            public LocalizedWebPart()
            {
    
                // Add a handler for the PreRender event.
                PreRender += LocalizedWebPart_PreRender;
            }
    
            // Creates the control tree.
            protected override void CreateChildControls()
            {
    
                // Add a Label control to display content.
                m_textLabel = new Label();
                m_textLabel.Text = Text;
                Controls.Add(m_textLabel);
            }
    
            // The PreRender event is raised after the Web Part is edited,
            // so this is a good time to update the child control.
            void LocalizedWebPart_PreRender(object sender, EventArgs e)
            {
    
                // Make sure that CreateChildControls has been called.
                EnsureChildControls();
    
                // Apply current Web Part settings to the child control.
                m_textLabel.Font.Bold = Bold;
            }
    
    
            // Indicates whether the text is bold.
            // This property is exposed in the Web Part Editor.
            [WebBrowsable]
            [Personalizable(PersonalizationScope.Shared)]
            [Category(CATEGORY_TEXT)]
            [WebDisplayName(TOOLPANE_BOLD)]
            [WebDescription(TOOLPANE_BOLD_DESC)]
            public bool Bold { get; set; }
    
            // Overrides the Description property.
            [WebBrowsable(false), Personalizable(false)]
            public override string Description
            {
                get { return WEB_PART_DESC; }
                set { ; }
            }
    
            // Gets the text to display.
            private string Text
            {
                get { return LABEL_TEXT; }
            }
    
            // Overrides the Title property.
            [WebBrowsable(false), Personalizable(false)]
            public override string Title
            {
                get { return WEB_PART_TITLE; }
                set { ; }
            }
    
            // Gets a collection of custom verbs that provide items
            // on the options menu for the Web Part.
            public override WebPartVerbCollection Verbs
            {
                get
                {
    
                    // Add a custom verb to toggle underlining for the text.
                    WebPartVerb verb = new WebPartVerb(this.ID + "_UnderlineVerb",
                     (sender, args) =>
                     {
                         EnsureChildControls();
                         m_textLabel.Font.Underline = !m_textLabel.Font.Underline;
                     });
    
                    verb.Checked = m_textLabel.Font.Underline;
                    verb.Text = VERB_UNDERLINE;
                    verb.Description = VERB_UNDERLINE_DESC;
    
                    WebPartVerb[] newVerbs = new WebPartVerb[] { verb };
                    return new WebPartVerbCollection(base.Verbs, newVerbs);
                }
            }
        }
    }
    
    Imports System
    Imports System.ComponentModel
    Imports System.Globalization
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports Microsoft.SharePoint.WebControls
    
    <ToolboxItemAttribute(false)> _
    Public Class LocalizedWebPart
        Inherits WebPart
    
        ' Constants for display strings.
        Const CATEGORY_TEXT As String = "Text"
        Const LABEL_TEXT As String = "Hello, world!"
        Const TOOLPANE_BOLD As String = "Bold"
        Const TOOLPANE_BOLD_DESC As String = "Make the text bold"
        Const VERB_UNDERLINE As String = "Underline"
        Const VERB_UNDERLINE_DESC As String = "Draw a line under the text"
        Const WEB_PART_TITLE As String = "Localized Web Part"
        Const WEB_PART_DESC As String = "An example localized Web Part"
    
        ' Member variable for the child control.
        Private m_textLabel As Label
    
        ' Instantiates the Web Part.
        Public Sub New()
            ' Add a handler for the PreRender event.
            AddHandler PreRender, AddressOf LocalizedWebPart_PreRender
        End Sub
    
        ' Creates the control tree.
        Protected Overrides Sub CreateChildControls()
            ' Add a Label control to display content.
            m_textLabel = New Label()
            m_textLabel.Text = Text
            Controls.Add(m_textLabel)
        End Sub
    
        ' The PreRender event is raised after the Web Part is edited,
        ' so this is a good time to update the child control.
        Private Sub LocalizedWebPart_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    
            ' Make sure that CreateChildControls has been called.
            EnsureChildControls()
    
            ' Apply current Web Part settings to the child control.
            m_textLabel.Font.Bold = Bold
        End Sub
    
        Private m_bold As Boolean
    
        ' Indicates whether the text is bold.
        ' This property is exposed in the Web Part Editor.
        <WebBrowsable()> _
        <Personalizable(PersonalizationScope.[Shared])> _
        <Category(CATEGORY_TEXT)> _
        <WebDisplayName(TOOLPANE_BOLD)> _
        <WebDescription(TOOLPANE_BOLD_DESC)> _
        Public Property Bold() As Boolean
            Get
                Return m_bold
            End Get
            Set(ByVal value As Boolean)
                m_bold = value
            End Set
        End Property
    
        ' Overrides the Description property.
        <WebBrowsable(False)> _
        <Personalizable(False)> _
        Public Overrides Property Description As String
            Get
                Return WEB_PART_DESC
            End Get
            Set(ByVal value As String)
            End Set
        End Property
    
        ' Gets the text to display.
        ReadOnly Property Text As String
            Get
                Return LABEL_TEXT
            End Get
        End Property
    
        ' Overrides the Title property.
        <WebBrowsable(False)> _
        <Personalizable(False)> _
        Public Overrides Property Title As String
            Get
                Return WEB_PART_TITLE
            End Get
            Set(ByVal value As String)
            End Set
        End Property
    
        ' Gets a collection of custom verbs that provide items
        ' on the options menu for the Web Part.
        Public Overrides ReadOnly Property Verbs() As WebPartVerbCollection
            Get
    
                ' Add a custom verb to toggle underlining for the text.
                Dim verb As New WebPartVerb(Me.ID + "_UnderlineVerb", AddressOf UnderlineVerbHandler)
    
                verb.Checked = m_textLabel.Font.Underline
                verb.Text = VERB_UNDERLINE
                verb.Description = VERB_UNDERLINE_DESC
    
                Dim newVerbs As WebPartVerb() = New WebPartVerb() {verb}
                Return New WebPartVerbCollection(MyBase.Verbs, newVerbs)
            End Get
        End Property
    
        ' Toggles underlining.
        Private Sub UnderlineVerbHandler(ByVal sender As Object, ByVal args As EventArgs)
            EnsureChildControls()
            m_textLabel.Font.Underline = Not m_textLabel.Font.Underline
        End Sub
    
    End Class
    
  4. Ctrl + Shift + S キーを押すと、すべて保存されます。

Web パーツが作成され動作したら、次はそれをテストします。F5 キーを押すと、ソリューションが構築および展開されます。Web サイトが開いている場合は、新しい [Web パーツ] ページを作成し、そのページに Web パーツを追加します。ページが編集モードの場合は、Web パーツの枠の右上隅にあるオプション メニューを確認します。[下線] を複数回クリックし、ユーザー設定の動詞が機能していることを確認します。次に [Web パーツの編集] をクリックします。ツール ウィンドウが表示されたら、ウィンドウの下部にあるカスタム プロパティを探します。[太字] を選択し、[適用] をクリックして、プロパティが正しく設定されていることを確認します。

言語リソースの追加

Web パーツをローカライズする最初の手順として、言語リソース ファイルをプロジェクトに追加します。このチュートリアルが必要とする言語リソース ファイルは 2 つだけです。1 つは Web サイトの既定の言語用のファイル、もう 1 つは Web サイトでサポートされている第 2 言語用のファイルです。自分の好きな言語を使用できますが、このチュートリアルでは、英語とスペイン語を使用します。

ヒントヒント

開発用 Web サイトでサポートされている言語を確認するには、まず、ブラウザーのホーム ページを開きます。[サイトの操作]、[サイトの設定] の順にクリックします。[サイトの管理] で [言語設定] をクリックします。既定の言語はページの上部に、第 2 言語は既定の言語の下に表示されています。

作成するリソース ファイルは、表示テキストを生成する Web パーツのすべてのコンポーネントがアクセスできる Web サーバーのファイル システムに展開する必要があります。そのコンポーネントの例を次に示します。

ターゲット Web サーバーで言語リソースを準備する方法は複数ありますが、最も簡単で、以降も他の言語をより柔軟にサポートできる方法は、SharePoint Foundation インストール ルートの下の Resources フォルダーに言語リソース ファイルを準備するというものです。Visual Studio がこのフォルダーに使用するトークンは、{SharePointRoot}\Resources です。完全パスは %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\Resources です。SharePoint Foundation では、このフォルダーに組み込み Web パーツが使用する言語リソースがインストールされます。

リソースを {SharePointRoot}\Resources フォルダーに追加するには

  1. ソリューション エクスプローラーで [Wingtip.Webparts] プロジェクト ノードを選択します。[プロジェクト] メニューで [SharePoint のマップされたフォルダーの追加] をクリックします。

  2. [SharePoint のマップされたフォルダーの追加] ダイアログ ボックスで [Resources] を選択し、[OK] をクリックします。

    Resources フォルダーがプロジェクトに追加されます。このフォルダーのプロパティを確認します。DeploymentLocation プロパティが {SharePointRoot}\Resources に設定されています。

  3. ソリューション エクスプローラーで、[Resources] フォルダーを選択します。[プロジェクト] メニューで [新しいアイテムの追加] をクリックします。

  4. [新しい項目の追加] ダイアログ ボックスで、開発言語が C# の場合は [Visual C#] のノードを、Visual Basic で作業している場合は [共通項目] のノードを展開し、[全般] を選択します。テンプレート ウィンドウで [リソース ファイル] を選択します。Wingtip.WebParts.resx ファイルを指定して、[追加] をクリックします。

    リソース ファイルがプロジェクトに追加され、マネージ リソース エディターで開きます。

    ヒントヒント

    グローバルな Resources フォルダーでファイルを準備している場合は必ず、他のアプリケーションで準備されているリソースと名前が競合しないように一意のファイル名を選択することをお勧めします。一つの方法として、会社名をファイル名の先頭に追加します。たとえば、Wingtip Toys 社の場合は、展開するすべてのリソース ファイルの名前の先頭に "Wingtip" を付けることができます。

  5. ソリューション エクスプローラーで、作成したばかりのリソース ファイルを右クリックし、[プログラムから開く] を選択します。[プログラムから開く] ダイアログ ボックスで [XML (テキスト) エディター] を選択し、[OK] をクリックします。

    リソース ファイルがエディターで開きます。

  6. ファイルの下部までスクロールし、</root> タグのすぐ上に空白行を挿入します。次に、以下のマークアップをコピーし、ファイルのカーソル位置に貼り付けます。

      <data name="Bold" xml:space="preserve">
        <value>Bold (invariant)</value>
      </data>
      <data name="BoldDesc" xml:space="preserve">
        <value>Make the text bold (invariant)</value>
      </data>
      <data name="FeatureDesc" xml:space="preserve">
        <value>An example localized Web Part (invariant)</value>
      </data>
      <data name="FeatureTitle" xml:space="preserve">
        <value>Localized Web Part (invariant)</value>
      </data>
      <data name="LabelText" xml:space="preserve">
        <value>Hello, world! (invariant)</value>
      </data>
      <data name="TextCategory" xml:space="preserve">
        <value>Text (invariant)</value>
      </data>
      <data name="Underline" xml:space="preserve">
        <value>Underline (invariant)</value>
      </data>
      <data name="UnderlineDesc" xml:space="preserve">
        <value>Draw a line under the text (invariant)</value>
      </data>
      <data name="WebPartDesc" xml:space="preserve">
        <value>An example localized Web Part (invariant)</value>
      </data>
      <data name="WebPartTitle" xml:space="preserve">
        <value>Localized Web Part (invariant)</value>
      </data>
    
  7. Ctrl + S キーを押してファイルを保存します。次に、[ファイル] メニューで [閉じる] をクリックしてファイルを閉じます。

  8. ソリューション エクスプローラーで Wingtip.WebParts.resx ファイルを選択します。[編集] メニューで [コピー] をクリックします。次に、[貼り付け] をクリックします。

    "Wingtip.WebParts.resx のコピー" という名前のファイルが Resources フォルダーに追加されます。

  9. "Wingtip.WebParts.resx のコピー" という名前のファイルを右クリックし、[名前の変更] をクリックします。ファイルの名前を Wingtip.WebParts.language-COUNTRY/REGION.resx に変更します。ここで、language には Web サイトの既定の言語の 2 文字のコードを、COUNTRY/REGION にはサブカルチャの 2 文字のコードを指定します。

    たとえば、既定の言語が英語 (米国) の場合、リソース ファイル名は Wingtip.WebParts**.en-US**.resx になります。

    言語識別子の詳細については、System.Globalization.CultureInfo クラスを参照してください。

    重要重要

    このチュートリアルの後半で説明するように、リソース ファイル名は、"Wingtip.WebParts" などの基本ファイル名を使用して参照します。SharePoint Foundation は、リソース値を取得するとき、基本ファイル名の後に、現在実行中のスレッドの CurrentUICulture プロパティの値に対応する言語識別子が続くリソース ファイル名を参照します。SharePoint Foundation では、この名前が正確に一致する必要があります。たとえば、現在のスレッドの言語識別子が "pt-BR" の場合、SharePoint Foundation は、"BaseFilename.pt-BR.resx" という名前のリソース ファイルを参照します。"BaseFilename.pt.resx"、"BaseFilename.pt-PT.resx" など、言語コードが同じでもサブカルチャが異なるファイルは無視されます。

    実行中のスレッドの言語識別子と一致するリソース ファイルが見つからなかった場合、SharePoint Foundation は、"フォールバック" リソース ファイルを参照します。最初に参照するのがインバリアント カルチャのリソース ファイル、つまり、ファイル名の後ろに言語識別子がないリソース ファイルです。たとえば、現在のプロジェクトのインバリアント カルチャのリソースファイルは Wingtip.WebParts.resx です。インバリアント カルチャのリソース ファイルが見つからない場合、SharePoint Foundation は既定の言語にフォールバックしようとします。既定の言語のリソース ファイルが見つからない場合は、リソース名が表示されます。

  10. 手順 8. と 9. を繰り返します。ここでは Web サイトで有効になっている第 2 言語の言語識別子が追加されます。

  11. 作成したばかりのカルチャ固有の言語リソース ファイルを両方とも開き、"invariant" という用語をファイルの言語識別子に置き換えます。たとえば、Wingtip.WebParts.en-US.resx の場合、"Bold (invariant)" は "Bold (en-US)" になります。

    このチュートリアルでは、文字列値を翻訳する必要がありません。代わりに、言語識別子を未翻訳の文字列に追加します。Web パーツをテストするとき、この言語識別子が、表示されている言語を示します。

  12. Ctrl + Shift + S キーを押すと、すべて保存されます。

フィーチャーおよび Web パーツ コントロール記述ファイルのローカライズ

次は、2 つの重要な XML ファイルであるフィーチャー要素ファイルと Web パーツ コントロール記述ファイル (英語)内の表示文字列をローカライズします。両方のファイルにタイトルと説明の文字列が含まれ、これを、次のパターンと一致する ASP.NET 表現に置き換えることができます。

$Resources:BaseFileName,ResourceName

ドル記号 ($) はその後ろに表現が続くことを、プレフィックス "Resources" は表現の種類を示しています。コロン (:) の後ろに続くサフィックスは解決する表現の値です。値の最初の部分はリソース ファイルの基本ファイル名で、コンマの後ろに続く部分は、ファイルから取得するローカライズされた文字列の名前です。表現ではスペースを使用できません。

注意

セミコロン (;) で終わるリソース表現が表示される場合があります。この区切り文字は使用できますが、SharePoint Foundation 2010 では必要ありません。

フィーチャーをローカライズするには

  1. ソリューション エクスプローラーで [Features] フォルダーを展開し、[Feature1] をダブルクリックします。

    Feature1.feature がフィーチャー デザイナーで開きます。

  2. フィーチャー デザイナーで [タイトル] ボックスのテキストを削除し、次のリソース表現に置き換えます。

    $Resources:Wingtip.WebParts,FeatureTitle
    
  3. 次の表現を [説明] ボックスに貼り付けます。

    $Resources:Wingtip.WebParts,FeatureDesc
    
  4. Ctrl + Shift + S キーを押すと、すべて保存されます。

Web パーツ コントロールの説明をローカライズするには

  1. ソリューション エクスプローラーで LocalizedWebPart フォルダーを展開し、[LocalizedWebPart.webpart] をダブルクリックします。

    LocalizedWebPart.webpart が XML エディターで開きます。

  2. Title プロパティ ("LocalizedWebPart") の値を削除し、次のリソース表現に置き換えます。

    $Resources:Wingtip.WebParts,WebPartTitle
    
  3. Description プロパティ ("My WebPart") の値を削除し、次のリソース表現に置き換えます。

    $Resources:Wingtip.WebParts,WebPartDesc
    
  4. Ctrl + Shift + S キーを押すと、すべて保存されます。

コード記述による、ローカライズされた文字列の取得

一般的な ASP.NET アプリケーションのコードをローカライズするには、グローバル アセンブリ キャッシュでメイン アセンブリと共に展開されたサテライト アセンブリに言語リソースを埋め込みます。コードでリソース値を取得する必要がある場合は、HttpContext.GetGlobalResourceObject メソッドを呼び出します。

このメソッドは、SharePoint Foundation で実行されているコードから確実に呼び出すことができますが、SharePoint Foundation 開発者は代替のメソッドを使用できます。この代替メソッドでは、言語リソースをサテライト アセンブリに展開する必要がありません。SPUtility クラスには、Web サーバー上の {SharePointRoot}\Resources フォルダーに配置されているリソース ファイルから文字列値を取得できる静的メソッド GetLocalizedString が含まれています。このチュートリアルでは、GetLocalizedString メソッドを使用します。

GetLocalizedString メソッドでは、最初の引数ではリソース表現が、2 つ目の引数ではリソース ファイルの基本ファイル名が使用されます。Web パーツ コードからのメソッドの呼び出しを簡単にするには、GetLocalizedString を独自のデザインのメソッドにラップします。

リソース文字列を取得するメソッドを作成するには

  1. ソリューション エクスプローラーで [Wingtip.Webparts] プロジェクト ノードを選択します。[プロジェクト] メニューで [クラスの追加] をクリックします。

    [新しいアイテムの追加] ダイアログ ボックスが表示されます。

  2. [名前] フィールドに、言語に応じて「Utility.cs」または「Utility.vb」と入力し、[OK] をクリックします。

  3. Ctrl + A キーを押してすべて選択します。次に、以下のコードをコピーして貼り付けて、ファイルの現在のコンテンツを置き換えます。

    using Microsoft.SharePoint.Utilities;
    
    namespace Wingtip.WebParts.LocalizedWebPart
    {
        public static class Utility
        {
    
            // Wraps the SPUtility method of the same name.
            public static string GetLocalizedString(string resourceName, int LCID)
            {
                if (string.IsNullOrEmpty(resourceName))
                    return string.Empty;
    
                // SPUtility.GetLocalized string needs a resource expression as the first argument.
                string resourceExpression = string.Format("$Resources:{0}", resourceName);
    
                string resourceFile = "Wingtip.WebParts";
    
                // Note: If the named resource does not have a value for the specified language, 
                // SPUtility.GetLocalizedString returns the value for the invariant language.
                // If the named resource does not exist, it returns the original expression.
               return SPUtility.GetLocalizedString(resourceExpression, resourceFile, (uint)LCID);
            }
        }
    }
    
    Imports Microsoft.SharePoint.Utilities
    
    Public NotInheritable Class Utility
    
        ' Wraps the SPUtility method of the same name.
        Public Shared Function GetLocalizedString(ByVal resourceName As String, ByVal LCID As Integer) As String
    
            If String.IsNullOrEmpty(resourceName) Then
                Return String.Empty
            End If
    
            Dim resourceFile As String = "Wingtip.WebParts"
    
            ' SPUtility.GetLocalized string needs a resource expression as the first argument.
            Dim resourceExpression As String = String.Format("$Resources:{0}", resourceName)
    
            ' Note: If the named resource does not have a value for the specified language, 
            ' SPUtility.GetLocalizedString returns the value for the invariant language.
            ' If the named resource does not exist, it returns the original expression.
            Return SPUtility.GetLocalizedString(resourceExpression, resourceFile, CUInt(LCID))
    
        End Function
    
    End Class
    
  4. Ctrl + Shift + S キーを押すと、すべて保存されます。

Web パーツ プロパティのローカライズ

次は、表示テキストを返す Web パーツ プロパティのコードをローカライズします。いずれの場合も、ハードコードされた文字列を、ユーティリティ メソッド GetLocalizedString への呼び出しと置き換えて、リソース名と現在のスレッドの言語のロケール ID (LCID) を渡します。現在の言語の LCID を取得するには、静的プロパティ CultureInfo.CurrentUICulture にアクセスします。

Web パーツのプロパティをローカライズするには

  1. ソリューション エクスプローラーで LocalizedWebPart.cs または LocalizedWebPart.vb をダブルクリックし、ソース ファイルを開きます。

  2. ファイル上部の LocalizedWebPart クラスの宣言のすぐ下には、複数の文字列定数の宣言があります。その宣言を削除し、以下のコードに置き換えます。

    // Translate resource keys to string constants.
    const string CATEGORY_TEXT = "TextCategory";
    const string LABEL_TEXT = "LabelText";
    const string TOOLPANE_BOLD = "Bold";
    const string TOOLPANE_BOLD_DESC = "BoldDesc";
    const string VERB_UNDERLINE = "Underline";
    const string VERB_UNDERLINE_DESC = "UnderlineDesc";
    const string WEB_PART_TITLE = "WebPartTitle";
    const string WEB_PART_DESC = "WebPartDesc";
    
    ' Translate resource keys to string constants.
    Const CATEGORY_TEXT As String = "TextCategory"
    Const LABEL_TEXT As String = "LabelText"
    Const TOOLPANE_BOLD As String = "Bold"
    Const TOOLPANE_BOLD_DESC As String = "BoldDesc"
    Const VERB_UNDERLINE As String = "Underline"
    Const VERB_UNDERLINE_DESC As String = "UnderlineDesc"
    Const WEB_PART_TITLE As String = "WebPartTitle"
    Const WEB_PART_DESC As String = "WebPartDesc"
    
  3. Description プロパティに移動します。get アクセサーのコードを次のコードに置き換えます。

    get { return Utility.GetLocalizedString(WEB_PART_DESC, CultureInfo.CurrentUICulture.LCID); }
    
    Get
        Return Utility.GetLocalizedString(WEB_PART_DESC, CultureInfo.CurrentUICulture.LCID)
    End Get
    
  4. Text プロパティに移動します。get アクセサーのコードを次のコードに置き換えます。

    get { return Utility.GetLocalizedString(LABEL_TEXT, CultureInfo.CurrentUICulture.LCID); }
    
    Get
        Return Utility.GetLocalizedString(LABEL_TEXT, CultureInfo.CurrentUICulture.LCID)
    End Get
    
  5. Title プロパティに移動します。get アクセサーのコードを次のコードに置き換えます。

    get { return Utility.GetLocalizedString(WEB_PART_TITLE, CultureInfo.CurrentUICulture.LCID); }
    
    Get
        Return Utility.GetLocalizedString(WEB_PART_TITLE, CultureInfo.CurrentUICulture.LCID)
    End Get
    
  6. Verbs プロパティに移動します。verb.Text プロパティおよび verb.Description プロパティを設定する 2 つのコード行を、次の行に置き換えます。

    verb.Text = Utility.GetLocalizedString(VERB_UNDERLINE, CultureInfo.CurrentUICulture.LCID);
    verb.Description = Utility.GetLocalizedString(VERB_UNDERLINE_DESC, CultureInfo.CurrentUICulture.LCID);
    
    verb.Text = Utility.GetLocalizedString(VERB_UNDERLINE, CultureInfo.CurrentUICulture.LCID)
    verb.Description = Utility.GetLocalizedString(VERB_UNDERLINE_DESC, CultureInfo.CurrentUICulture.LCID)
    
  7. Ctrl + Shift + S キーを押すと、すべて保存されます。

属性のローカライズ

Web パーツのコードは、次のスニペットで示すように、複数の属性で修飾されているカスタム Bold プロパティを定義します。

[WebBrowsable]
[Personalizable(PersonalizationScope.Shared)]
[Category(CATEGORY_TEXT)]
[WebDisplayName(TOOLPANE_BOLD)]
[WebDescription(TOOLPANE_BOLD_DESC)]
public bool Bold { get; set; }
<WebBrowsable()> _
<Personalizable(PersonalizationScope.[Shared])> _
<Category(CATEGORY_TEXT)> _
<WebDisplayName(TOOLPANE_BOLD)> _
<WebDescription(TOOLPANE_BOLD_DESC)> _
Public Property Bold() As Boolean
    Get
        Return m_bold
    End Get
    Set(ByVal value As Boolean)
        m_bold = value
    End Set
End Property

WebBrowsable 属性および Personalizable 属性により、ユーザーが Web パーツを編集するときに、プロパティが編集ユーザー UI に表示されます。3 つの追加属性は、編集 UI に対して表示テキストを提供します。

  • Category

    プロパティのカスタム カテゴリの表示名を提供します。

  • WebDisplayName

    プロパティの表示名を提供します。

  • WebDescription

    プロパティのツールヒントのテキストを提供します。

これらの属性のテキストをローカライズするには、CategoryAttribute クラス、WebDisplayNameAttribute クラス、および WebDescriptionAttribute クラスから派生するカスタム属性クラスを記述する必要があります。

Category、WebDisplayName、および WebDescription attributes 属性をローカライズするには

  1. ソリューション エクスプローラーで LocalizedWebPart.cs または LocalizedWebPart.vb をダブルクリックし、ソース ファイルを開きます。

  2. LocalizedWebPart クラスの下部 (ただし、クラス内) で、次のサブクラスのコードを挿入します。

    public sealed class LocalizedCategoryAttribute : CategoryAttribute
    {
        public LocalizedCategoryAttribute(string category)
            : base(category)
        { }
    
        // Override this method to return values from the webpart's resource file.
        protected override string GetLocalizedString(string value)
        {
            return Utility.GetLocalizedString(value, CultureInfo.CurrentUICulture.LCID);
        }
    }
    
    public sealed class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
    {
        bool m_isLocalized;
    
        public LocalizedWebDisplayNameAttribute(string displayName)
            : base(displayName)
        { }
    
        // Override this property to return values from the webpart's resource file.
        public override string DisplayName
        {
            get
            {
                if (!m_isLocalized)
                {
                    this.DisplayNameValue = Utility.GetLocalizedString(base.DisplayName, CultureInfo.CurrentUICulture.LCID);
                    m_isLocalized = true;
                }
                return base.DisplayName;
            }
        }
    }
    
    public sealed class LocalizedWebDescriptionAttribute : WebDescriptionAttribute
    {
        bool m_isLocalized;
    
        public LocalizedWebDescriptionAttribute(string description)
            : base(description)
        { }
    
        // Override this property to return values from the webpart's resource file.
        public override string Description
        {
            get
            {
                if (!m_isLocalized)
                {
                    this.DescriptionValue = Utility.GetLocalizedString(base.Description, CultureInfo.CurrentUICulture.LCID);
                    m_isLocalized = true;
                }
                return base.Description;
            }
        }
    }
    
    Public NotInheritable Class LocalizedCategoryAttribute
        Inherits CategoryAttribute
        Public Sub New(ByVal category As String)
            MyBase.New(category)
        End Sub
    
        ' Override this method to return values from the webpart's resource file.
        Protected Overrides Function GetLocalizedString(ByVal value As String) As String
            Return Utility.GetLocalizedString(value, CultureInfo.CurrentUICulture.LCID)
        End Function
    End Class
    
    Public NotInheritable Class LocalizedWebDisplayNameAttribute
        Inherits WebDisplayNameAttribute
        Private m_isLocalized As Boolean
    
        Public Sub New(ByVal displayName As String)
            MyBase.New(displayName)
        End Sub
    
        ' Override this property to return values from the webpart's resource file.
        Public Overrides ReadOnly Property DisplayName() As String
            Get
                If Not m_isLocalized Then
                    Me.DisplayNameValue = Utility.GetLocalizedString(MyBase.DisplayName, CultureInfo.CurrentUICulture.LCID)
                    m_isLocalized = True
                End If
                Return MyBase.DisplayName
            End Get
        End Property
    End Class
    
    Public NotInheritable Class LocalizedWebDescriptionAttribute
        Inherits WebDescriptionAttribute
        Private m_isLocalized As Boolean
    
        Public Sub New(ByVal description As String)
            MyBase.New(description)
        End Sub
    
        ' Override this property to return values from the webpart's resource file.
        Public Overrides ReadOnly Property Description() As String
            Get
                If Not m_isLocalized Then
                    Me.DescriptionValue = Utility.GetLocalizedString(MyBase.Description, CultureInfo.CurrentUICulture.LCID)
                    m_isLocalized = True
                End If
                Return MyBase.Description
            End Get
        End Property
    End Class
    
  3. Bold プロパティに移動します。次に、Category、WebDisplayName、および WebDescription 属性を LocalizedCategory、LocalizedWebDisplayName、および LocalizedWebDescription に変更します。

    属性パラメーターは変更しないでください。変更されたコードは次のとおりです。

    [LocalizedCategory(CATEGORY_TEXT)]
    [LocalizedWebDisplayName(TOOLPANE_BOLD)]
    [LocalizedWebDescription(TOOLPANE_BOLD_DESC)]
    
    <LocalizedCategory(CATEGORY_TEXT)> _
    <LocalizedWebDisplayName(TOOLPANE_BOLD)> _
    <LocalizedWebDescription(TOOLPANE_BOLD_DESC)> _
    
  4. Ctrl + Shift + S キーを押すと、すべて保存されます。

Web パーツのテスト

Web パーツをテストし、正しく動作することを確認します。

Web パーツをテストするには

  1. Visual Studio で、F5 キーを押してデバッグを開始します。

    [出力] ウィンドウに構築および展開プロセスがレポートされ、最後に、Web サイトが既定のブラウザーに表示されます。

  2. [サイトの操作]、[サイトの設定] の順にをクリックします。[サイト コレクションの管理] で [サイト コレクションの機能] をクリックします。ローカライズされた Web パーツの機能に、タイトルと説明が既定の言語で表示されることを確認します。

  3. ページ右上隅に表示されている名前をクリックします。[表示言語の選択] をポイントし、Web パーツのローカライズで使用した第 2 言語をクリックします。ローカライズされた Web パーツ機能のタイトルと説明が第 2 言語で表示されることを確認します。

  4. (オプション) 手順 3. を繰り返します。ただし、言語リソース ファイルを展開しなかった言語を選択します。ローカライズされた Web パーツ機能のタイトルと説明がロケールに依存しない言語で表示されることを確認します。

  5. 手順 3. を繰り返します。ただし、言語を Web サイトの既定に戻します。

  6. [サイトの操作]、[サイトの設定] の順にクリックします。[ギャラリー] で [Web パーツ] をクリックし、LocalizedWebPart.webpart をクリックします。タイトル、説明、および例が既定の言語で表示されることを確認します。

  7. 手順 3. と 4. を繰り返します (手順 4. はオプション)。言語が期待どおりに変更されることを確認し、既定の言語の表示に戻します。

  8. [サイトの操作] をクリックし、[その他のオプション] をクリックします。[ページ] をクリックし、[Web パーツ ページ] を選択して、[作成] をクリックします。ページの名前を入力し、[作成] をクリックします。

    新しい Web パーツ ページが表示されます。

  9. [中央列] で [Web パーツの追加] をクリックし、[カテゴリ] で [カスタム] をクリックします。LocalizedWebPart を選択し、[追加] をクリックします。

    注意

    ユーザー インターフェイスのこの領域では、Web パーツのタイトルと例が常に Web サイトの既定の言語で表示されます。第 2 言語に切り替えても、Web パーツによって表示されるテキストには影響しません。

    Web パーツがページに追加されます。

  10. [編集の終了] をクリックします。第 2 言語に切り替えて、Web パーツのタイトルとラベルのテキストが期待どおりに変更されることを確認し、既定の言語に戻します。

  11. ページの一番右にカーソルを移動します。下矢印が表示されたら、その矢印をクリックします。[下線] アクションが既定の言語でメニューに表示されることを確認します。

  12. 第 2 言語に切り替えて、手順 11. を繰り返します。[下線] アクションが第 2 言語で表示されることを確認し、既定の言語の表示に戻します。

  13. ページの一番右にカーソルを移動します。下矢印が表示されたら、その矢印をクリックし、[Web パーツの編集] をクリックします。ツール ウィンドウの下部で、[テキスト] カテゴリを見つけ、名前が既定の言語で表示されていることを確認します。カテゴリを展開し、[太字] チェック ボックスとツールヒントが既定の言語で表示されていることを確認します。

  14. 第 2 言語に切り替えて、手順 13. を繰り返します。表示テキストが第 2 言語に変更されることを確認します。

関連項目

タスク

[チュートリアル] 列、コンテンツ タイプ、およびリストをローカライズする

参照

CultureInfo

GetLocalizedString

WebBrowsableAttribute

PersonalizableAttribute

CategoryAttribute

WebDisplayNameAttribute

WebDescriptionAttribute

概念

多言語ユーザー インターフェイス (MUI) について

その他の技術情報

Make multiple languages available for your site's user interface (英語)

方法: マップされたフォルダーを追加および削除する (英語)