<commit_before>#!/usr/bin/env bash
# DESCRIPTION
#   Translate <DefaultValue> and add a new <Value> element to each <Resource> node.
#   To get a starting environement run the following command:
#     cp Resources.{en,fr}.xml # create a copy from source lang to target lang
#
# USAGE
#   ./xml-translator.bash en fr input.xml
#
# @author: <NAME> <EMAIL>


sourceLang="$1"
targetLang="$2"
inputFile="$3"
outputFile="${inputFile%.xml}.$targetLang.xml"

let count=$(xmlstarlet sel -t -m "/" -v "count(/Resources/Resource/DefaultValue)" "$inputFile")

# Translates a phrase from English to Portuguese
function translate() {
  local text="$1"
  local sourceLang="$2"
  local targetLang="$3"
  curl -A "Mozilla/5.0 XML-translator" -s --data-urlencode text="$text" "http://translate.google.com/translate_a/t?client=t&hl=en&sl=$sourceLang&tl=$targetLang&ie=UTF-8&oe=UTF-8&multires=1&prev=btn&ssel=0&tsel=0&sc=1" | sed 's/\[\[\["\([^"]*\).*/\1/'
}


function run() {
  local sourceLang="$1"
  local targetLang="$2"

  for ((i=1; i<=$count; i++));
  do
    echo "$i"
    text="$(xmlstarlet sel -t \
        -m "/Resources/Resource[$i]" \
        -v "./DefaultValue" \
        -n "$inputFile"
      )"
    # trad="$(translate "$text" "$sourceLang" "$targetLang")"
    trad="dummy translation test"
    printf "%s -> %s\n" "$text" "$trad"
    xmlstarlet ed -a "/Resources/Resource[$i]/DefaultValue" \
      -t elem -n "Value" \
      -v "$trad" "$outputFile" \
      > "$outputFile.tmp"
    cp "$outputFile"{.tmp,}
  done
}

run "$sourceLang" "$targetLang"<commit_msg>refactor(Xpath,xmlstarlet): add variable $xpath, $i18nTag for reusability<commit_after>#!/usr/bin/env bash
# DESCRIPTION
#   Translate <DefaultValue> and add a new <Value> element to each <Resource> node.
#   To get a starting environement run the following command:
#     cp Resources.{en,fr}.xml # create a copy from source lang to target lang
#
# USAGE
#   ./xml-translator.bash en fr input.xml
#
# @author: <NAME> <EMAIL>


sourceLang="$1"
targetLang="$2"
inputFile="$3"
outputFile="${inputFile%.xml}.$targetLang.xml"

let count=$(xmlstarlet sel -t -m "/" -v "count(/Resources/Resource/DefaultValue)" "$inputFile")

# Translates a phrase from English to Portuguese
function translate() {
  local text="$1"
  local sourceLang="$2"
  local targetLang="$3"
  curl -A "Mozilla/5.0 XML-translator" -s --data-urlencode text="$text" "http://translate.google.com/translate_a/t?client=t&hl=en&sl=$sourceLang&tl=$targetLang&ie=UTF-8&oe=UTF-8&multires=1&prev=btn&ssel=0&tsel=0&sc=1" | sed 's/\[\[\["\([^"]*\).*/\1/'
}


function run() {
  local sourceLang="$1"
  local targetLang="$2"

  for ((i=1; i<=$count; i++));
  do
    echo "$i"
    xpath="/Resources/Resource[$i]"
    i18nTag="DefaultValue"

    text="$(xmlstarlet sel -t \
        -m "$xpath" \
        -v "$i18nTag/text()" \
        -n "$inputFile"
      )"
    # trad="$(translate "$text" "$sourceLang" "$targetLang")"
    trad="dummy translation test"
    printf "%s -> %s\n" "$text" "$trad"
    xmlstarlet ed -a "$xpath/$i18nTag" \
      -t elem -n "Value" \
      -v "$trad" "$outputFile" \
      > "$outputFile.tmp"
    cp "$outputFile"{.tmp,}
  done
}

run "$sourceLang" "$targetLang"
{'fraction_non_alphanumeric': 0.19412316348859018, 'fraction_numerical': 0.013754298218193186, 'mean_word_length': 3.5779685264663805, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '616309', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1143, 'n_words': 271}