<?xml version="1.0" encoding="UTF-8"?>
<!-- By Vassilii Khachaturov <vassilii@tarunz.org>.  -->
<!-- $Id: gramps-translate.xsl,v 1.5 2012-04-06 12:59:03 vassilii Exp $ -->

<xsl:stylesheet version="1.0" 
xmlns:g="http://gramps-project.org/xml/1.4.0/"
xmlns:d="http://tarunz.org/xml/dict/0.1.0/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8"
doctype-public="-//Gramps//DTD Gramps XML 1.4.0//EN"
doctype-system="http://gramps-project.org/xml/1.4.0/grampsxml.dtd"/>

<xsl:param name="living" select="'[CENSORED]'"/>
<xsl:param name="dictxml" select="'t.xml'"/>
<xsl:param name="lang-from" select="'ru'"/>
<xsl:param name="lang-to" select="'en'"/>
<xsl:param name="name-delims" select="'.-()?/, '"/>
<xsl:param name="log-missing" select="0"/>

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

<!-- translate all name text fields and place titles -->
<xsl:template match="//g:name/*/text()|//g:ptitle/text()">
  <xsl:call-template name="translate">
    <xsl:with-param name="text" select="."/>
  </xsl:call-template>
</xsl:template>

<!-- translate some attribute values: 
all in the location element, and of name prefix attribute.
-->
<xsl:template match="//g:location/@*|//g:last/@prefix">
  <xsl:attribute name="{name(.)}">
	  <xsl:call-template name="translate">
		<xsl:with-param name="text" select="."/>
	  </xsl:call-template>
  </xsl:attribute>
</xsl:template>

<!-- Suppress patronymic altogether until http://www.gramps-project.org/bugs/view.php?id=4538 is fixed
Could grok the family relation ourselves from the GRAMPS XML and do proper censorship on children
of living people only.
-->
<xsl:template match="//g:patronymic"/>

<!-- Censor all name fields if the first name contained "[", i.e., the living marker start
 -->
<xsl:template match="//g:name[g:first[contains(., '[')]]/*/text()">
  <xsl:value-of select="$living"/>
</xsl:template>

<!-- ... but don't censor the last name -->
<xsl:template match="//g:name/g:last/text()">
  <xsl:call-template name="translate">
    <xsl:with-param name="text" select="."/>
  </xsl:call-template>
</xsl:template>

<!-- Separate text into delimited stems to translate using the dictionary lookup.
-->
<xsl:template name="translate">
  <xsl:param name="text" />
  <xsl:param name="delims" select="$name-delims"/>
  <xsl:choose>
		<xsl:when test="not($delims)">
			<xsl:call-template name="translate-stem">
				<xsl:with-param name="text" select="$text"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="delim" select="substring($delims,1,1)"/>
			<xsl:choose>
				<xsl:when test="contains($text,$delim)">
					<xsl:call-template name="translate">
						<xsl:with-param name="text" select="substring-before($text,$delim)"/>
						<xsl:with-param name="delims" select="$delims"/>
					</xsl:call-template>
					<xsl:value-of select="$delim"/>
					<xsl:call-template name="translate">
						<xsl:with-param name="text" select="substring-after($text,$delim)"/>
						<xsl:with-param name="delims" select="$delims"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="translate">
						<xsl:with-param name="text" select="$text"/>
						<xsl:with-param name="delims" select="substring($delims,2)"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- Translate a given stem (already w/o delimiters to be afraid of) using dictionary lookup.
Based on replace-acronyms from http://www.dpawson.co.uk/xsl/sect2/replace.html  -->
<xsl:template name="translate-stem">
  <xsl:param name="words"
    select="document($dictxml)/d:dict/d:w" />
  <xsl:param name="text" />
  <xsl:choose>
    <xsl:when test="not($words)">
	  <!-- The stem tagged with lang-from never found (under any word).
	  This can happen either if the dictionary needs to be amended to include it,
	  or because the stem in the first place doesn't belong to the language to be translated.
	  -->
      <xsl:value-of select="$text" />
	  <xsl:if test="$log-missing != 0">
		<xsl:message>
		  <xsl:value-of select="$text" />
		</xsl:message>
	  </xsl:if>
    </xsl:when>
    <xsl:when test="not(string($text))" />
    <xsl:otherwise>
      <xsl:variable name="stemnode" select="$words[1]/d:s[@lang=$lang-from]" />
      <xsl:variable name="stem" select="$stemnode/text()" />
      <xsl:choose>
        <xsl:when test="$text=$stem">
		  <!-- 
		  This will work only if a stem with the target language code is present:
		  <xsl:value-of select="$stemnode/../d:s[@lang=$lang-to]" />

		  Better target language selection (below)
		  (TBD: externalize, parametrize with d:w (-?) node and lang preference list)
		  1) first, as now, try lang-to
		  2) then, try any language not equal to lang-from
			  (TBD: given a preference list, rather than in the XML order of the translations in the dict)
		  3) finally, fall back to lang-from (TBD: and log - what's the point of 
			a single language entry under a word?)
		  <xsl:value-of select="$stemnode/../d:s[@lang=$lang-to]" />
		  -->
		  <xsl:choose>
			  <xsl:when test="$stemnode/../d:s[@lang=$lang-to]">
				  <xsl:value-of select="$stemnode/../d:s[@lang=$lang-to]" />
			  </xsl:when>
			  <xsl:when test="$stemnode/../d:s[@lang!=$lang-from]">
				  <xsl:value-of select="$stemnode/../d:s[@lang!=$lang-from]" />
			  </xsl:when>
			  <xsl:otherwise>
				  <xsl:value-of select="$stem" />
			  </xsl:otherwise>
		  </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="translate-stem">
            <xsl:with-param name="text" select="$text" />
            <xsl:with-param name="words" select="$words[position()>1]" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>
