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.

extract_module.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. # This script is used to extract modules to pot
  3. # then pass the .pot in poedit to merge duplication
  4. if [[ "$#" -ne 1 ]]; then
  5. echo "Please pass exactly 1 parameter: the txt module"
  6. exit 1
  7. fi
  8. ORIGINAL_TXT="$1"
  9. POT_DIR="$(pwd)/Modules/pot/"
  10. TEMP_DIR="$(mktemp -d)"
  11. POT=$(basename "${ORIGINAL_TXT%%.txt}.pot")
  12. TEMP_POT="${TEMP_DIR}/${POT}"
  13. POT="${POT_DIR}/${POT}"
  14. # Reinit exiting POT
  15. mkdir -p "${POT_DIR}"
  16. truncate -s 0 "${POT}"
  17. dos2unix "${ORIGINAL_TXT}" &> /dev/null
  18. comment_event=""
  19. comment_node=""
  20. comment_story=""
  21. comment_out=""
  22. event_empty=0
  23. out_incr=1
  24. story_first_line=0
  25. while read -r line; do
  26. if [[ ${line} == *"-- [EVENT]"* ]]; then
  27. comment_event="${line}"
  28. # At start of event, we set event_empty just in case
  29. event_empty=1
  30. elif [[ ${line} == *"+[NODE]"* ]]; then
  31. comment_node="${line}"
  32. # At least one node, disable event_empty
  33. event_empty=0
  34. elif [[ ${line} == *"[STORY]"* ]]; then
  35. comment_story="${comment_event}@@${comment_node}@@${line}"
  36. #Time for new story, write the comment to pot
  37. echo "#. ${comment_story}" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  38. echo >> "${TEMP_POT}"
  39. story_first_line=1
  40. elif [[ ${line} == *"[/STORY]"* ]]; then
  41. # we reach end of story, write the empty msgid and reinit the comment_story
  42. echo 'msgstr ""' >> "${TEMP_POT}"
  43. echo >> "${TEMP_POT}"
  44. comment_story=""
  45. elif [[ ${line} == *"[OUT]"* ]]; then
  46. comment_out="${comment_event}@@${comment_node}@@[OUT]${out_incr}"
  47. # OUT are one line, let's write it to the pot
  48. echo "#. ${comment_out}" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  49. echo >> "${TEMP_POT}"
  50. # Get rid of special character
  51. line="${line//'"'/'\"'}"
  52. echo "msgid \"${line#\[OUT\]}\"" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  53. echo >> "${TEMP_POT}"
  54. echo 'msgstr ""' >> "${TEMP_POT}"
  55. echo >> "${TEMP_POT}"
  56. ((out_incr++))
  57. elif [[ ${line} == *"[/NODE]"* ]]; then
  58. # We reach end of node, reinit
  59. comment_node=""
  60. comment_story=""
  61. comment_out=""
  62. out_incr=1
  63. elif [[ ${line} == *"[/EVENT]"* && ${event_empty} == 1 ]]; then
  64. # We reach end of event and the event is empty, write special lines
  65. comment_empty="${comment_event}EMPTY@@[EMPTY]0@@[EMPTY]0"
  66. echo "#. ${comment_empty}" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  67. echo >> "${TEMP_POT}"
  68. echo 'msgid "[SPECIAL EMPTY EVENT STRING]"' >> "${TEMP_POT}"
  69. echo >> "${TEMP_POT}"
  70. echo 'msgstr "[SPECIAL EMPTY EVENT STRING]"' >> "${TEMP_POT}"
  71. echo >> "${TEMP_POT}"
  72. # Then reinit
  73. comment_event=""
  74. comment_node=""
  75. comment_story=""
  76. comment_out=""
  77. event_empty=0
  78. out_incr=1
  79. elif [[ ${line} == *"[/EVENT]"* ]]; then
  80. # We reach end of event, reinit
  81. comment_event=""
  82. comment_node=""
  83. comment_story=""
  84. comment_out=""
  85. out_incr=1
  86. elif [[ "${comment_story}" != "" && ${story_first_line} == 1 ]]; then
  87. # If the line has nothing particular, and the comment_story is not empty, it's a story string
  88. # Get rid of special character
  89. line="${line//'"'/'\"'}"
  90. # As the story_fist_line is set, it's the first line of the story, add the msgid
  91. echo "msgid \"${line}\"" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  92. echo >> "${TEMP_POT}"
  93. story_first_line=0
  94. elif [[ ${comment_story} != "" ]]; then
  95. # We are on a multilines story, write it but without the msgid
  96. # As developer sometimes forgot empty lines at the end of STORY, skip empty lines
  97. if [[ "${line}" = "" ]]; then
  98. continue
  99. fi
  100. # Get rid of special character
  101. line="${line//'"'/'\"'}"
  102. # Also as this is multiline, we need to add \n to the previous line (no way to detect it sooner...)
  103. sed -e '$s/\(.*\)"$/\1\\n"/' -i "${TEMP_POT}"
  104. echo "\"${line}\"" | tr -d '\r' | tr -d '\n' >> "${TEMP_POT}"
  105. echo >> "${TEMP_POT}"
  106. else
  107. # It should be a newline, write a newline
  108. # If new case are added later, we need to deal with it manually, do not break the file by writing them
  109. echo "" >> "${TEMP_POT}"
  110. fi
  111. done < "${ORIGINAL_TXT}"
  112. # Unify duplicate due to pot syntax
  113. msguniq --no-wrap "${TEMP_POT}" > "${POT}"