Office 365 サンプル アドインのローカリゼーション機能の使用

Core.CreateContentTypes サンプルは、サイト、一覧、コンテンツ タイプ、サイト列にに対する Office 365 のローカリゼーション機能の使用法を示します。 このサンプル コードは、コンソール アプリケーションを使用して以下の操作を実行します。

  • コンテンツ タイプ、サイト列、一覧を作成し、サイト列をコンテンツ タイプに関連付けます。
  • コンテンツ タイプ、サイト列、リスト、ユーザー指定のサイトをローカライズします。

注:

この記事で説明しているローカライズ機能は、Office 365 のみで使用できます。 Office 365 Dedicated またはオンプレミスの SharePoint Server で使用できるローカライズ機能の詳細については、「SharePoint アドインをローカライズする」と「SharePoint ソリューションのローカライズ」を参照してください。

はじめに

まず、Core.CreateContentTypes サンプル アドインを、GitHub 上の Office 365 Developer Patterns and Practices プロジェクトからダウンロードします。

このコード サンプルを実行する前に、以下の手順を完了してください。

サイトで言語設定を構成する

  1. チーム サイトで、[設定]>[サイトの設定] を選択します。

  2. [サイトの管理] で、[言語設定] を選択します。

  3. [言語設定] ページの [第 2 言語] で、サイトでサポートする第 2 言語を選択します。 たとえば、次の図に示すように [フィンランド語][フランス語] を選択できます。

    サイトの言語設定が表示されたスクリーンショット。

  4. [OK] をクリックします。

ユーザーのプロファイル ページで表示言語を設定する

  1. Office 365 サイトの上部にある自分のプロファイル画像を選択して、[自己紹介] を選択します。

    [自己紹介] が選択された状態のユーザー プロファイル ページのスクリーンショット

  2. [自己紹介] ページで、 [プロファイルの編集] を選択します。

  3. 追加オプションの省略記号 (...) を選択してから、[言語と地域] を選択します。

  4. [表示言語][新しい言語の選択] リストを使用して、サイト上で設定したのと同様に新しい言語を選択してから、[追加] を選択します。 たとえば、次の図に示すように [フィンランド語][フランス語] を選択します。 上下の矢印ボタンを選択して、優先言語を上下に移動できます。

    ユーザーのプロファイル ページの [言語と地域] セクションのスクリーンショット

  5. [すべて保存して閉じる] を選択します。

注:

選択した言語でサイトが表示されるまでに数分かかる場合があります。

Core.CreateContentTypes サンプル アプリを実行する

このコード サンプルを実行すると、以下の図に示すコンソール アプリケーションが表示されます。 ローカライズするサイトと、Office 365 管理者の資格情報を入力する必要があります。

Core.CreateContentTypes コンソール アプリケーションのスクリーンショット

このコンソール アプリケーションを実行すると、Program.cs 内の Main 関数により次のタスクが実行されます。

  • CreateContentTypeIfDoesNotExist メソッドを呼び出して、Contoso Document というコンテンツ タイプを作成します。

  • CreateSiteColumn メソッドを呼び出して、Contoso String というサイト列を作成します。

  • AddSiteColumnToContentType メソッドを呼び出して、Contoso String サイト列と Contoso Document コンテンツ タイプをリンクします。

  • CreateCustomList メソッドを呼び出して、MyList という新しい一覧を作成します。

その後、Main メソッドは LocalizeSiteAndList メソッドと LocalizeContentTypeAndField メソッドを呼び出します。

LocalizeSiteAndList メソッドは以下の処理を実行する方法を示します。

  • Web オブジェクトの TitleResource プロパティと DescriptionResource プロパティに対して SetValueForUICulture メソッドを使用して、サイトのタイトルと説明に関するローカライズされたさまざまな値を設定します。

  • Web オブジェクトの TitleResource プロパティと DescriptionResource プロパティに対して SetValueForUICulture メソッドを使用して、サイトのタイトルと説明に関するローカライズされたさまざまな値を設定します。

LocalizeContentTypeAndField メソッドは以下の処理を実行する方法を示します。

  • ContentType オブジェクトの NameResource プロパティと DescriptionResource プロパティに対して SetValueForUICulture メソッドを使用して、コンテンツ タイプの名前と説明に関するローカライズされたさまざまな値を設定します。

  • Field オブジェクトの TitleResource プロパティと DescriptionResource プロパティに対して SetValueForUICulture メソッドを使用して、サイトのタイトルと説明に関するローカライズされたさまざまな値を設定します。

注:

この記事で提供されるコードは、明示または黙示のいかなる種類の保証なしに現状のまま提供されるものであり、特定目的への適合性、商品性、権利侵害の不存在についての暗黙的な保証は一切ありません。

private static void LocalizeSiteAndList(ClientContext cc, Web web)
        {
            // Localize the site title.
            web.TitleResource.SetValueForUICulture("en-US", "Hello World");
            web.TitleResource.SetValueForUICulture("fi-FI", "Hello World - Finnish");
            web.TitleResource.SetValueForUICulture("fr-FR", "Hello World - French");
            // Localize the site description.
            web.DescriptionResource.SetValueForUICulture("en-US", "Hello World site sample");
            web.DescriptionResource.SetValueForUICulture("fi-FI", " Hello World site sample - Finnish");
            web.DescriptionResource.SetValueForUICulture("fr-FR", " Hello World site sample - French");
            web.Update();
            cc.ExecuteQuery();

            // Localize the custom list that was created previously.
            List list = cc.Web.Lists.GetByTitle("MyList");
            cc.Load(list);
            cc.ExecuteQuery();
            // Localize the list title.
            list.TitleResource.SetValueForUICulture("en-US", "Hello World");
            list.TitleResource.SetValueForUICulture("fi-FI", "Hello World - Finnish");
            list.TitleResource.SetValueForUICulture("fr-FR", " Hello World - French");
            // Localize the list description.
            list.DescriptionResource.SetValueForUICulture("en-US", "This example localizes a list using CSOM.");
            list.DescriptionResource.SetValueForUICulture("fi-FI", "This example localizes a list using CSOM - Finnish.");
            list.DescriptionResource.SetValueForUICulture("fr-FR", "This example localizes a list using CSOM - French.");
            list.Update();
            cc.ExecuteQuery();
        }

private static void LocalizeContentTypeAndField(ClientContext cc, Web web)
        {
            ContentTypeCollection contentTypes = web.ContentTypes;
            ContentType myContentType = contentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2");
            cc.Load(contentTypes);
            cc.Load(myContentType);
            cc.ExecuteQuery();
            // Localize content type name.
            myContentType.NameResource.SetValueForUICulture("en-US", "Contoso Document");
            myContentType.NameResource.SetValueForUICulture("fi-FI", "Contoso Document - Finnish");
            myContentType.NameResource.SetValueForUICulture("fr-FR", "Contoso Document - French");
            // Localize content type description.
            myContentType.DescriptionResource.SetValueForUICulture("en-US", "This is the Contoso Document.");
            myContentType.DescriptionResource.SetValueForUICulture("fi-FI", " This is the Contoso Document - Finnish.");
            myContentType.DescriptionResource.SetValueForUICulture("fr-FR", " This is the Contoso Document - French.");
            myContentType.Update(true);
            cc.ExecuteQuery();

            // Localize the site column.
            FieldCollection fields = web.Fields;
            Field fld = fields.GetByInternalNameOrTitle("ContosoString");
            // Localize site column title.
            fld.TitleResource.SetValueForUICulture("en-US", "Contoso String");
            fld.TitleResource.SetValueForUICulture("fi-FI", "Contoso String - Finnish");
            fld.TitleResource.SetValueForUICulture("fr-FR", "Contoso String - French");
            // Localize site column description.
            fld.DescriptionResource.SetValueForUICulture("en-US", "Used to store Contoso specific metadata.");
            fld.DescriptionResource.SetValueForUICulture("fi-FI", "Used to store Contoso specific metadata - Finnish.");
            fld.DescriptionResource.SetValueForUICulture("fr-FR", "Used to store Contoso specific metadata - French.");
            fld.UpdateAndPushChanges(true);
            cc.ExecuteQuery();
        }

以下の図に示すように、カスタムのフランス語の「Hello World - French」というタイトルがサイトに表示されます。これは LocalizeSiteAndList メソッドを使って設定した内容です。

更新されたカスタマイズ ページ タイトルのスクリーンショット

関連項目