![]() |
![]() ![]() ![]() |
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のインスタンス
static void Main()
{
Wwnavi.Resource.Init(); *2)
...
}
*2)上記*1)のResourceManager作成処理を呼んでいます。[STAThread] static void Main() { // System.Threading.Thread.CurrentThread.CurrentUICultureの設定を初めに行う必要があります。 Wwnavi.Resource.Init(); VBの場合は、Sub New()の冒頭。
MessageBox.Show(Wwnavi.Resource.GetString("wwnavi.Msg.Id1")... WwnaviResource.cs ... public static String GetString(String id){ ... return wwnaviRs.GetString(id); }
<?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'エレメントに文字列を記述する必要があります。
1. コンポーネントプロパティをセットする フォームデザイナビューで以下のプロパティをセットします。 Form.Localizable = true Form.Language = Default (Label.AutoSize = true) 2. resxファイルをプロジェクトに追加する resxファイル(例、上記の'wwnavi_string.resx')をプロジェクトに追加します。 VC++プロジェクトを使っている場合プロジェクトの直下に追加し、 2005以降のC#プロジェクトの場合は、ドラッグアンドドロップで'Properties'ディレクトリの 下に追加します。 (World Wide Naviはこれらの手順を自動的に行います) project- Form1.h (or .cs) <- *Localizableをtrueにセット Form1.resx <- *Visual Studioにより作成 wwnavi_string.resx or Properties/wwnavi_string.resx (C#のみ) 3. ソリューションをリビルドする ソリューションをリビルドし実行ファイルを生成します。 これらの手順では、リソース(Visual Studioのリソース'Form1.resx'と自分自身のリソース 'wwnavi_string.resx')は実行ファイルにデフォルトのものとして埋めこまれるため、 DLLは作成されません。 Debug/Release- WindowsFormApplication.exe <- *Form1.resxとwwnavi_string.resx のリソースを含む 4. 他の言語リソースを追加する 他の言語リソースをプロジェクトに追加した場合Visual Studioは自動的にサテライトDLLを 作成します。 例えば、Form1.resxとwwnavi_string.resxを日本語に翻訳されたリソースにコピーし (名前は'*.ja.resx'となります)、それらをプロジェクトに追加しリビルドすると、 自動的に日本語リソースのDLLが作成されます。 project- Form1.h (or .cs) <- *Localizableをtrueにセット Form1.resx <- *Visual Studioにより作成 Form1.ja.resx <- *コピーし翻訳されたもの *3) wwnavi_string.resx wwnavi_string.ja.resx <- *コピーし翻訳されたもの Properties/wwnavi_string.resx (C#のみ) /wwnavi_string.ja.resx (C#のみ) *3)Form1.ja.resxは、フォームデザイナビューでForm.Languageを'ja'にすることで Visual Studioで自動的に作成することもできます。 Debug/Release- WindowsFormApplication.exe <- *Form1.resxとwwnavi_string.resx のリソースを含む ja/WindowsFormApplication.resources.dll <- *Form1.ja.resxとwwnavi_string.ja.resx のリソースを含む === 注意 === これらの手順は'Assembly' ResourceManagerを使った場合のみ有効です。 'FileBased'のマネージャを作る場合は、.resourcesファイルを実行ディレクトリに 手動でコピーし、常にそれらを実行ファイルに添付する必要があります。
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のヘルプをごらんください。
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を右クリックし、「ブラウザで開く」メニューで確認してください。