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.

61 lines
2.1 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 2 ]]; then
  6. echo "Please passe exactly 2 parameters: the .xml and the corresponding .po"
  7. exit 1
  8. fi
  9. ORIGINAL_XML=$1
  10. FR_XML="${ORIGINAL_XML%%.xml}.fr.xml"
  11. ORIGINAL_PO=$2
  12. TEMP_PO="/tmp/passage.temp"
  13. TEMP_MSGSTR="/tmp/msgstr.temp"
  14. TEMP_MSGID="/tmp/msgid.temp"
  15. #Let's work on a FR xml:
  16. cp ${ORIGINAL_XML} ${FR_XML}
  17. function insert_to_xml {
  18. sed -i "s~\(Key=\"${key}\" Val=\)\".*\"\(.*\)~\1\"${translation}\"\2~g" ${FR_XML}
  19. }
  20. for key in $(grep Key ${ORIGINAL_XML} | awk -F'"' '{print $2}'); do
  21. msgstr=""
  22. msgid=""
  23. translation=""
  24. # We search for the exact key in the po file untile we get a blank line, to get the full msgstr and the msgid
  25. awk -v key="${key}" '{pat="#. "key"$"} !NF{s=0}s;$0 ~ pat{ print $0; s=1 }' ${ORIGINAL_PO} > ${TEMP_PO}
  26. # Extract msgstr and merge all lines into one
  27. awk '!NF{s=0}s;/msgstr/{ print $0; s=1 }' ${TEMP_PO} | sed -e 's/^"//' -e 's/"$//' -e 's/^msgstr "//' > ${TEMP_MSGSTR}
  28. msgstr=$(awk 'NR{printf "%s",$0;next;}1' ${TEMP_MSGSTR})
  29. # Extract msgid and merge all lines into one
  30. awk '/msgid/{ s=1 }s;/msgstr/{ s=0 }' ${TEMP_PO} |grep -v "msgstr" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgid "//' > ${TEMP_MSGID}
  31. msgid=$(awk 'NR{printf "%s",$0;next;}1' ${TEMP_MSGID})
  32. # Escape special char causing problem in with sed and xml
  33. msgstr=${msgstr//'\n'/'\\n'}
  34. msgstr=${msgstr//'\"'/"""}
  35. msgstr=${msgstr//'&'/'\&'}
  36. msgid=${msgid//'\n'/'\\n'}
  37. msgid=${msgid//'\"'/"""}
  38. msgid=${msgid//'&'/'\&'}
  39. if [[ "${msgstr}" == "" ]]; then
  40. # if the msgstr is empty, then it's not translated yet, we use original string
  41. translation=${msgid}
  42. else
  43. translation=${msgstr}
  44. fi
  45. # Now we have the translation, we need to insert it to the xml file
  46. insert_to_xml
  47. done