lang-build.sh 8.5 KB

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