Skip to content

Instantly share code, notes, and snippets.

@shuuryou
Created February 22, 2024 23:53
Show Gist options
  • Select an option

  • Save shuuryou/76a112c980ec028dd94c0ed3c91de323 to your computer and use it in GitHub Desktop.

Select an option

Save shuuryou/76a112c980ec028dd94c0ed3c91de323 to your computer and use it in GitHub Desktop.
Create a TMX file from two TMX files, that has the first file's source language and the second file's destination language.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
/* Create a TMX file from the source language of the first TMX file and the destination language
* of the second TMX file, using an identical language (first's destination/second's source) as
* the "Rosetta Stone".
*
* You have:
* * de-DE to en-US
* * en-US to en-GB
*
* You want:
* * de-DE to en-GB
*/
var first = LoadTranslations(@"C:\first.tmx", "de-DE", "en-US");
var second = LoadTranslations(@"C:\second.tmx", "en-US", "en-GB");
var merged = MergeTranslations(first, second);
WriteTranslationsToTMX(merged, "C:\\merged.tmx", "de-DE", "en-GB");
}
static Dictionary<string, string> LoadTranslations(string filePath, string sourceLang, string targetLang)
{
XDocument doc = XDocument.Load(filePath);
XNamespace ns = "http://www.w3.org/XML/1998/namespace"; // Default namespace for xml:lang
var translations = new Dictionary<string, string>();
foreach (var tu in doc.Descendants("tu"))
{
var sourceText = tu.Elements("tuv").FirstOrDefault(tuv => (string)tuv.Attribute(ns + "lang") == sourceLang)?.Element("seg")?.Value;
var targetText = tu.Elements("tuv").FirstOrDefault(tuv => (string)tuv.Attribute(ns + "lang") == targetLang)?.Element("seg")?.Value;
if (sourceText != null && targetText != null)
translations[sourceText] = targetText;
}
return translations;
}
static Dictionary<string, string> MergeTranslations(Dictionary<string, string> deUs, Dictionary<string, string> usUk)
{
var deUk = new Dictionary<string, string>();
foreach (var deUsPair in deUs)
if (deUsPair.Value != null && usUk.TryGetValue(deUsPair.Value, out var ukTranslation))
deUk[deUsPair.Key] = ukTranslation;
return deUk;
}
static void WriteTranslationsToTMX(Dictionary<string, string> translations, string filePath, string firstLang, string secondLang)
{
XNamespace ns = "http://www.w3.org/XML/1998/namespace"; // Define the XML namespace
var tmx = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("tmx",
new XAttribute("version", "1.4"),
new XElement("header",
new XAttribute("creationtool", "TMXTranslationMerger"),
new XAttribute("creationtoolversion", "1.0"),
new XAttribute("datatype", "PlainText"),
new XAttribute("segtype", "sentence"),
new XAttribute("adminlang", "en"),
new XAttribute("srclang", "DE"),
new XAttribute("o-tmf", "ABC Translation Memory"),
new XAttribute("creationdate", DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ")),
new XAttribute("creationid", "TMXMerger")
),
new XElement("body",
translations.Select(kv =>
new XElement("tu",
new XElement("tuv",
new XAttribute(XNamespace.Xml + "lang", firstLang),
new XElement("seg", kv.Key)
),
new XElement("tuv",
new XAttribute(XNamespace.Xml + "lang", secondLang),
new XElement("seg", kv.Value)
)
)
)
)
)
);
tmx.Save(filePath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment