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.

147 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//'\"'/"&quot;"}
  38. msgstr=${msgstr//'&'/'\&'}
  39. msgid=${msgid//'\"'/"&quot;"}
  40. msgid=${msgid//'&'/'\&'}
  41. if [[ "${msgstr}" == "" ]]; then
  42. # if the msgstr is empty, then it's not translated yet, we use original string
  43. translation=${msgid}
  44. else
  45. translation=${msgstr}
  46. fi
  47. # Using printf is more consistent then echo, here it transforms \n in real newline 😄
  48. printf "${translation}"
  49. }
  50. function rebuild_txt {
  51. ## We need to recurse on all event, then getting all node and their content for each event ##
  52. # In order to recurse properly, we need to sort the translationArray
  53. # To sort we use a new sorted array which contains all keys sorted following "Version style" order
  54. # eg: 0.2.story is before 0.2.1 and 0.2.1 is before 0.11.1
  55. sorted=()
  56. while IFS= read -rd '' key; do
  57. sorted+=( "$key" )
  58. done < <(printf '%s\0' "${!translationArray[@]}" | sort -zV)
  59. sortedEvent=()
  60. while IFS= read -rd '' key; do
  61. sortedEvent+=( "$key" )
  62. done < <(printf '%s\0' "${!eventArray[@]}" | sort -zV)
  63. currentEvent=""
  64. currentNode=""
  65. for index in "${sortedEvent[@]}"; do
  66. # If currentEvent is different than our index, then we just changed event and need to close the precedent
  67. # We also closed the next event in line
  68. if [[ ${currentEvent} != ${index} && "${currentEvent}" != "" ]]; then
  69. echo "[/NODE]" >> "${TXT}"
  70. echo >> "${TXT}"
  71. echo "[/EVENT]" >> "${TXT}"
  72. echo >> "${TXT}"
  73. fi
  74. echo "${eventArray[${index}]}" >> "${TXT}"
  75. for key in "${sorted[@]}"; do
  76. # We work only on key related to our current event
  77. # We need to compare key to the event id in index, ie the first part of the key (see #7)
  78. if [[ "${key/.*/}" == "${index}" ]]; then
  79. eventID="${index}"
  80. nodeID="$(awk -F'.' '{print $2}' <<< ${key})"
  81. type="$(awk -F'.' '{print $3}' <<< ${key})"
  82. # If nodeID is different than currentNodeID, then we just changed the node and need to close the node
  83. # We do this only when we are in the same event and not for the first iteration
  84. if [[ "${currentNodeID}" != "${nodeID}" && ${currentEvent} == ${index} && "${currentNodeID}" != "" ]]; then
  85. echo "[/NODE]" >> "${TXT}"
  86. echo >> "${TXT}"
  87. fi
  88. # Here type is a story or an out
  89. if [[ "${type}" == "story" ]]; then
  90. echo "+[NODE]${nodeID}" >> "${TXT}"
  91. echo "[STORY]" >> "${TXT}"
  92. echo "${translationArray[${key}]}" >> "${TXT}"
  93. echo "[/STORY]" >> "${TXT}"
  94. currentNodeID=${nodeID}
  95. currentEvent=${index}
  96. else
  97. echo "[OUT]${translationArray[${key}]}" >> "${TXT}"
  98. fi
  99. fi
  100. done
  101. done
  102. # At the end of last event, we go outside the loop so we need to close remaining node and event
  103. echo "[/NODE]" >> "${TXT}"
  104. echo >> "${TXT}"
  105. echo "[/EVENT]" >> "${TXT}"
  106. echo >> "${TXT}"
  107. }
  108. for index in "${!FILE[@]}"; do
  109. line="${FILE[${index}]}"
  110. line=${line##\#. }
  111. # If the line contains an event, we need to init variables for following compute
  112. if [[ ${line} == *"[EVENT]"* ]]; then
  113. event="$(awk -F'@@' '{print $1}' <<< ${line})"
  114. eventID="$(awk -F'(' '{print $2}' <<< ${event})"
  115. eventID="${eventID%%\)}"
  116. node="$(awk -F'@@' '{print $2}' <<< ${line})"
  117. nodeID="$(awk -F']' '{print $2}' <<< ${node})"
  118. eventArray[${eventID}]="${event}"
  119. fi
  120. if [[ ${line} == *"[STORY]"* ]]; then
  121. translation=$(extract_translation "${line}")
  122. translationArray["${eventID}.${nodeID}.story"]="${translation}"
  123. elif [[ ${line} == *"[OUT]"* ]]; then
  124. out="$(awk -F'@@' '{print $3}' <<< ${line})"
  125. outID=$(awk -F']' '{print $2}' <<< ${out})
  126. translation=$(extract_translation "${line}")
  127. translationArray["${eventID}.${nodeID}.${outID}"]="${translation}"
  128. fi
  129. done
  130. # Now we have 2 arrays: one with all event name, and one with all translation. We can rebuild the original file
  131. rebuild_txt