Skip to content

Instantly share code, notes, and snippets.

@lambda125
Created September 24, 2015 13:23
Show Gist options
  • Save lambda125/844fe77c9722374a2fb5 to your computer and use it in GitHub Desktop.
Save lambda125/844fe77c9722374a2fb5 to your computer and use it in GitHub Desktop.
Cross-platform Text localisation for mobile apps (RESX + T4 + C# + RESW/XML)
public interface IResourceLoader
{
string GetResource(string resourceKey);
bool IsLocalisedResource(string resourceKey);
}
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ output extension=".resw" #>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!-- This file was autogenerated by <#=System.IO.Path.GetFileName(Host.TemplateFile)#> on <#=DateTime.Now#> -->
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<#
var resources = GetResourceDataValues(Host.TemplateFile);
foreach (var resource in resources)
{
#>
<#=resource.ToString()#>
<#}#>
</root>
<#+
/*IEnumerable<KeyValuePair<string, string>>*/
public static IEnumerable<XElement> GetResourceDataValues(string ttFilePath)
{
var resxFilePath = GetResxFilePath(ttFilePath);
if (string.IsNullOrWhiteSpace(resxFilePath))
{
//return Enumerable.Empty<KeyValuePair<string, string>>();
return Enumerable.Empty<XElement>();
}
/*
new KeyValuePair<string, string>(
x.Attribute("name").Value,
x.Element("value").Value)
*/
var resourceNames =
XElement.Load(resxFilePath)
.Descendants("data")
.Select(x => x)
.ToList();
return resourceNames;
}
private static readonly XNamespace ProjectNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
private static XName CsprojName(string name)
{
return ProjectNamespace + name;
}
private static string GetResxFilePath(string templatePath)
{
var resxFileName = Path.GetFileNameWithoutExtension(templatePath);
var projectFilePath = GetProjectFilePath(templatePath);
var project = XElement.Load(projectFilePath);
var resxPath = (from itemGroup in project.Descendants(CsprojName("ItemGroup"))
let noneElement = itemGroup.Element(CsprojName("None"))
where noneElement != null &&
noneElement.Elements(CsprojName("Link")).Any() &&
Path.GetFileName(noneElement.Attribute("Include").Value) == resxFileName + ".resx"
select noneElement.Attribute("Include").Value)
.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(resxPath))
{
var dir = Path.GetDirectoryName(projectFilePath);
resxPath = Path.Combine(Path.GetDirectoryName(projectFilePath), resxPath);
}
return resxPath;
}
private static string GetProjectFilePath(string templatePath)
{
var templateDir = new DirectoryInfo(Path.GetDirectoryName(templatePath));
string searchPattern = "*.csproj";
FileInfo[] matches = templateDir.GetFiles(searchPattern);
while(matches.Length == 0)
{
templateDir = templateDir.Parent;
if(templateDir == null) break;
matches = templateDir.GetFiles(searchPattern);
}
var match = matches.FirstOrDefault();
return match == null ? null : match.FullName;
}
#>
namespace Money.Resources
{
using System;
using Money.Common.Resources;
//A layer of abstraction to keep the loader seperate from a class that is accessible as a singleton.
//The class name matches the tt file name
public class Strings
{
private static Strings _instance;
private Strings(IResourceLoader resourceLoader)
{
ResourceLoader = resourceLoader;
}
public static void CreateInstance(IResourceLoader resourceLoader)
{
if (resourceLoader == null)
throw new ArgumentNullException("resourceLoader");
if (_instance != null)
throw new InvalidOperationException("A 'Strings' instance has already been created.");
_instance = new Strings(resourceLoader);
}
public static Strings Instance
{
get { return _instance; }
}
private Money.Common.Resources.IResourceLoader ResourceLoader
{
get; set;
}
public string Get(string resourceKey)
{
return ResourceLoader.GetResource(resourceKey);
}
}
public static class AppTileLabels
{
private static Strings _Strings
{
get { return Strings.Instance; }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_SquareTile1_ExpensesText
///</summary>
public static string SquareTile1_ExpensesText
{
get { return _Strings.Get("AppTileLabels_SquareTile1_ExpensesText"); }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_SquareTile1_SpentMonthText
///</summary>
public static string SquareTile1_SpentMonthText
{
get { return _Strings.Get("AppTileLabels_SquareTile1_SpentMonthText"); }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_SquareTile1_SpentText
///</summary>
public static string SquareTile1_SpentText
{
get { return _Strings.Get("AppTileLabels_SquareTile1_SpentText"); }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_SquareTile1_SpentWeekText
///</summary>
public static string SquareTile1_SpentWeekText
{
get { return _Strings.Get("AppTileLabels_SquareTile1_SpentWeekText"); }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_WideTile1_RecentTransactionsHeading
///</summary>
public static string WideTile1_RecentTransactionsHeading
{
get { return _Strings.Get("AppTileLabels_WideTile1_RecentTransactionsHeading"); }
}
///<summary>
/// Gets the localised value for the resource AppTileLabels_NumberOfItems
///</summary>
public static string NumberOfItems
{
get { return _Strings.Get("AppTileLabels_NumberOfItems"); }
}
}
public static class ButtonLabels
{
private static Strings _Strings
{
get { return Strings.Instance; }
}
///<summary>
/// Gets the localised value for the resource ButtonLabels_Add
///</summary>
public static string Add
{
get { return _Strings.Get("ButtonLabels_Add"); }
}
///<summary>
/// Gets the localised value for the resource ButtonLabels_Clear
///</summary>
public static string Clear
{
get { return _Strings.Get("ButtonLabels_Clear"); }
}
///<summary>
/// Gets the localised value for the resource ButtonLabels_ClearAll
///</summary>
public static string ClearAll
{
get { return _Strings.Get("ButtonLabels_ClearAll"); }
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AppTileLabels_SquareTile1_ExpensesText" xml:space="preserve">
<value>Expenses</value>
</data>
<data name="AppTileLabels_SquareTile1_SpentMonthText" xml:space="preserve">
<value>this month</value>
</data>
<data name="AppTileLabels_SquareTile1_SpentText" xml:space="preserve">
<value>spent</value>
</data>
<data name="AppTileLabels_SquareTile1_SpentWeekText" xml:space="preserve">
<value>this week</value>
</data>
<data name="AppTileLabels_WideTile1_RecentTransactionsHeading" xml:space="preserve">
<value>Recent transactions</value>
</data>
<data name="ButtonLabels_Add" xml:space="preserve">
<value>add</value>
</data>
<data name="ButtonLabels_Clear" xml:space="preserve">
<value>clear</value>
</data>
<data name="ButtonLabels_ClearAll" xml:space="preserve">
<value>clear all</value>
</data>
<!-- more UI text strings here -->
</root>
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ output extension=".generated.cs" #>
<#
var resxFileName = Host.TemplateFile.Replace(".tt", ".resx");
IEnumerable<string> resourceNames = XElement.Load(resxFileName).Descendants("data").Select(x => x.Attribute("name").Value);
var resourcesByClass =
resourceNames.GroupBy(k => k.Contains("_") ? k.Substring(0, k.IndexOf("_")) : Path.GetFileNameWithoutExtension(resxFileName))
.ToDictionary(@group => @group.Key, groupValue => groupValue);
var mainClassName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
#>
namespace <#= CallContext.LogicalGetData("NamespaceHint") #>
{
using System;
using Money.Common.Resources;
//A layer of abstraction to keep the loader seperate from a class that is accessible as a singleton.
//The class name matches the tt file name
public class <#=mainClassName#>
{
private static <#=mainClassName#> _instance;
private <#=mainClassName#>(IResourceLoader resourceLoader)
{
ResourceLoader = resourceLoader;
}
public static void CreateInstance(IResourceLoader resourceLoader)
{
if (resourceLoader == null)
throw new ArgumentNullException("resourceLoader");
if (_instance != null)
throw new InvalidOperationException("A '<#=mainClassName#>' instance has already been created.");
_instance = new <#=mainClassName#>(resourceLoader);
}
public static <#=mainClassName#> Instance
{
get { return _instance; }
}
private Money.Common.Resources.IResourceLoader ResourceLoader
{
get; set;
}
public string Get(string resourceKey)
{
return ResourceLoader.GetResource(resourceKey);
}
}
<#
foreach (var resourceKey in resourcesByClass.Keys)
{
#>
public static class <#=resourceKey#>
{
private static <#=mainClassName#> _<#=mainClassName#>
{
get { return <#=mainClassName#>.Instance; }
}
<#
var resourcesForThisClass = resourcesByClass[resourceKey];
foreach (var resource in resourcesForThisClass)
{
var propertyName = resource.Substring(resource.IndexOf("_") + 1);
#>
///<summary>
/// Gets the localised value for the resource <#=resource#>
///</summary>
public static string <#=propertyName#>
{
get { return _<#=mainClassName#>.Get("<#=resource#>"); }
}
<#
}
#>
}
<#}#>
}
//This is how the whole thing could be used in ViewModel code...in a PCL:
var something = new SomethingViewModel
{
Title = Resources.Labels.UpcomingTransactions, //Resources.Labels is part of the generated code
IsVisible = true,
IsBusy = true,
//... other properties here
};
using System.Diagnostics;
using Windows.ApplicationModel.Resources.Core;
using Money.Common.Resources;
namespace Money.Resources
{
public class ResourceLoader : IResourceLoader
{
private readonly Windows.ApplicationModel.Resources.ResourceLoader _resourceLoader;
public ResourceLoader(string resourceFile)
{
_resourceLoader = new Windows.ApplicationModel.Resources.ResourceLoader(resourceFile);
}
public string GetResource(string resourceKey)
{
return _resourceLoader.GetString(resourceKey);
}
public bool IsLocalisedResource(string resourceKey)
{
var value = _resourceLoader.GetString(resourceKey);
return !string.IsNullOrWhiteSpace(value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment