lang-export.sh 3.1 KB

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