3 Replies Latest reply on Dec 19, 2015 3:21 AM by user19752

    Google Translate API - escape character?


      I'm experimenting with the google translate API. Finding it pretty easy to work with with InsertfromURL and some pretty straightforward parsing of the resulting JSON.


      (I'm well aware of the imperfect nature of google translation, btw. I'm using this as a "first pass" for a native speaker to clean up.)


      Here's my challenge. I'm translating phrases (not individual words, since word order changes from language to language, and context is important generally, as well). Sometimes those phrases include field data that just "is what it is", and shouldn't be translated.


      For example, I may be on a record for a person whose first name is "Destiny", and I might want to translate the phrase "Do you want to send Destiny an email?" into Spanish. The Google API comes back with "¿Quieres enviar destino un correo electrónico?".


      Surely, there must be some sort of escape character that I can put around "Destiny" so that her name doesn't get translated... but I cannot find it. If it were just names, of course, I'm sure I could do a down and dirty workaround, but I'd imagine this same issue could come up with things like product names, or just places where I want to insert the value from a field "as-is".


      For translating web sites, there's a <notranslate> tag (or something like that) in the api, but I can't see the equivalent for just translating a phrase, nor can I figure out how to pass my phrase through the part of the api where that tag would work.


      Has anyone tried this and figured it out? If not, I'll keep looking, but I don't want to wear out my keyboard searching google if one of you has already figured this out.


      Chris Cain


        • 1. Re: Google Translate API - escape character?

          Did you try html?

          How do I tell Google Translate API to NOT translate something?
          You can use the following HTML tag <span class="e;notranslate"e;> </span>. Note that this functionality requires the source text to be submitted in HTML.
          • 2. Re: Google Translate API - escape character?

            Sounds like it could be the right track, but could you elaborate?


            Here's the basic pattern I'm using thus far:


            1) Insert from URL


                  & “?key=" & $apiKey

                  & "&source=" & $inputLanguageAbbreviation & "&target=" & $outputLanguageAbbreviation

                  & "&q=" & Substitute ( $phrase ; " " ; "%20" )

            2 ) Parse the result

                 JSON result

                 Parse “translatedText”

                 Check for errors


            So, if $phrase = "Do you want to send Destiny an email?"...


            Am I using the wrong bit of the api for this? Or do you see how I could insert appropriate html into this existing method?


            I hate to be dense, I just can't seem to find a way forward. This basic pattern worked well right away, so I ran with it. Now, when I've tried to tinker with it to use the "notranslate" tag, I manage to just make it not work at all. I've read the documentation and a ton of blogs and forums and such, but it's as though there are developers just translating phrases, and there are developers translating web pages on the fly, and the two groups don't seem to speak the same language (if you will).


            I'm probably just missing one or two things that should be obvious to me, but the problem is that I don't know what they are, so I may need a rundown of many "obvious" things before I see the ones I'm missing.


            Thanks in advance for your patience.


            Chris Cain


            • 3. Re: Google Translate API - escape character?

              I didn't tested, but reading https://cloud.google.com/translate/v2/using_rest#query-params

              it would be

              $phrase = "Do you want to send <span class="e;notranslate"e;>Destiny</span> an email?".


                    & "&q=" & GetAsURLEncoded ( $phrase )


              You don't use format parameter in this URL, then it is already html as default value, if there is no html tag in the phrase.

              Possibly inserted <span> tag will remain in the translated result.