lang-build.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/bin/bash
  2. #
  3. # Version 1.0.2 Build 28
  4. #
  5. # lang-build.sh - multi-language support script
  6. # generate lang_xx.bin (language binary file)
  7. #
  8. # Input files:
  9. # lang_en.txt or lang_en_xx.txt
  10. #
  11. # Output files:
  12. # lang_xx.bin
  13. #
  14. # Depending on files:
  15. # ../Firmware/config.h to read the max allowed size for translations
  16. #
  17. # Temporary files:
  18. # lang_en.cnt //calculated number of messages in english
  19. # lang_en.max //maximum size determined by reading "../Firmware/config.h"
  20. # lang_xx.tmp
  21. # lang_xx.dat
  22. #
  23. #############################################################################
  24. # Change log:
  25. # 18 June 2018, XPila, Initial
  26. # 19 June 2018, XPila, New ML support
  27. # 18 Oct. 2018, XPila, New lang French
  28. # 26 Nov. 2018, mkbel, Automate secondary language support build.
  29. # 7 May 2019, ondratu Check translation dictionary files to display definition
  30. # 19 June 2019, mkbel Disable language check warnings of type "[W]: No display definition on line".
  31. # Those warnings were masking all other much more useful build process output.
  32. # 14 Sep. 2019, 3d-gussner, Prepare adding new language
  33. # 18 Sep. 2020, 3d-gussner, Update new messages and their translations, fix translations
  34. # Update CZ, FR, IT, ES translations
  35. # CZ thanks to @DRracer
  36. # FR thanks to Carlin Dcustom
  37. # ES
  38. # IT thanks to @wavexx
  39. # Co-authored-by: @DRracer, @wavexx
  40. # 1 Mar. 2021, 3d-gussner, Add Dutch translation
  41. # 17 Dec. 2021, 3d-gussner, Use one config file for all languages
  42. # 21 Dec. 2021, 3d-gussner, Prepare more community languages
  43. # Swedish
  44. # Danish
  45. # Slovanian
  46. # Hungarian
  47. # Luxembourgian
  48. # Croatian
  49. # 3 Jan. 2022, 3d-gussner, Prepare Lithuanian
  50. # Cleanup outdated code
  51. # 11 Jan. 2022, 3d-gussner, Add message and size count comparison
  52. # Added version and Change log
  53. # colored output
  54. # Add Community language support
  55. # Use `git rev-list --count HEAD lang-build.sh`
  56. # to get Build Nr
  57. # 25 Jan. 2022, 3d-gussner, Fix check
  58. # Update documentation
  59. # 10 Feb. 2022, 3d-gussner, Use SRCDIR for compatibility with build server
  60. # 11 Feb. 2022, 3d-gussner, Change to python3
  61. #############################################################################
  62. #
  63. # Config:
  64. if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
  65. if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo "$(tput setaf 1)Config NG!$(tput sgr 0)" >&2; exit 1; fi
  66. if [ ! -z "$COMMUNITY_LANGUAGES" ]; then
  67. LANGUAGES+=" $COMMUNITY_LANGUAGES"
  68. fi
  69. #startup message
  70. echo "$(tput setaf 2)lang-build.sh started$(tput sgr 0)" >&2
  71. echo "lang-build languages:$(tput setaf 2)$LANGUAGES$(tput sgr 0)" >&2
  72. #awk code to format ui16 variables for dd
  73. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  74. #exiting function
  75. finish()
  76. {
  77. if [ $1 -eq 0 ]; then
  78. echo "$(tput setaf 2)lang-build.sh finished with success$(tput sgr 0)" >&2
  79. else
  80. echo "$(tput setaf 1)lang-build.sh finished with errors!$(tput sgr 0)" >&2
  81. fi
  82. exit $1
  83. }
  84. #returns hexadecial data for lang code
  85. lang_code_hex_data()
  86. # $1 - language code ('en', 'cz'...)
  87. {
  88. case "$1" in
  89. *en*) echo '\x6e\x65' ;;
  90. *cz*) echo '\x73\x63' ;;
  91. *de*) echo '\x65\x64' ;;
  92. *es*) echo '\x73\x65' ;;
  93. *fr*) echo '\x72\x66' ;;
  94. *it*) echo '\x74\x69' ;;
  95. *pl*) echo '\x6c\x70' ;;
  96. #Community language support
  97. #Dutch
  98. *nl*) echo '\x6c\x6e' ;;
  99. #Swedish
  100. *sv*) echo '\x76\x73' ;;
  101. #Norwegian
  102. *no*) echo '\x6f\x6e' ;;
  103. #Danish
  104. *da*) echo '\x61\x64' ;;
  105. #Slovak
  106. *sk*) echo '\x6b\x73' ;;
  107. #Slovanian
  108. *sl*) echo '\x6c\x73' ;;
  109. #Hungarian
  110. *hu*) echo '\x75\x68' ;;
  111. #Luxembourgish
  112. *lb*) echo '\x62\x6c' ;;
  113. #Croatian
  114. *hr*) echo '\x72\x68' ;;
  115. #Lithuanian
  116. *lt*) echo '\x74\x6c' ;;
  117. #Romanian
  118. *ro*) echo '\x6f\x72' ;;
  119. #Use the 2 lines below as a template and replace 'qr' and `\x71\x72`
  120. ##New language
  121. # *qr*) echo '\x71\x72' ;;
  122. esac
  123. echo '??'
  124. }
  125. write_header()
  126. # $1 - lang
  127. # $2 - size
  128. # $3 - count
  129. # $4 - checksum
  130. # $5 - signature
  131. {
  132. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  133. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  134. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  135. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  136. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  137. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  138. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  139. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  140. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  141. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  142. sig_h=$(($5 / 65536))
  143. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  144. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  145. sig_l=$(($5 - $sig_h * 65536))
  146. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  147. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  148. }
  149. generate_binary()
  150. # $1 - language code ('en', 'cz'...)
  151. {
  152. echo "lang=$(tput setaf 2)$1$(tput sgr 0)" >&2
  153. #remove output and temporary files
  154. rm -f lang_$1.bin
  155. rm -f lang_$1.tmp
  156. rm -f lang_$1.dat
  157. LNG=$1
  158. #check lang dictionary
  159. python3 lang-check.py $1 #--no-warning
  160. #create lang_xx.tmp - different processing for 'en' language
  161. if [[ "$1" = "en" || ! -f "lang_en.max" ]]; then
  162. #remove comments and empty lines
  163. cat lang_en.txt | sed '/^$/d;/^#/d'
  164. #calculate number of strings
  165. count=$(grep -c '^"' lang_en.txt)
  166. echo "count="$count >&2
  167. #Calculate the number of strings and save to temporary file
  168. echo $count >lang_en.cnt
  169. #read the allowed maxsize from "../Firmware/config.h" and save to temporary file
  170. maxsize=$(($(grep "#define LANG_SIZE_RESERVED" $SRCDIR/Firmware/config.h|sed -e's/ */ /g' |cut -d ' ' -f3)))
  171. echo "maxsize="$maxsize >&2
  172. echo $maxsize >lang_en.max
  173. else
  174. #remove comments and empty lines, print lines with translated text only
  175. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  176. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  177. #create lang_xx.dat (binary text data file)
  178. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  179. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  180. #calculate number of strings
  181. count=$(grep -c '^"' lang_$1.tmp)
  182. echo "count="$count >&2
  183. # read string count of English and compare it with the translation
  184. encount=$(cat lang_en.cnt)
  185. if [ "$count" -eq "$encount" ]; then
  186. echo "$(tput setaf 2)OK:"$1"="$count"$(tput sgr 0) is equal to $(tput setaf 2)en="$encount"$(tput sgr 0)" >&2
  187. else
  188. echo "$(tput setaf 1)Error:"$1"="$count"$(tput sgr 0) is NOT equal to $(tput setaf 1)en="$encount"$(tput sgr 0)" >&2
  189. finish 1
  190. fi
  191. #calculate text data offset
  192. offs=$((16 + 2 * $count))
  193. echo "offs="$offs >&2
  194. #calculate text data size
  195. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  196. echo "size="$size >&2
  197. # read maxsize and compare with the translation
  198. maxsize=$(cat lang_en.max)
  199. if [ "$size" -lt "$maxsize" ]; then
  200. free_space=$(($maxsize - $size))
  201. echo "$(tput setaf 2)OK:"$1"="$size"$(tput sgr 0) is less than $(tput setaf 2)"$maxsize"$(tput sgr 0). Free space:$(tput setaf 2)"$free_space"$(tput sgr 0)" >&2
  202. else
  203. echo "$(tput setaf 1)Error:"$1"="$size"$(tput sgr 0) is higer than $(tput setaf 3)"$maxsize"$(tput sgr 0)" >&2
  204. finish 1
  205. fi
  206. #write header with empty signature and checksum
  207. write_header $1 $size $count 0x0000 0x00000000
  208. #write offset table
  209. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  210. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  211. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  212. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  213. #write binary text data
  214. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  215. #write signature
  216. if [ "$1" != "en" ]; then
  217. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  218. fi
  219. #calculate and update checksum
  220. chsum=$(cat lang_$1.bin | xxd | cut -c11-49 | tr ' ' "\n" | sed '/^$/d' | awk 'BEGIN { sum = 0; } { sum += strtonum("0x"$1); if (sum > 0xffff) sum -= 0x10000; } END { printf("%x\n", sum); }')
  221. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  222. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  223. }
  224. if [ -z "$1" ]; then set 'all'; fi
  225. if [ "$1" = "all" ]; then
  226. generate_binary 'en'
  227. for lang in $LANGUAGES; do
  228. echo " Running : $lang" >&2
  229. generate_binary $lang
  230. done
  231. else
  232. generate_binary $1
  233. fi
  234. finish 0