开发者

Adding localization to XSLT

I'm using an XSL file to do the rendering of an ASP.NET webpart that I made, but now I want to localize the strings inside that XSL file:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:siteInfo="urn:siteInfo" version="1.0">
  <xsl:output method="html" omit-xml-declaration="no"/>
  <xsl:template match="*|/">
    <p>
     Project name: <xsl:value-of select="siteInfo:GetProjectName()"/>
    </p>

In the above example I want to localize "Project name". The language in which I'm working is C# and this is the snippet I use for transforming:

XslCompiledTransform transfor开发者_StackOverflow社区mationEngine = new XslCompiledTransform();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultNetworkCredentials;
transformationEngine.Load(XslUrl, new XsltSettings(true, true), resolver);
XsltArgumentList args = new XsltArgumentList();
args.AddExtensionObject("urn:siteInfo", siteInfo);
transformationEngine.Transform(xmlDocument, args, ms);

Is it possible to do localization through resource (resx) files in that XSL file? In the rest of my project I'm using the following line to insert localized strings:

HttpContext.GetGlobalResourceObject(resourceName, resourceKey).ToString();

Kind regards,

Jeroen


XSLT is incognizant of "resource files".

It is possible to use one or more XML files that contain the necessary localization and to access them during transformation using the document() function and the key() function to "do the translation".

I have many times answered similar question and given examples, but can't find them immediately now. Thus here is another example: http://www.dpawson.co.uk/xsl/sect2/N4852.html#d6180e1251


XSL:

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:siteInfo="urn:siteInfo"
               version="1.0">
  <xsl:output method="html" omit-xml-declaration="no"/>
  <xsl:template match="*|/">
    <style type="text/css">
      .siteInfoHeader {
       font-weight: bold;
       font-size: 10pt;
       margin-bottom: 6pt;
      }
      #siteInfo p {
       margin-top: 0;
       margin-bottom: 6pt; 
      }
    </style>
    <div id="siteInfo">
      <h3 class="siteInfoHeader">$Resources:GWOSXmlWebPartResources,Objectives;:</h3>
      <p>
        <xsl:value-of select="siteInfo:GetObjectives()"/>
      </p>
      <h3 class="siteInfoHeader">$Resources:GWOSXmlWebPartResources,Audience;:</h3>

The resources have been done through the standard SharePoint way to handle resources in XML ($Resources:ResourceFile,Key;). Before performing the XSL transformation there is the possibility to invoke a standard SharePoint function GetLocalizedXmlDocument of the object Microsoft.SharePoint.SPXmlDocCache through reflection, this will hand back an XmlDocument object which has been parsed through the "localization filter", using the standard resources inside the 12-hive/resources. Thanks to Tom Nys for helping me out on this.


Text in an XSLT may be localized by reading the translated strings from an XML document. Numbers may also be localized.

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.

Both options use the XPath 'document' function to read the XML with the translated strings. Define parameters for each string used in the XSLT. Using parameters rather than variables allows the values to be overridden when the XSLT is transformed. Use xsl:value-of to display the translated text. When the transform is processed, pass the language code, for example, 'fr', and the URL to the resource XML document for the desired language.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜