- ResourceManagerを作成する
-
初期処理(MainやInitializeComponent)でResourceManagerのインスタンスを作成します。
以下はWorld Wide Naviのサンプルコードです。
WwnaviResource.cs
namespace Wwnavi {
using System;
public class Resource {
public static void Init(){
// Set the user interface to display in the same culture as that set in Control Panel.
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
// (*コントロールパネルの地域と言語の設定を反映させるための重要なコードです。)
// Assembly
wwnaviRs(*1) = new System.Resources.ResourceManager(
"WindowsFormsApplication.(Properties.)wwnavi_string",
(*'Properties' は2005以降のC# プロジェクトのみ必要です)
System.Reflection.Assembly.GetExecutingAssembly());
// File Based
// wwnaviRs = System.Resources.ResourceManager.
// CreateFileBasedResourceManager(
// "WindowsFormsApplication.wwnavi_string",
// "./",null);
}
public static String GetString(String id){
if (wwnaviRs == null) Init(); *3)
return wwnaviRs.GetString(id);
}
public static System.Resources.ResourceManager GetResourceManager(){
return wwnaviRs;
}
private static System.Resources.ResourceManager wwnaviRs = null;
};
}
*1)wwnaviRs ... RsourceManagerのインスタンス
このサンプルは、埋めこみリソースまたはサテライトDLLを読む
'Assembly' RsourceManagerを作成しています。
2番目のコメントアウトされたコードは'FileBased' RsourceManagerを作成しており、
.resourcesファイルを読み込むものです。
static void Main()
{
Wwnavi.Resource.Init(); *2)
...
}
*2)上記*1)のResourceManager作成処理を呼んでいます。
ResourceManagerの作成は初期処理で行うのが望ましいですが必須ではありません。
*3)のコードはリソースデータを返す前にResourceManagerが作成されていない場合、'Init'を呼んでいます。
これはクラスライブラリなどの初期処理を持たないプロジェクトのためです。
フォームアプリケーションの場合は、Main関数内の冒頭で、InitializeComponent()が呼ばれる前にInitを呼ぶ必要があります。
[STAThread]
static void Main()
{
// System.Threading.Thread.CurrentThread.CurrentUICultureの設定を初めに行う必要があります。
Wwnavi.Resource.Init();
VBの場合は、Sub New()の冒頭。
WPF、およびSilverlightの場合は、こちらをごらんください。
- 文字列取得のコードを埋めこむ
-
ResourceManager.GetStringメソッドでresxファイルから文字列情報を取得します。
MessageBox.Show(Wwnavi.Resource.GetString("wwnavi.Msg.Id1")...
WwnaviResource.cs
...
public static String GetString(String id){
...
return wwnaviRs.GetString(id);
}
- 文字列をresxファイルに記述する
-
メッセージIDと共に文字列をresxファイルの以下のエレメントに記述します。
このファイル名はResourceManager作成メソッド
(上記1-*1を参照、"WindowsFormsApplication.(Properties.)wwnavi_string")に対応している必要があり、
このケースでは'wwnavi_string.resx'でなければいけません。
*Visual Studioアドインの実装では、"resources"という名前を使う必要がありまます。
(例: ResourceManager "MyAddin.resources"、 resx ファイル "resources.resx")
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
...
</xsd:schema>
...
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="wwnavi.Msg.Id1" xml:space="preserve"> *1)
<value>Hello, how are you? This is a sample.</value>
</data>
</root>
*1)'data'エレメントに文字列を記述する必要があります。
"wwnavi.Msg.Id1"は'GetString'のメッセージIDに対応しています。
- リソースファイルをコンパイルし配置する
- WPFとSilverlightの場合
WPFスタンドアプリケーションとSilverlightの場合は、UIロケールの設定の仕方が異なります。
-
WPFスタンドアプリケーションの場合:
デフォルトではMain関数が自動生成され、UICultureの設定ができないため、
自分でMain関数用のクラスを記述し、Main関数の自動生成をOFFにします。
1. App.xamlのプロパティの「ビルドアクション」を「Page」に変更。
2.以下のようなMain関数用のクラスを作成し、Wwnavi.Resource.Init()(CurrentUICultureの設定)
を呼び出します。
using System;
using System.Collections.Generic;
namespace Wwnavi{
class TempMain{
[STAThread]
static public void Main(string[] args) {
Wwnavi.Resource.Init(); // CurrentUICultureの設定
YOUR_APP_NAME.App app = new YOUR_APP_NAME.App();
// YOUR_APP_NAME is your application name (e.g. WpfApplication1)
app.InitializeComponent();
app.Run();
}
}
}
3.あとは正常にビルドされればOKです。
*各国語のdllはビルドされたexeからSisulizerを使って、XAMLファイルの変更なしに容易に作成できます。
YOUR_PROJECT_PATH/bin/Debug/WpfApplication1.exe
ja/WpfApplication1.resources.dll (*Sisulizerを使って作成された日本語のdll)
Sisulizerの詳細については、World Wide Naviのヘルプをごらんください。
-
Silverlightの場合:
Silverlightでは、Silverlightのオブジェクト(xap)を埋めこんだHTML(ASP、PHPなど)でユーザーのロケール(言語)を認識し、それをSilverlightのパラメータとして渡し、適切なxapファイルを読み込むのが一般的です。
以下は、Visual Studioのテスト用ASPを修正した、World Wide Naviのサンプルです。
SilverlightApplication1TestPage.aspx
...
<%--
//// These are Silverlight i18n/l10n part. ////
--%>
<script runat="server">
// Get the current locale.
//ユーザーのHTTPリクエストから言語情報を取得します。
string sLang = System.Web.HttpContext.Current.Request.UserLanguages[0];
string sPath = System.Web.HttpContext.Current.Request.UserLanguages[0];
string root = System.Web.HttpContext.Current.Server.MapPath("/ClientBin/");
</script>
<%
// Set Silverlight file (xap) path.
//ユーザーの言語情報に対応するxapファイルのパスを生成します。
// ClientBin/SilverlightApplication1.xap ... デフォルトxap
// ClientBin/ja/SilverlightApplication1.xap ... 日本語xap
// ClientBin/ko/SilverlightApplication1.xap ... 韓国語xap
// *各国語のxapはSisulizerを使って、XAMLファイルの変更なしに容易に作成できます。
// Sisulizerの詳細については、World Wide Naviのヘルプをごらんください。
if (!System.IO.Directory.Exists(root + sPath))
{
if (sPath.Length > 2) sPath = sPath.Substring(0, 2);
}
if (!System.IO.Directory.Exists(root + sPath))
{
sPath = "";
}
if (sPath != "") sPath = sPath + "/";
%>
...
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<%--
Switch xap files to load on the current locale.
These are directly localized files by Sisulizer WITHOUT XAML sources.
--%>
<param name="source" value="ClientBin/<%=sPath %>SilverlightApplication1.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<%--
Set the current locale to the Silverlight code.
--%>
<param name="uiculture" value="<%=sLang %>" />
<param name="culture" value="<%=sLang %>" />
//これら2つのパラメータがSilverlightオブジェクトのカルチャとして引き継がれます。
*デバックには、デバックメニューではなく、Visual Studio内でこのASPを右クリックし、「ブラウザで開く」メニューで確認してください。