Localization for XSLT + RESX in ASP.NET

I have an ASP.NET web application where trailing leaf data (XML format) is transformed using XSLT, creating XHTML that is rendered to a page.

Simplified code:

XmlDocument xmlDoc = MyRepository.RetrieveXmlData(keyValue);
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(pathToXsl, XsltSettings.TrustedXslt, null);
StringWriter stringWriter = new StringWriter();
xsl.Transform(xmlDoc, null, stringWriter);
myLiteral.Text = stringWriter.ToString();

      

Currently, my XSL file contains XHTML markup elements as well as text labels, which are currently in English. eg:

<p>Title:<br />
  <xsl:value-of select="title"/>
</p>
<p>Description:<br />
  <xsl:value-of select="desc"/>
</p>

      

I would like the text labels (title and description above) to be localized. I was thinking about using .NET resource files (.resx), but I don't know how the resx string resources will be pulled into the XSLT when the transformation happens.

I would prefer not to have language copies of the XSLT file, as that means a lot of repetitive transformation logic.

(NOTE: the XML data is already localized, so I don't need to change that)

+2


source to share


3 answers


Replace the text in XSLT files with a placeholder element, and then write another localization transform that takes a resx file and uses it to replace placeholders with the desired piece of text.



<p><localized name="title"/>:<br />
  <xsl:value-of select="title"/>
</p>
<p><localized name="desc"/>:<br />
  <xsl:value-of select="desc"/>
</p>

      

+1


source


An XML document can contain either one language with one XML document for each language, or, alternatively, one XML document with all languages. The XML formats in the following example correspond to Microsoft .NET resource files (.resx) (one file per language) or one TMX (Translation Memory exchange) document with all languages. However, any format can be used as long as XPath is used to read the text.



See my article " How to Localize XSLT " for a complete functional example at http://www.codeproject.com/Articles/338731/LocalizeXSLT .

+1


source


Since the .Resx file is an XML file, you can use it as another source for XSLT using the document function .

0


source







All Articles