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.

145 lines
5.8 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 pass exactly 2 parameters: the .po and the language"
  7. exit 1
  8. fi
  9. LANGUAGE="${2}"
  10. WORKDIR="/tmp/thea2/${LANGUAGE}"
  11. PO="${1}"
  12. FINALDIR="$(pwd)/Translation/${LANGUAGE}/game_files/Modules/"
  13. TXT=$(basename "${PO%%.po}.txt")
  14. TXT="${FINALDIR}/${TXT}"
  15. # Initialization
  16. mkdir -p "${WORKDIR}"
  17. mkdir -p "${FINALDIR}"
  18. truncate -s 0 "${TXT}"
  19. readarray -t FILE < "${PO}"
  20. declare -A eventArray=()
  21. declare -A translationArray=()
  22. # First thing first, let's anonymize the po and delete all email references
  23. sed -i 's/[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,6\}//' "${PO}"
  24. function extract_translation {
  25. key="${line}"
  26. # We need to escape special char for regex: [ + and (
  27. key="${key//[/\\\\[}"
  28. key="${key//+/\\\\\+}"
  29. key="${key//(/\\\\\(}"
  30. # We search for the exact key in the po file until we get a blank line, to get the full msgstr and the msgid
  31. tempPO=$(awk -v key="${key}" '{pat="#. "key""} !NF{s=0}s;$0 ~ pat{ print $0; s=1 }' "${PO}")
  32. # Extract msgstr and merge all lines into one
  33. tempMsgstr=$(awk '!NF{s=0}s;/msgstr/{ print $0; s=1 }' <<< "${tempPO}" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgstr "//')
  34. msgstr=$(awk 'NR{printf "%s",$0;next;}1' <<< ${tempMsgstr})
  35. # Extract msgid and merge all lines into one
  36. tempMsgid="$(awk '/msgid/{ s=1 }s;/msgstr/{ s=0 }' <<< \"${tempPO}\" |grep -v "msgstr" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgid "//')"
  37. msgid="$(awk 'NR{printf "%s",$0;next;}1' <<< ${tempMsgid})"
  38. # Escape special char causing problem in with sed and xml
  39. msgstr=${msgstr//'\n'/'\\n'}
  40. msgstr=${msgstr//'\"'/"&quot;"}
  41. msgstr=${msgstr//'&'/'\&'}
  42. msgid=${msgid//'\n'/'\\n'}
  43. msgid=${msgid//'\"'/"&quot;"}
  44. msgid=${msgid//'&'/'\&'}
  45. if [[ "${msgstr}" == "" ]]; then
  46. # if the msgstr is empty, then it's not translated yet, we use original string
  47. translation=${msgid}
  48. else
  49. translation=${msgstr}
  50. fi
  51. echo ${translation}
  52. }
  53. function rebuild_txt {
  54. ## We need to recurse on all event, then getting all node and their content for each event ##
  55. # In order to recurse properly, we need to sort the translationArray
  56. # To sort we use a new sorted array which contains all keys sorted following "Version style" order
  57. # eg: 0.2.story is before 0.2.1 and 0.2.1 is before 0.11.1
  58. sorted=()
  59. while IFS= read -rd '' key; do
  60. sorted+=( "$key" )
  61. done < <(printf '%s\0' "${!translationArray[@]}" | sort -zV)
  62. currentEvent=""
  63. currentNode=""
  64. for index in "${!eventArray[@]}"; do
  65. # If currentEvent is different than our index, then we just changed event and need to close the precedent
  66. # We also closed the next event in line
  67. if [[ ${currentEvent} != ${index} && "${currentEvent}" != "" ]]; then
  68. echo "[/NODE]" >> "${TXT}"
  69. echo >> "${TXT}"
  70. echo "[/EVENT]" >> "${TXT}"
  71. echo >> "${TXT}"
  72. fi
  73. echo "${eventArray[${index}]}" >> "${TXT}"
  74. for key in "${sorted[@]}"; do
  75. # We work only on key related to our current event
  76. if [[ "${key}" == "${index}"* ]]; then
  77. eventID="${index}"
  78. nodeID="$(awk -F'.' '{print $2}' <<< ${key})"
  79. type="$(awk -F'.' '{print $3}' <<< ${key})"
  80. # If nodeID is different than currentNodeID, then we just changed the node and need to close the node
  81. # We do this only when we are in the same event and not for the first iteration
  82. if [[ "${currentNodeID}" != "${nodeID}" && ${currentEvent} == ${index} && "${currentNodeID}" != "" ]]; then
  83. echo "[/NODE]" >> "${TXT}"
  84. echo >> "${TXT}"
  85. fi
  86. # Here type is a story or an out
  87. if [[ "${type}" == "story" ]]; then
  88. echo "+[NODE]${nodeID}" >> "${TXT}"
  89. echo "[STORY]" >> "${TXT}"
  90. echo "${translationArray[${key}]}" >> "${TXT}"
  91. echo "[/STORY]" >> "${TXT}"
  92. currentNodeID=${nodeID}
  93. currentEvent=${index}
  94. else
  95. echo "[OUT]${translationArray[${key}]}" >> "${TXT}"
  96. fi
  97. fi
  98. done
  99. done
  100. # At the last of last event, we go outside the loop so we need to close remaining node and event
  101. echo "[/NODE]" >> "${TXT}"
  102. echo >> "${TXT}"
  103. echo "[/EVENT]" >> "${TXT}"
  104. echo >> "${TXT}"
  105. }
  106. for index in "${!FILE[@]}"; do
  107. line="${FILE[${index}]}"
  108. line=${line##\#. }
  109. # If the line contains an event, we need to init variables for following compute
  110. if [[ ${line} == *"[EVENT]"* ]]; then
  111. event="$(awk -F'@@' '{print $1}' <<< ${line})"
  112. eventID="$(awk -F'(' '{print $2}' <<< ${event})"
  113. eventID="${eventID%%\)}"
  114. node="$(awk -F'@@' '{print $2}' <<< ${line})"
  115. nodeID="$(awk -F']' '{print $2}' <<< ${node})"
  116. eventArray[${eventID}]="${event}"
  117. fi
  118. if [[ ${line} == *"[STORY]"* ]]; then
  119. translation=$(extract_translation "${line}")
  120. translationArray["${eventID}.${nodeID}.story"]="${translation}"
  121. elif [[ ${line} == *"[OUT]"* ]]; then
  122. out="$(awk -F'@@' '{print $3}' <<< ${line})"
  123. outID=$(awk -F']' '{print $2}' <<< ${out})
  124. translation=$(extract_translation "${line}")
  125. translationArray["${eventID}.${nodeID}.${outID}"]="${translation}"
  126. fi
  127. done
  128. # Now we have 2 arrays: one with all event name, and one with all translation. We can rebuild the original file
  129. rebuild_txt