Skip to content

Instantly share code, notes, and snippets.

@msoutopico
Created June 11, 2023 22:32
Show Gist options
  • Save msoutopico/c063ff5f9d9c489ecda90ae9503f6fd0 to your computer and use it in GitHub Desktop.
Save msoutopico/c063ff5f9d9c489ecda90ae9503f6fd0 to your computer and use it in GitHub Desktop.
xliff2tmx_v2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="urn:oasis:names:tc:xliff:document:1.1"
xmlns:copyright="http://www.w3.org/1999/xhtml">
<copyright:info>
<!-- authors make it -->
<copyright:author name="Manuel Souto Pico" />
<!-- copyright holders buy it from authors and sell it -->
<copyright:holder name="cApStAn sprl" />
<copyright:license type="GPL">
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses.
</copyright:license>
</copyright:info>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- this captures the output folder parameter given in the call -->
<xsl:param name="odir" select="odir" />
<!-- this captures the input folder parameter given in the call -->
<xsl:param name="idir" select="idir" />
<!-- this template iterates through the files in the input -->
<xsl:template name="main">
<xsl:variable name="input-files" select="concat($idir, '?select=*.xlf')" />
<xsl:apply-templates select="collection($input-files)"/>
</xsl:template>
<!-- this template defines the name of the output folder and files -->
<xsl:template match="/">
<!-- <xsl:variable name="output-name" select="replace(
tokenize(document-uri(/), '/')[last()],
'(.+)\.xlf',
'$1_bilingual.xlf'
)"/>
<xsl:result-doment href="output_{$dir}/{$output-name}">-->
<!-- $idir is "ms_xlf_2lang/xx-XX" -->
<xsl:variable name="output-name" select="replace(
concat(document-uri(/), '.tmx'),
tokenize($idir, '/')[1],
$odir
)"/>
<xsl:result-document href="{$output-name}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:template>
<xsl:template match="xliff">
<xsl:element name="tmx">
<xsl:attribute name="version">1.4</xsl:attribute>
<xsl:element name="header">
<xsl:attribute name="creationtool">MemoryLn</xsl:attribute>
<xsl:attribute name="creationtoolversion">0.9</xsl:attribute>
<xsl:attribute name="datatype">unknown</xsl:attribute>
<xsl:attribute name="segtype">sentence</xsl:attribute>
<xsl:attribute name="adminlang"><xsl:text>en</xsl:text></xsl:attribute>
<xsl:attribute name="srclang">
<xsl:value-of select="/xliff/file[1]/@source-language"/>
</xsl:attribute>
<xsl:attribute name="o-tmf">sdlxTM</xsl:attribute>
</xsl:element>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="body">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="trans-unit">
<xsl:element name="tu">
<xsl:attribute name="tuid"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="srclang">
<xsl:value-of select="ancestor::file/@source-language"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="source">
<xsl:element name="tuv">
<xsl:attribute name="xml:lang">
<xsl:value-of select="@xml:lang"/>
</xsl:attribute>
<xsl:element name="seg"><xsl:value-of select="."/></xsl:element>
</xsl:element>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="target">
<xsl:element name="tuv">
<xsl:attribute name="xml:lang">
<xsl:value-of select="@xml:lang"/>
</xsl:attribute>
<xsl:element name="seg"><xsl:value-of select="."/></xsl:element>
</xsl:element>
<xsl:apply-templates/>
</xsl:template>
<!-- delete these elements -->
<!-- xsl:template match="prop-group">
</xsl:template-->
<xsl:template match="text()"></xsl:template>
<xsl:template match="alt-trans"></xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment