Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization approach for XSLT + RESX in ASP.NET

I have an ASP.NET web app where the back end data (XML format) is transformed using XSLT, producing XHTML which is output into the 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 the XHTML markup elements, as well as text labels, which are currently in English. for example:

<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 of using .NET resource files (.resx), but I don't know how the resx string resources would get pulled in to the XSLT when the transformation takes place.

I would prefer not to have locale-specific copies of the XSLT file, since that means a lot of duplicate transformation logic.

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

like image 417
frankadelic Avatar asked Jan 01 '26 16:01

frankadelic


2 Answers

Replace the text in the XSLT files with a placeholder element and then write another localizing transformation that takes the resx file and uses it to replace the 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>
like image 56
ctford Avatar answered Jan 03 '26 07:01

ctford


The XML document may contain either one language with one XML document for each language, or alternatively, a single XML document with all languages. The XML formats in the following example follows Microsoft .NET resource (.resx) files (one file per language) or a single TMX (translation memory exchange) document with all languages. Any format, however, may be used as long as the XPath used to read the text is consistent.

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

like image 27
Doug Domeny Avatar answered Jan 03 '26 06:01

Doug Domeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!