frankadelic
frankadelic

Reputation: 20803

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)

Upvotes: 3

Views: 3670

Answers (3)

Doug Domeny
Doug Domeny

Reputation: 4470

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.

Upvotes: 1

pauloya
pauloya

Reputation: 2563

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

Upvotes: 0

ctford
ctford

Reputation: 7297

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>

Upvotes: 1

Related Questions