lang-build.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #Romanian
  112. *ro*) echo '\x6f\x72' ;;
  113. #Use the 2 lines below as a template and replace 'qr' and `\x71\x72`
  114. ##New language
  115. # *qr*) echo '\x71\x72' ;;
  116. esac
  117. echo '??'
  118. }
  119. write_header()
  120. # $1 - lang
  121. # $2 - size
  122. # $3 - count
  123. # $4 - checksum
  124. # $5 - signature
  125. {
  126. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  127. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  128. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  129. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  130. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  131. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  132. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  133. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  134. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  135. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  136. sig_h=$(($5 / 65536))
  137. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  138. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  139. sig_l=$(($5 - $sig_h * 65536))
  140. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  141. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  142. }
  143. generate_binary()
  144. # $1 - language code ('en', 'cz'...)
  145. {
  146. echo "lang=$(tput setaf 2)$1$(tput sgr 0)" >&2
  147. #remove output and temporary files
  148. rm -f lang_$1.bin
  149. rm -f lang_$1.tmp
  150. rm -f lang_$1.dat
  151. LNG=$1
  152. #check lang dictionary
  153. ./lang-check.py $1 #--no-warning
  154. #create lang_xx.tmp - different processing for 'en' language
  155. if [[ "$1" = "en" || ! -f "lang_en.max" ]]; then
  156. #remove comments and empty lines
  157. cat lang_en.txt | sed '/^$/d;/^#/d'
  158. #calculate number of strings
  159. count=$(grep -c '^"' lang_en.txt)
  160. echo "count="$count >&2
  161. #Calculate the number of strings and save to temporary file
  162. echo $count >lang_en.cnt
  163. #read the allowed maxsize from "../Firmware/config.h" and save to temporary file
  164. maxsize=$(($(grep "#define LANG_SIZE_RESERVED" ../Firmware/config.h|sed -e's/ */ /g' |cut -d ' ' -f3)))
  165. echo "maxsize="$maxsize >&2
  166. echo $maxsize >lang_en.max
  167. else
  168. #remove comments and empty lines, print lines with translated text only
  169. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  170. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  171. #create lang_xx.dat (binary text data file)
  172. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  173. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  174. #calculate number of strings
  175. count=$(grep -c '^"' lang_$1.tmp)
  176. echo "count="$count >&2
  177. # read string count of English and compare it with the translation
  178. encount=$(cat lang_en.cnt)
  179. if [ "$count" -eq "$encount" ]; then
  180. echo "$(tput setaf 2)OK:"$1"="$count"$(tput sgr 0) is equal to $(tput setaf 2)en="$encount"$(tput sgr 0)" >&2
  181. else
  182. echo "$(tput setaf 1)Error:"$1"="$count"$(tput sgr 0) is NOT equal to $(tput setaf 1)en="$encount"$(tput sgr 0)" >&2
  183. finish 1
  184. fi
  185. #calculate text data offset
  186. offs=$((16 + 2 * $count))
  187. echo "offs="$offs >&2
  188. #calculate text data size
  189. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  190. echo "size="$size >&2
  191. # read maxsize and compare with the translation
  192. maxsize=$(cat lang_en.max)
  193. if [ "$size" -lt "$maxsize" ]; then
  194. free_space=$(($maxsize - $size))
  195. 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
  196. else
  197. echo "$(tput setaf 1)Error:"$1"="$size"$(tput sgr 0) is higer than $(tput setaf 3)"$maxsize"$(tput sgr 0)" >&2
  198. finish 1
  199. fi
  200. #write header with empty signature and checksum
  201. write_header $1 $size $count 0x0000 0x00000000
  202. #write offset table
  203. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  204. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  205. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  206. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  207. #write binary text data
  208. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  209. #write signature
  210. if [ "$1" != "en" ]; then
  211. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  212. fi
  213. #calculate and update checksum
  214. 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); }')
  215. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  216. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  217. }
  218. if [ -z "$1" ]; then set 'all'; fi
  219. if [ "$1" = "all" ]; then
  220. generate_binary 'en'
  221. for lang in $LANGUAGES; do
  222. echo " Running : $lang" >&2
  223. generate_binary $lang
  224. done
  225. else
  226. generate_binary $1
  227. fi
  228. finish 0