开发者

HTML generator that handles multiple (spoken) languages

开发者 https://www.devze.com 2023-03-13 11:25 出处:网络
I have a need to maintain some static HTML pages in several languages. I\'m looking for a tool that will make this reasonably straightforward to maintain.

I have a need to maintain some static HTML pages in several languages. I'm looking for a tool that will make this reasonably straightforward to maintain.

My ideal tool could take some HTML markup like this:

<div>
  <p>
    <langs>
      <lang lang="en">Welcome!</lang>
      <lang lang="de">Willkommen!</lang>
      <lang lang="it">Benvenuti!</lang>
    </langs>
  </p>
  <p>
    <langs>
      <lang lang="en">Where do you want to go today?</lang>
      <lang lang="de">Wo möchten Sie heute unternehmen?</lang>
    </langs>
  </p>
</div>

And if you ran it with the desired language codes, "de,e开发者_JS百科n" it would produce:

<div>
  <p>Willkommen!</p>
  <p>Wo möchten Sie heute unternehmen?</p>
</div>

But if you ran with, "it,en" it would produce:

<div>
  <p>Benvenuti!</p>
  <p>Where do you want to go today?</p>
</div>

Where the second paragraph falls back to English as there wasn't an available translation in Italian. (The argument "it,en" indicates which languages to use in preference order.)

Anybody know of a tool that would fit the bill? I would consider something a bit more esoteric* like HAML if it allowed something similar to the above.

* By "esoteric" I mean something where the source isn't HTML (or close to HTML) but produces HTML as the output.


You can write an XSLT template that does this.

Edit: Here's an example XSL template:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>

    <xsl:param name="desiredLang" select="'it'"/>
    <xsl:param name="defaultLang" select="'en'"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="langs">
        <xsl:variable name="desiredValue" select="./lang[@lang=$desiredLang]"/>
        <xsl:variable name="defaultValue" select="./lang[@lang=$defaultLang]"/>
        <xsl:choose>
            <xsl:when test="$desiredValue">
                <xsl:value-of select="$desiredValue"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$defaultValue"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

Here's its output:

<div>
    <p>Benvenuti!</p>
    <p>Where do you want to go today?</p>
</div>
0

精彩评论

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