2023-03-31

is there a way to exclude some words from being translated when using google cloud API on javascript?

is there a way to exclude some words from being translated when using google cloud API?

As an example lets say we are trying to translate to spanish the following sentence:

On my "playbook" the dogs where funny.

In which everything gets translated with the exception of "Playbook".

Extra notes: only javascript can be used here -in the example i tried to add the words "playbook" and ricky to not be translated. -i dont have the choice to add an exeption glossary

Hope you guys can help, been trying to find a proper solution to exclude some specific words.

 // Check if the text contains the words "playbooks" or "ricky"
    if (text.indexOf("playbooks") === -1 && text.indexOf("ricky") === -1) {
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "https://translation.googleapis.com/language/translate/v2");
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.onload = function() {
        if (xhr.status === 200) {
          var response = JSON.parse(xhr.responseText);
          var translatedText = response.data.translations[0].translatedText;
          
          // Replace all occurrences of quotation marks
          translatedText = translatedText.replace(/"/g, '"');
          
          // Replace the ampersand character with "y"
          translatedText = translatedText.replace(/&/g, "y");

          // Replace the word "playbooks" (with word boundary)
          translatedText = translatedText.replace(/\bplaybooks\b/gi, 'playbooks');


No comments:

Post a Comment