Reputation: 187
Does anybody know whether there is a possiblity to exclude portions of a text from being processed by the google translator toolkit?
A great advantage of this tool is that it suggests the translations of sentences which have already been translated in another context. However, if any additional footnote and/or remark has been added to the text, it won't be recognized as a match. I am looking for a possibility to enclose such text in "brackets" within which it will be ignored.
For example, the following two strings should be recognized as identical:
"This is one continuous sentence."
"This is {this text will be ignored}one continuous sentence."
and be translated i.e. into German as:
"Dies ist ein zusammenhängender Satz."
"Dies is {this text will be ignored}ein zusammenhängender Satz."
If neccessary I could number such insertions and place their content into additional paragraphs like:
"This is one continuous sentence."
"This is {1}one continuous sentence."
"{1 this text will be ignored}
thanks a lot in advance, Marcel
Upvotes: 5
Views: 4060
Reputation: 22672
Quoting from the Google Translate FAQ ...
How do I tell Cloud Translation API to NOT translate something?
You can use the following HTML tags:
<span> Translate this text ... <span translate="no">but not this text</span> <span class="notranslate">nor this text</span> ... but do translate this text. </span>
This functionality requires the source text to be submitted in HTML.
If it's "not working for you" a likely reason is that you requested TEXT translation not HTML translation via the API. If you only want TEXT translation then wrapper your plan text in HTML tags (with <span>
tags as above) and then unwrap after translation.
The same span
tags can be used with the Azure Translation API for HTML translation.
Preventing Translation:
Upvotes: 2
Reputation: 453
I was having issues with using the HTML as suggested in other answers.
What I did was replace every {{
with {{Z_
and every }}
with _Z}}
so instead of Hello {{name}}
I send Hello {{Z_name_Z}}
. After receiving the translations I replaced it backward and everything came back correctly.
textToTranslate = textToTranslate.replaceAll('{{', '{{Z_').replaceAll('}}', '_Z}}');
translation = await translate.translateText...
translation = translation.replaceAll('{{Z_', '{{').replaceAll('_Z}}', '}}');
Upvotes: 0
Reputation: 11
The "skiptranslate" class didn't work for me in the po files I submitted to gtt. Gtt continued to translate everything. I noticed that gtt does not translate anything inside an href attribute, so what I did was to pre-process my .po file (using a "copy" grunt task) by changing {{my_expr}}
type strings to <a href='{{my_expr}}/>
strings, then changing them back to {{my_expr}}
after the gtt translation (using another "copy" grunt task).
I'm not sure how this affects the semantics of the translation, but at least the resulting translation doesn't break my templating code.
Here is my grunt copy task config showing the regular expressions I used:
copy: {
fixupPoFileForTranslation: {
src: [], // Fill in src and dest!
dest: '',
options : {
process: function (content, srcpath) {
return content.replace(/"Go page"/g, '"Go"')
// Change handlebars {{<name>}} to <a ref='<name>'/> to stop
// machine translation from translating them.
return content.replace(/(\{\{[a-zA-Z_\$].*?\}\})/g, '<a href=\'$1\'/>')
// Same thing for our js .format {0}, {1}, ...
.replace(/(\{\d*?\})/g, '<a href=\'$1\'/>');
}
}
},
fixupPoFileForMerge: {
src: [],
dest: '',
options : {
process: function (content, srcpath) {
// Restore <a href=... back to {{<name>}}
return content..replace(/<a href='(\{\{[a-zA-Z_\$].*?\}\})'(\/>|)/gi, '$1')
// Same thing for {0} constructs
.replace(/<a href=' *(\{.\d?\})('\/>|)/gi, '$1');
}
}
}
}
Upvotes: 1
Reputation: 9
Add a span tag with a class of 'skiptranslate' to the bits that you do not want to be translated, like this:
"This is <span class="skiptranslate">this text will be ignored</span>
one continuous sentence."
Upvotes: 0