Translate Mustache templates using XML tag-handling
mainMustache templates often contain embedded tags (e.g., {{name}}) and HTML tags (e.g., <b>) that should not be translated. This example demonstrates a pattern for translating these templates by using DeepL's XML tag-handling capability.
To prevent the translation engine from altering the Mustache syntax, the process follows these steps:
- Parse the template to separate literal text from Mustache tags.
- Replace Mustache tags with unique placeholder XML tags (e.g.,
<m id=0 />). - Translate the resulting XML string via the DeepL API, which preserves the XML structure.
- Parse the translated XML and swap the placeholder tags back for the original Mustache tags.
This ensures that Hello {{name}}. You have just won <b>{{value}} dollars</b>! becomes Hallo {{name}}. Sie haben gerade <b>{{value}} Dollar</b> gewonnen! in German.