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.

148 lines
5.8 KiB

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