0

I am trying to translate all marked layers in Photoshop 2023 through the DeepL API using JavaScript. On each text layer, I'd like to be asked which target language should be used.

But I am running into syntax errors - can you help me out here?

This is the error message:

Error 8: Syntaxerror Linie: 13 -> const url = ${apiUrl}?>auth_key=${API_KEY}&text=${encodedText}&source_lang=${sourceLang}&target_lang=${targetLang};

And this is the current script:

const API_KEY = 'key';

// Eine Funktion, um Text zu übersetzen
function translateText(text, sourceLang, targetLang) {
  // API-Endpunkt für DeepL
  const apiUrl = 'https://api.deepl.com/v2/translate';

  // Der Text in URL-kodiertem Format
  const encodedText = encodeURIComponent(text);

  // Die vollständige URL für den API-Aufruf
  const url = `${apiUrl}?auth_key=${API_KEY}&text=${encodedText}&source_lang=${sourceLang}&target_lang=${targetLang}`;

  // Eine HTTP-Anfrage, um den Text zu übersetzen
  const response = new XMLHttpRequest();
  response.open('POST', url, false);
  response.send();

  // Die übersetzte Text
  const translatedText = JSON.parse(response.responseText).translations[0].text;

  return translatedText;
}

// Eine Funktion, um eine Textebene zu übersetzen
function translateLayer(layer, sourceLang) {
  // Der Text in der Textebene
  const text = layer.textItem.contents;

  // Nach der Zielsprache fragen
  const targetLang = prompt(`In welche Sprache soll die Textebene "${layer.name}" übersetzt werden?`, 'EN');

  // Wenn eine Zielsprache angegeben wurde, die Textebene übersetzen
  if (targetLang) {
    // Der übersetzte Text
    const translatedText = translateText(text, sourceLang, targetLang);

    // Die Textebene aktualisieren
    layer.textItem.contents = translatedText;
  }
}

// Eine Funktion, um alle markierten Textebenen zu übersetzen
function translateSelectedLayers(sourceLang) {
  // Alle markierten Ebenen in einem Array
  const selectedLayers = app.activeDocument.activeLayers;

  // Alle Textebenen in den markierten Ebenen filtern
  const textLayers = selectedLayers.filter(layer => layer.kind === LayerKind.TEXT);

  // Jede Textebene übersetzen
  textLayers.forEach(layer => translateLayer(layer, sourceLang));
}

// Beispiel: Alle markierten Textebenen von Deutsch übersetzen
translateSelectedLayers('DE');```

Thank you for your help!
thowi
thowi
  • 35
  • 7
  • Photoshop's version of JavaScript probably does not support template literals; that's a fairly recent introduction to JavaScript. – Pointy May 09 '23 at 12:33
  • It might not even support `const` and `let`. – Pointy May 09 '23 at 12:34
  • It's kicking an error as you have a ` instead of a single or double quote (' or ") – Ghoul Fool May 13 '23 at 08:36
  • @Pointy Photoshop ECMA script didn't used to support `const` and `let` but that is okay now. However, I'm pretty sure `filter` and ` =>` will be obstacles. – Ghoul Fool May 13 '23 at 08:41
  • @thowi You might also want to take a look at [this](https://community.adobe.com/t5/photoshop-ecosystem-discussions/using-xmlhttprequest-within-photoshop-script/m-p/7342869) – Ghoul Fool May 13 '23 at 08:55

1 Answers1

1

It's not as concise, but if string interpolation is not supported you could try doing this in line 13:

  const url = [apiUrl, '?auth_key=', API_KEY, '&text=', encodedText, '&source_lang=', sourceLang, '&target_lang=', targetLang].join('');
Gaze
  • 149
  • 1
  • 9