Some words should never be translated: product names, brand names, model numbers, internal terminology. A glossary can map a term to itself, but you need a dictionary entry for every language pair, which gets cumbersome when you have thousands of terms and many target languages.
This example shows a language-independent alternative. You keep a single do-not-translate list, and the DeepL API leaves those terms unchanged in every target language.
- The plain text is escaped so it can be sent as HTML.
- Every protected term is wrapped in
<span translate="no">…</span>. - The text is translated with
tag_handling="html". DeepL leaves the contents oftranslate="no"elements untouched (docs). - The spans are removed, and you get plain text back.
Input: Sign in to FlowDesk with your company account.
Sent: Sign in to <span translate="no">FlowDesk</span> with your company account.
Output: Connectez-vous à FlowDesk avec votre compte d'entreprise.
pip install deepl
export DEEPL_AUTH_KEY=your-key
python protected_terms.pyimport deepl
from protected_terms import build_term_pattern, translate_with_protected_terms
client = deepl.DeepLClient("your-key")
pattern = build_term_pattern(["FlowDesk", "Power Hub", "Acme X-200"]) # build once, reuse
translations = translate_with_protected_terms(
client,
["Your sensors send their data to Power Hub every minute."],
target_lang="DE",
term_pattern=pattern,
)Any extra keyword arguments (context, formality, glossary and so on) are passed through to translate_text.
- Matching: terms are matched case-sensitively and only as whole words. When terms overlap, the longest one wins, so
Power Hub Protakes precedence overPower Hub. - Large term lists: load your terms from a file or database and call
build_term_patternonce at startup. It compiles a single regular expression for the whole list. - Give DeepL context: describe the protected terms in the
contextparameter (for example, "FlowDesk and Power Hub are product names"). Context is not translated or billed. - Already working with XML? Wrap the terms in a custom tag and use
tag_handling="xml"withignore_tagsinstead (docs).
MIT