lang-build.sh 7.1 KB

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