Category Archives: Internationalization

Need to Localize an XSLT

Here is our attempt at the XLST translator, main chunk of the work was by Darren, it’s a console application that applies an xsl (i.e. circle.xsl) to some xml (i.e. number.xml) and writes out the result to a console window.

private const String _filename = "number.xml";
private const String _stylesheet = "circle.xsl";
private const String _culturename = "en";
public Program()
{
    //Set the UI to the specified culture.
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(_culturename);

    //Create the XslTransform and load the stylesheet.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(_stylesheet);

    //Load the XML data file.
    XPathDocument doc = new XPathDocument(_filename);

    //Create an XsltArgumentList.
   XsltArgumentList xslArg = new XsltArgumentList();

   //Add an object to get the resources for the specified language.
   ResourceTranslator resTran = new ResourceTranslator("Resources.Resource");
   xslArg.AddExtensionObject("urn:myResTran", resTran);

   //Add an object to calculate the circumference of the circle.
   Calculate obj = new Calculate();
   xslArg.AddExtensionObject("urn:myObj", obj);

   //Create an XmlTextWriter to output to the console.             
   XmlTextWriter writer = new XmlTextWriter(Console.Out);

   //Transform the file.
   xslt.Transform(doc, xslArg, writer, null);

   writer.Close();
}
//Calculates the circumference of a circle given the radius.
public class Calculate
{
    public static double Circumference(double radius)
    {
        return Math.PI * 2 * radius;
    }
}

The translation is based upon our database resource translator i.e. a class that creates a resource manager based on the default/neutral resource resx, and implements a Find method. This method takes a string that we want to find (specified in the default language i.e. in our case English) which is then effectively used as a key into the resource file(s). The text that is passed into the Find method is first converted into a valid identifier (e.g. spaces removed, slashes removed etc… etc…), before it can be used as a key. Then, if a key exists for the specified culture (French in the case of the test solution) then the appropriate language version of the original passed in text is returned. 

In order to use the resource translator in the xsl, an object of this type is first created and then passed into the xsl using extension objects. The Find method can then be called by referencing the correct extension object, and specifying the piece of text that we would like to be translated.
 
public string Find(string displayText)
{
    if (displayText == null) return null;

    var assem = System.Reflection.Assembly.Load("Resources");
    var rman = new ResourceManager(_baseName, assem);
    var s = rman.GetString(Regex.Replace(displayText, "[^\\w]", string.Empty, RegexOptions.CultureInvariant));
            
    return s jQuery15201710984220262617_1352536393022 displayText;
}

XslResourceTranslator.zip (90.45 kb)

Source can be found on GitHub https://github.com/BryanAvery/XslResourceTranslator