Scripting tools to interact with Thea 2 The Shattering files in order to translate them easily.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.5 KiB

  1. #!/bin/bash
  2. # to add document to zanata, extract .pot with:
  3. # grep Key DATABASE_UI_LOCALIZATION.xml | awk -F'"' '{print "#. "$2"\n" "msgid " "\""$4"\"" "\n" "msgstr " "\"\""}' > DATABASE_UI_LOCALIZATION.pot
  4. # then pass the .pot in poedit to merge duplication and add the Zanata header
  5. if [[ "$#" -ne 3 ]]; then
  6. echo "Please pass exactly 3 parameters: the .xml, the corresponding .po and the language"
  7. exit 1
  8. fi
  9. ORIGINAL_XML="$1"
  10. LANGUAGE="$3"
  11. FINAL_DIR="$(pwd)/Translation/${LANGUAGE}/game_files/Databases/"
  12. TRANS_XML=$(basename "${ORIGINAL_XML}")
  13. TRANS_XML="${FINAL_DIR}/${TRANS_XML}"
  14. ORIGINAL_PO="$2"
  15. WORKDIR="/tmp/thea2/${LANGUAGE}"
  16. TEMP_PO="/${WORKDIR}/passage.temp"
  17. TEMP_MSGSTR="/${WORKDIR}/msgstr.temp"
  18. TEMP_MSGID="/${WORKDIR}/msgid.temp"
  19. #Let's work on the translated xml:
  20. mkdir -p "${FINAL_DIR}"
  21. mkdir -p "${WORKDIR}"
  22. cp "${ORIGINAL_XML}" "${TRANS_XML}"
  23. # First thing first, let's anonymize the pot and delete all email references
  24. sed -i 's/[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,6\}//' "${ORIGINAL_PO}"
  25. function insert_to_xml {
  26. sed -i "s~\(Key=\"${key}\" Val=\)\".*\"\(.*\)~\1\"${translation}\"\2~g" ${TRANS_XML}
  27. }
  28. for key in $(grep Key ${ORIGINAL_XML} | awk -F'"' '{print $2}'); do
  29. msgstr=""
  30. msgid=""
  31. translation=""
  32. # We search for the exact key in the po file until we get a blank line, to get the full msgstr and the msgid
  33. awk -v key="${key}" '{pat="#. "key"$"} !NF{s=0}s;$0 ~ pat{ print $0; s=1 }' ${ORIGINAL_PO} > ${TEMP_PO}
  34. # Extract msgstr and merge all lines into one
  35. awk '!NF{s=0}s;/msgstr/{ print $0; s=1 }' ${TEMP_PO} | sed -e 's/^"//' -e 's/"$//' -e 's/^msgstr "//' > ${TEMP_MSGSTR}
  36. msgstr=$(awk 'NR{printf "%s",$0;next;}1' ${TEMP_MSGSTR})
  37. # Extract msgid and merge all lines into one
  38. awk '/msgid/{ s=1 }s;/msgstr/{ s=0 }' ${TEMP_PO} |grep -v "msgstr" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgid "//' > ${TEMP_MSGID}
  39. msgid=$(awk 'NR{printf "%s",$0;next;}1' ${TEMP_MSGID})
  40. # Escape special char causing problem in with sed and xml
  41. msgstr=${msgstr//'\n'/'\\n'}
  42. msgstr=${msgstr//' & '/' & '}
  43. msgstr=${msgstr//'\"'/"""}
  44. msgstr=${msgstr//'&'/'\&'}
  45. msgid=${msgid//'\n'/'\\n'}
  46. msgid=${msgid//' & '/' & '}
  47. msgid=${msgid//'\"'/"""}
  48. msgid=${msgid//'&'/'\&'}
  49. if [[ "${msgstr}" == "" ]]; then
  50. # if the msgstr is empty, then it's not translated yet, we use original string
  51. translation=${msgid}
  52. else
  53. translation=${msgstr}
  54. fi
  55. # Now we have the translation, we need to insert it to the xml file
  56. insert_to_xml
  57. done