lang-build.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/bin/bash
  2. #
  3. # Version 1.0.2 Build 27
  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. #############################################################################
  61. #
  62. # Config:
  63. if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
  64. if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo "$(tput setaf 1)Config NG!$(tput sgr 0)" >&2; exit 1; fi
  65. if [ ! -z "$COMMUNITY_LANGUAGES" ]; then
  66. LANGUAGES+=" $COMMUNITY_LANGUAGES"
  67. fi
  68. #startup message
  69. echo "$(tput setaf 2)lang-build.sh started$(tput sgr 0)" >&2
  70. echo "lang-build languages:$(tput setaf 2)$LANGUAGES$(tput sgr 0)" >&2
  71. #awk code to format ui16 variables for dd
  72. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  73. #exiting function
  74. finish()
  75. {
  76. if [ $1 -eq 0 ]; then
  77. echo "$(tput setaf 2)lang-build.sh finished with success$(tput sgr 0)" >&2
  78. else
  79. echo "$(tput setaf 1)lang-build.sh finished with errors!$(tput sgr 0)" >&2
  80. fi
  81. exit $1
  82. }
  83. #returns hexadecial data for lang code
  84. lang_code_hex_data()
  85. # $1 - language code ('en', 'cz'...)
  86. {
  87. case "$1" in
  88. *en*) echo '\x6e\x65' ;;
  89. *cz*) echo '\x73\x63' ;;
  90. *de*) echo '\x65\x64' ;;
  91. *es*) echo '\x73\x65' ;;
  92. *fr*) echo '\x72\x66' ;;
  93. *it*) echo '\x74\x69' ;;
  94. *pl*) echo '\x6c\x70' ;;
  95. #Community language support
  96. #Dutch
  97. *nl*) echo '\x6c\x6e' ;;
  98. #Swedish
  99. *sv*) echo '\x76\x73' ;;
  100. #Danish
  101. *da*) echo '\x61\x64' ;;
  102. #Slovanian
  103. *sl*) echo '\x6c\x73' ;;
  104. #Hungarian
  105. *hu*) echo '\x75\x68' ;;
  106. #Luxembourgish
  107. *lb*) echo '\x62\x6c' ;;
  108. #Croatian
  109. *hr*) echo '\x72\x68' ;;
  110. #Lithuanian
  111. *lt*) echo '\x74\x6c' ;;
  112. #Romanian
  113. *ro*) echo '\x6f\x72' ;;
  114. #Use the 2 lines below as a template and replace 'qr' and `\x71\x72`
  115. ##New language
  116. # *qr*) echo '\x71\x72' ;;
  117. esac
  118. echo '??'
  119. }
  120. write_header()
  121. # $1 - lang
  122. # $2 - size
  123. # $3 - count
  124. # $4 - checksum
  125. # $5 - signature
  126. {
  127. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  128. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  129. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  130. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  131. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  132. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  133. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  134. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  135. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  136. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  137. sig_h=$(($5 / 65536))
  138. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  139. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  140. sig_l=$(($5 - $sig_h * 65536))
  141. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  142. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  143. }
  144. generate_binary()
  145. # $1 - language code ('en', 'cz'...)
  146. {
  147. echo "lang=$(tput setaf 2)$1$(tput sgr 0)" >&2
  148. #remove output and temporary files
  149. rm -f lang_$1.bin
  150. rm -f lang_$1.tmp
  151. rm -f lang_$1.dat
  152. LNG=$1
  153. #check lang dictionary
  154. ./lang-check.py $1 #--no-warning
  155. #create lang_xx.tmp - different processing for 'en' language
  156. if [[ "$1" = "en" || ! -f "lang_en.max" ]]; then
  157. #remove comments and empty lines
  158. cat lang_en.txt | sed '/^$/d;/^#/d'
  159. #calculate number of strings
  160. count=$(grep -c '^"' lang_en.txt)
  161. echo "count="$count >&2
  162. #Calculate the number of strings and save to temporary file
  163. echo $count >lang_en.cnt
  164. #read the allowed maxsize from "../Firmware/config.h" and save to temporary file
  165. maxsize=$(($(grep "#define LANG_SIZE_RESERVED" $SRCDIR/Firmware/config.h|sed -e's/ */ /g' |cut -d ' ' -f3)))
  166. echo "maxsize="$maxsize >&2
  167. echo $maxsize >lang_en.max
  168. else
  169. #remove comments and empty lines, print lines with translated text only
  170. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  171. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  172. #create lang_xx.dat (binary text data file)
  173. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  174. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  175. #calculate number of strings
  176. count=$(grep -c '^"' lang_$1.tmp)
  177. echo "count="$count >&2
  178. # read string count of English and compare it with the translation
  179. encount=$(cat lang_en.cnt)
  180. if [ "$count" -eq "$encount" ]; then
  181. echo "$(tput setaf 2)OK:"$1"="$count"$(tput sgr 0) is equal to $(tput setaf 2)en="$encount"$(tput sgr 0)" >&2
  182. else
  183. echo "$(tput setaf 1)Error:"$1"="$count"$(tput sgr 0) is NOT equal to $(tput setaf 1)en="$encount"$(tput sgr 0)" >&2
  184. finish 1
  185. fi
  186. #calculate text data offset
  187. offs=$((16 + 2 * $count))
  188. echo "offs="$offs >&2
  189. #calculate text data size
  190. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  191. echo "size="$size >&2
  192. # read maxsize and compare with the translation
  193. maxsize=$(cat lang_en.max)
  194. if [ "$size" -lt "$maxsize" ]; then
  195. free_space=$(($maxsize - $size))
  196. 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
  197. else
  198. echo "$(tput setaf 1)Error:"$1"="$size"$(tput sgr 0) is higer than $(tput setaf 3)"$maxsize"$(tput sgr 0)" >&2
  199. finish 1
  200. fi
  201. #write header with empty signature and checksum
  202. write_header $1 $size $count 0x0000 0x00000000
  203. #write offset table
  204. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  205. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  206. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  207. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  208. #write binary text data
  209. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  210. #write signature
  211. if [ "$1" != "en" ]; then
  212. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  213. fi
  214. #calculate and update checksum
  215. 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); }')
  216. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  217. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  218. }
  219. if [ -z "$1" ]; then set 'all'; fi
  220. if [ "$1" = "all" ]; then
  221. generate_binary 'en'
  222. for lang in $LANGUAGES; do
  223. echo " Running : $lang" >&2
  224. generate_binary $lang
  225. done
  226. else
  227. generate_binary $1
  228. fi
  229. finish 0