fkirc / attranslate

A command line tool for translating JSON, YAML, CSV, ARB, XML (via a CLI)
https://www.npmjs.com/package/attranslate
Other
336 stars 27 forks source link

YAML variable name is being translated in language ES #250

Closed wolframm closed 1 year ago

wolframm commented 1 year ago

Description

Using following YAML-file definition:

users:
  welcome(String value): "Hello $value!"

Running the following script:

COMMON_ARGS=("--srcFile=$INTL_DIR/messages.i18n.yaml" "--srcLng=en" "--srcFormat=yaml" "--targetFormat=yaml" "--service=google-translate" "--serviceConfig=$SERVICE_ACCOUNT_KEY" "--cacheDir=$INTL_DIR/cache" "--overwriteOutdated=true")

attranslate "${COMMON_ARGS[@]}" --targetFile=$TRANSLATION_DIR/messages_de.i18n.yaml --targetLng=de
attranslate "${COMMON_ARGS[@]}" --targetFile=$TRANSLATION_DIR/messages_es.i18n.yaml --targetLng=es

Generates the following, correct YAML in German...

users:
  welcome(String value): "Hallo $value!"

...and the following, faulty YAML in Spanish, where the variable $value suddenly shows up as $valor:

users:
  welcome(String value): "¡Hola $valor!"

Expected behavior The Spanish YAML should look as follows:

users:
  welcome(String value): "¡Hola $value!"
fkirc commented 1 year ago

Hello,

attranslate itself does not have a solution at the moment, but there are a few options to workaround this error:

For example, something like the following postprocessing script could be used (not tested, taken from ChatGPT):

const fs = require('fs');

const filePath = 'path/to/your/spanish/file.yaml';
const searchStr = '$valor';
const replaceStr = '$value';

fs.readFile(filePath, 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }

  const result = data.replace(new RegExp(searchStr, 'g'), replaceStr);

  fs.writeFile(filePath, result, 'utf8', (err) => {
    if (err) {
      console.error(err);
      return;
    }

    console.log(`File "${filePath}" updated successfully.`);
  });
});
wolframm commented 1 year ago

I switched to using ARB-format instead of YAML. The error does not appear when working with ARB.

Thank you for attranslate, by the way. Apparently it does the same job as using expensive websites like lokalise.com or localizely.com!

fkirc commented 1 year ago

Okay please let me know if something else needs fixing with regard to the output-files!

Theoretically, your original YAML-error could be fixed by expanding the already existing "--keySearch/--keyReplace"-feature, but I am not sure whether this would bring any improvement over customized postprocessing-scripts.