lang-export.sh 3.4 KB

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