lang-export.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. #
  3. # lang-export.sh - multi-language support script
  4. # for generating lang_xx.po
  5. #
  6. # relative path to source folder
  7. SRCDIR="../Firmware"
  8. # selected language is 1st argument (cz, de, ...)
  9. LNG=$1
  10. # if no arguments, 'all' is selected (all po and also pot will be generated)
  11. if [ -z "$LNG" ]; then LNG=all; fi
  12. # if 'all' is selected, script will generate all po files and also pot file
  13. if [ "$LNG" = "all" ]; then
  14. ./lang-export.sh en
  15. ./lang-export.sh cz
  16. ./lang-export.sh de
  17. ./lang-export.sh es
  18. ./lang-export.sh fr
  19. ./lang-export.sh it
  20. ./lang-export.sh pl
  21. exit 0
  22. fi
  23. # language code (iso639-1) is equal to LNG
  24. LNGISO=$LNG
  25. # exception for 'cz' (code='cs')
  26. if [ "$LNG" = "cz" ]; then LNGISO=cs; fi
  27. # po/pot creation/revision date
  28. DATE=$(date)
  29. # if 'en' is selected, generate pot instead of po
  30. if [ "$LNG" = "en" ]; then
  31. INFILE=lang_en.txt
  32. OUTFILE=po/Firmware.pot
  33. LNGNAME="English"
  34. else
  35. # language name in english
  36. LNGNAME=$(\
  37. case "$LNG" in
  38. *cz*) echo "Czech" ;;
  39. *de*) echo "German" ;;
  40. *es*) echo "Spanish" ;;
  41. *fr*) echo "French" ;;
  42. *it*) echo "Italian" ;;
  43. *pl*) echo "Polish" ;;
  44. esac)
  45. # unknown language - error
  46. if [ -z "LNGNAME" ]; then
  47. echo "Invalid argument '$LNG'."
  48. exit 1
  49. fi
  50. INFILE=lang_en_$LNG.txt
  51. OUTFILE=po/Firmware_$LNGISO.po
  52. fi
  53. # remove output file if exists
  54. if [ -e $OUTFILE ]; then rm -f -v $OUTFILE; fi
  55. echo "lang-export.sh started"
  56. #total strings
  57. CNTTXT=$(grep '^#' -c $INFILE)
  58. #not translated strings
  59. CNTNT=$(grep '^\"\\x00\"' -c $INFILE)
  60. echo " $CNTTXT texts, $CNTNT not translated"
  61. # list .cpp, .c and .h files from source folder
  62. SRCFILES=$(ls "$SRCDIR"/*.cpp "$SRCDIR"/*.c "$SRCDIR"/*.h)
  63. echo " selected language=$LNGNAME"
  64. # write po/pot header
  65. (
  66. echo "# Translation of Prusa-Firmware into $LNGNAME."
  67. echo "#"
  68. echo 'msgid ""'
  69. echo 'msgstr ""'
  70. echo '"MIME-Version: 1.0\n"'
  71. echo '"Content-Type: text/plain; charset=UTF-8\n"'
  72. echo '"Content-Transfer-Encoding: 8bit\n"'
  73. echo '"Language: '$LNGISO'\n"'
  74. echo '"Project-Id-Version: Prusa-Firmware\n"'
  75. echo '"POT-Creation-Date: '$DATE'\n"'
  76. echo '"PO-Revision-Date: '$DATE'\n"'
  77. echo '"Language-Team: \n"'
  78. echo '"X-Generator: Poedit 2.0.7\n"'
  79. echo '"X-Poedit-SourceCharset: UTF-8\n"'
  80. echo '"Last-Translator: \n"'
  81. echo '"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"'
  82. echo
  83. ) >$OUTFILE
  84. #loop over all messages
  85. s0=''
  86. s1=''
  87. s2=''
  88. num=1
  89. (cat $INFILE | sed "s/\\\\/\\\\\\\\/g" | while read -r s; do
  90. if [ "$s" = "" ]; then
  91. echo " processing $num of $CNTTXT" >&2
  92. # write po/pot item
  93. (
  94. if [ -z "$s2" ]; then s2=$s1; s1=$s0; s0='""'; fi
  95. search=$(/bin/echo -e "$s1")
  96. found=$(grep -m1 -n -F "$search" $SRCFILES | head -n1 | cut -f1-2 -d':' | sed "s/^.*\///")
  97. echo "$s2" | sed 's/ c=0//;s/ r=0//;s/^#/# /'
  98. echo "#: $found"
  99. /bin/echo -e "msgid $s1"
  100. if [ "$s0" = "\"\\\\x00\"" ]; then
  101. echo 'msgstr ""'
  102. else
  103. /bin/echo -e "msgstr $s0"
  104. fi
  105. echo
  106. )
  107. num=$((num+1))
  108. fi
  109. s2=$s1
  110. s1=$s0
  111. s0=$s
  112. done >>$OUTFILE) 2>&1
  113. #replace LF with CRLF
  114. sync
  115. sed -i 's/$/\r/' $OUTFILE
  116. echo "lang-export.sh finished"
  117. exit 0