[Qt-interest] Fwd: Translating a xml file using lupdate

Jordan Tcolov mazzzterz at gmail.com
Fri Sep 16 14:45:23 CEST 2011


And what am I suppose to write here ?!


---------- Forwarded message ----------
From: Jordan Tcolov <mazzzterz at gmail.com>
Date: Thu, Sep 15, 2011 at 16:21
Subject: Re: [Qt-interest] Translating a xml file using lupdate
To: Konstantin Tokarev <annulen at yandex.ru>


Thanks for the sugestion. It's the best one and doesn't require to change
the source or xml files in any special way. But instead of XQuery I used
XSLT. I thought that because I had really simple example at hand it will be
faster to do... but it took some time and learning. I made s simple example
for anyone having this issue.

XML file:
<?xml version="1.0" encoding="UTF-8"?>
<buttons_definitions>

    <buttons>
        <button id="0" name="POS">
             <text0>Position&#10;POSITION</text0>

        </button>

        <button id="1" name="PROGRAM">
            <text0>Program</text0>
            <text1>Programme</text1>
        </button>
    </buttons>

    <switch id="0" name="HISTORY">
        <text state="0">History</text>
        <text state="1">History Back</text>
        <text state="2">History Forw</text>
    </switch>

</buttons_definitions>

XSLT file:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" >

<xsl:output method="text" indent="yes"/>


<xsl:template match="/" >


<xsl:text>const char* __buttons__[] = {</xsl:text><xsl:call-template
name="insert-newline"/>
<xsl:for-each select="buttons_definitions/buttons/button">
   <xsl:apply-templates select="text0|text1"/>
   <xsl:choose>
      <xsl:when test="position() != last()">
         <xsl:call-template name="insert-comma-newline"/>
      </xsl:when>
   </xsl:choose>
</xsl:for-each>
<xsl:call-template name="insert-newline"/><xsl:text>};</xsl:text>


<xsl:text>&#10;&#10;&#10;</xsl:text>


<xsl:text>const char* __switches__[] = {</xsl:text><xsl:call-template
name="insert-newline"/>
<xsl:for-each select="buttons_definitions/buttons/switch">
   <xsl:apply-templates select="text"/>
   <xsl:choose>
      <xsl:when test="position() != last()">
         <xsl:call-template name="insert-comma-newline"/>
      </xsl:when>
   </xsl:choose>
</xsl:for-each>
<xsl:call-template name="insert-newline"/><xsl:text>};</xsl:text>

</xsl:template>






<!--
================================================================================
================================================================================
================================== TEMPLATES
===================================
================================================================================
================================================================================
-->



<!--
    This template hangles tags text0, text1, text.
    Usage:
    <xsl:apply-templates select="text0"/>
    <xsl:apply-templates select="text1"/>
    <xsl:apply-templates select="text0|text1"/>
-->
<xsl:template match="text0|text1|text">
   <xsl:variable name="ButtonName">
      <xsl:choose>
         <xsl:when test="contains(., '&lt;') and ((contains(., '&gt;') and
contains(., '&lt;/')) or contains(., '/&gt;'))">
            <xsl:call-template name="string-replace-all">
               <xsl:with-param name="text" select="." />
               <xsl:with-param name="replace" select="'&#10;'" />
               <xsl:with-param name="by" select="'&lt;br&gt;'" />
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="string-replace-all">
               <xsl:with-param name="text" select="." />
               <xsl:with-param name="replace" select="'&#10;'" />
               <xsl:with-param name="by" select="'\n'" />
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>
   <xsl:choose>
      <xsl:when test="string-length($ButtonName) != 0" >
         <xsl:text>    QT_TRANSLATE_NOOP("XML_Buttons_</xsl:text>
         <xsl:value-of select="name()"/>
         <xsl:text>", "</xsl:text>
         <xsl:copy-of select="$ButtonName"/>
         <xsl:text>")</xsl:text>
      </xsl:when>
      <xsl:otherwise>
         <xsl:text>    "</xsl:text>
         <xsl:copy-of select="$ButtonName"/>
         <xsl:text>"</xsl:text>
      </xsl:otherwise>
   </xsl:choose>
   <xsl:if test="position() != last()">
      <xsl:call-template name="insert-comma-newline"/>
   </xsl:if>
</xsl:template>



<!--
    This template is used for replacing in string
    because XSL 1.0 doesn't support repalce function.
    It's supported from version 2.0
    Usage:
    <xsl:call-template name="string-replace-all">
       <xsl:with-param name="text" select="__tag_name__" />
       <xsl:with-param name="replace" select="__find_this__" />
       <xsl:with-param name="by" select="__replace_with__" />
    </xsl:call-template>
-->
<xsl:template name="string-replace-all">
   <xsl:param name="text" />
   <xsl:param name="replace" />
   <xsl:param name="by" />
   <xsl:choose>
      <xsl:when test="contains($text, $replace)">
         <xsl:value-of select="substring-before($text,$replace)" />
         <xsl:value-of select="$by" />
         <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text"
select="substring-after($text,$replace)" />
            <xsl:with-param name="replace" select="$replace" />
            <xsl:with-param name="by" select="$by" />
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="$text" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>



<!--
    Inserts a comma.
    Usage:
    <xsl:call-template name="insert-comma"/>
-->
<xsl:template name="insert-comma">
   <xsl:text>,</xsl:text>
</xsl:template>



<!--
    Inserts newline.
    Usage:
    <xsl:call-template name="insert-newline"/>
-->
<xsl:template name="insert-newline">
   <xsl:text>&#10;</xsl:text>
</xsl:template>



<!--
    inserts comma followed by newline.
    Usage:
    <xsl:call-template name="insert-newline"/>
-->
<xsl:template name="insert-comma-newline">
   <xsl:call-template name="insert-comma"/><xsl:call-template
name="insert-newline"/>
</xsl:template>

</xsl:stylesheet>

That's the example. In it I included a replace template. Hope this helps
someone.



2011/9/13 Konstantin Tokarev <annulen at yandex.ru>

> 13.09.2011, 14:47, "Jordan Tcolov" <mazzzterz at gmail.com>:
> > Just after I posted I found another solution.
> >
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> <buttons_definitions>
> >> <buttons>
> >>
> >> <!-- Two state buttons -->
> >>     <button id="0" name="POS">
> >>         <text0>QT_TRANSLATE_NOOP("XML_Buttons", "Position")</text0>
> >>     </button>
> >>
> >>     <button id="1" name="PROGRAM">
> >>         <text0>QT_TRANSLATE_NOOP("XML_Buttons", "Program")</text0>
> >>     </button>
> >>
> >> </buttons>
> >> </buttons_definitions>
> >
> > It's better than the previous and I just have to remove QT_TRANSLATE_NOOP
> from the string when reading it which isn't that much of work, but I want to
> ask if there is anyway that I can point lupdate that I want to translate
> text between the tags <text0></text0>, <text1></text1>, <text2></text2> and
> so ?
>
> You can borrow solution used in Qt Creator, i.e. generate from your XMLs
> fake source file using XQuery script, and feed it to lupdate.
>
> --
> Regards,
> Konstantin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110916/0b70d35a/attachment.html 


More information about the Qt-interest-old mailing list