2015年2月1日日曜日

TextTemplate で C# のリソースを読み込む

C# のリソース文字列周り。デフォルトの Properties.Resources ではなく、独自の方法で取得する必要ができたのであります。
個々の文字を取得して返す部分はどうにかなるものの、大量のリソースを逐次やるのは困るので、T4で自動生成できないかと探したら、以下のサンプルを見つけました。
T4 Template でお手軽ローカリゼーション
http://www.xamlplayground.org/post/2010/11/25/Simplify-localization-with-a-T4-template.aspx

こちらを真似て、コード生成なしのresource ファイル用にしたものが以下の .tt コードになります。
実際に使う場合は、同名のResxファイル+TextTemplateファイルを用意、TextTemplate ファイルの内容へ以下のコードをコピぺ。
// MainWindow.tt
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" encoding="utf-8" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------
 
<#
string appName = ".resources Generator Template";
string version = "1.0.0.0";
string ns = (string)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");
string resxFileName = Path.ChangeExtension(Host.TemplateFile, ".resx");
string resxClassName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
string proxyClassName = string.Format("{0}ResourceProxy", resxClassName);
XDocument document = XDocument.Parse(File.ReadAllText(resxFileName));
#>
namespace <#=ns#>
{
    using System.Globalization;
    using System.Windows.Markup;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
 
    /// 
    /// Represent a proxy class for "<#= resxClassName #>" resources
    /// 
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
    public class <#= proxyClassName #> : ResourceProxyBase
    {
        private global::System.ComponentModel.ComponentResourceManager _resource;
        /// 
        /// Initializes the "<#= proxyClassName #>" class
        /// 
        public <#= proxyClassName #>()
        {
           var asm = System.Reflection.Assembly.GetExecutingAssembly();

           _resource = new System.ComponentModel.ComponentResourceManager(typeof(<#= resxClassName #>));
        }
    
<# foreach(var item in document.Element("root").Elements("data")) 
   { 
        string name = EscapeName(item);
    
        if (item.Attributes("type").Count() == 0)
        {
#>
 
        /// 
<# if (item.Elements("comment").Count() == 1) { #>
        /// <#= item.Element("comment").Value #>
<# } #>
        /// Gets the "<#= name #>" Property
        /// 
        [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
        public string <#= name #> 
        { 
           get{return _resource.GetString("<#= name #>", ResourceCulture);}
        }
<# 
        }
}
#>
    }
}<#+
public string EscapeName(XElement item)
{
    string name = item.Attribute("name").Value;
    return Regex.Replace(name, "[^a-zA-Z0-9_]{1,1}", "_");
}
#>
これに食べさせるための resx ファイルは下図のような感じ。
食べさせた結果以下のようなファイルが作られる。
 // MainWindow.cs
//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------
 
namespace Schwarzer
{
    using System.Globalization;
    using System.Windows.Markup;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
 
    /// 
    /// Represent a proxy class for "MainWindow" resources
    /// 
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCode(".resources Generator Template", "1.0.0.0")]
    public class MainWindowResourceProxy : ResourceProxyBase
    {
        private global::System.ComponentModel.ComponentResourceManager _resource;
        /// 
        /// Initializes the "MainWindowResourceProxy" class
        /// 
        public MainWindowResourceProxy()
        {
           var asm = System.Reflection.Assembly.GetExecutingAssembly();

           _resource = new System.ComponentModel.ComponentResourceManager(typeof(MainWindow));
        }
    
 
        /// 
        /// Gets the "Text1" Property
        /// 
        [System.CodeDom.Compiler.GeneratedCode(".resources Generator Template", "1.0.0.0")]
        public string Text1 
        { 
           get{return _resource.GetString("Text1", ResourceCulture);}
        }
 
        /// 
        /// Gets the "Text2" Property
        /// 
        [System.CodeDom.Compiler.GeneratedCode(".resources Generator Template", "1.0.0.0")]
        public string Text2 
        { 
           get{return _resource.GetString("Text2", ResourceCulture);}
        }
 
        /// 
        /// Gets the "Text3" Property
        /// 
        [System.CodeDom.Compiler.GeneratedCode(".resources Generator Template", "1.0.0.0")]
        public string Text3 
        { 
           get{return _resource.GetString("Text3", ResourceCulture);}
        }
    }
}
黙って Resx のpublic/internal クラス生成を使えと言われればそれまでなのだけど。
これを WPF に表示したければ以下のような感じで。
<Window x:Class="Schwarzer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Schwarzer"
        Height="150" Width="200">
    <Window.Resources>
        <!-- 先ほど自動生成したクラス -->
        <local:MainWindowResourceProxy x:Key="ResourceProxy"/>
    </Window.Resources>
    <Window.Title>
        <Binding Mode="OneWay" Path="Resource.Text3" Source="{StaticResource LangResoruce}"/>
    </Window.Title>

    <StackPanel>
        <Button Content="{Binding Text1, Mode=OneWay, Source={StaticResource ResourceProxy}}" />
        <Button Content="{Binding Text2, Mode=OneWay, Source={StaticResource ResourceProxy}}" />
    </StackPanel>
</Window>
上記を書いたソリューションはこちら。