locale_tool.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. # requires: gettext
  3. # Get into the project root
  4. cd $(dirname "${BASH_SOURCE}"); cd ..
  5. LANGUAGES=("all")
  6. for d in locale/*/ ; do
  7. [ -d "$d" ] || continue
  8. LANGUAGES+=($(basename "$d"))
  9. done
  10. function createTemplate ( ) {
  11. local VERSION=$(python -c 'from searxqt.version import __version__; print(__version__)')
  12. local CHARSET='UTF-8'
  13. if [ -f locale/searx-qt.pot ]; then
  14. rm locale/searx-qt.pot
  15. fi
  16. touch locale/searx-qt.pot
  17. find ./searxqt/ -type f -name '*.py' -exec xgettext --join-existing \
  18. --from-code=utf-8 -L python -d 'searx-qt' -o locale/searx-qt.pot {} \;
  19. #/usr/lib/python3.8/Tools/i18n/pygettext.py -d searx-qt -o locale/searx-qt.pot ./searxqt/
  20. replaceInPOT "PACKAGE VERSION" "$VERSION"
  21. replaceInPOT "CHARSET" "$CHARSET"
  22. }
  23. function replaceInPOT ( ) {
  24. sed -i -e "0,/$1/ s/$1/$2/" locale/searx-qt.pot
  25. }
  26. function updateLanguage ( ) {
  27. local language="$1"
  28. echo -n "$language: "
  29. msgmerge -N "locale/$language/LC_MESSAGES/searx-qt.po" locale/searx-qt.pot > "locale/$language/LC_MESSAGES/new.po"
  30. # TODO update the version in the header
  31. rm "locale/$language/LC_MESSAGES/searx-qt.po"
  32. mv "locale/$language/LC_MESSAGES/new.po" "locale/$language/LC_MESSAGES/searx-qt.po"
  33. }
  34. function updateAllLanguages ( ) {
  35. for langCode in ${LANGUAGES[@]:1} ; do
  36. updateLanguage "$langCode"
  37. done
  38. }
  39. function compileLanguage ( ) {
  40. local language="$1"
  41. echo -n "$language: "
  42. msgfmt -o "locale/$language/LC_MESSAGES/searx-qt.mo" "locale/$language/LC_MESSAGES/searx-qt"
  43. echo "done."
  44. }
  45. function compileAllLanguages ( ) {
  46. for langCode in ${LANGUAGES[@]:1} ; do
  47. compileLanguage "$langCode"
  48. done
  49. }
  50. function requireValidLanguageArg ( ) {
  51. if [ ! $# -eq 2 ]; then
  52. echo "Please supply a valid second argument"
  53. exit 2
  54. fi
  55. if [[ ! " ${LANGUAGES[@]} " =~ " ${2} " ]]; then
  56. echo "Language '$2' not found."
  57. exit 2
  58. fi
  59. }
  60. HELPTEXT='Valid options:
  61. -c --create-template
  62. Create searx-qt.pot
  63. -u --update-language LANGUAGE
  64. Update the .po file of a specific LANGUAGE.
  65. Set LANGUAGE to "all" to update all languages.
  66. -m --compile-language LANGUAGE
  67. Compile .mo file for a specific LANGUAGE.
  68. Set LANGUAGE to "all" to compile all languages.
  69. -l --list
  70. Prints all available LANGUAGEs.
  71. '
  72. option="$1"
  73. case $option in
  74. -c|--create-template)
  75. echo "[Create tempalte]"
  76. createTemplate
  77. echo "Done."
  78. ;;
  79. -u|--update-language)
  80. requireValidLanguageArg $@
  81. echo "[Update language] $2"
  82. if [ "$2" == "all" ]; then
  83. updateAllLanguages
  84. else
  85. updateLanguage "$2"
  86. fi
  87. ;;
  88. -m|--compile-language)
  89. requireValidLanguageArg $@
  90. echo "[Compile language] $2"
  91. if [ "$2" == "all" ]; then
  92. compileAllLanguages
  93. else
  94. compileLanguage "$2"
  95. fi
  96. ;;
  97. -l|--list)
  98. echo "Supported languages:"
  99. for langCode in ${LANGUAGES[@]:1}; do
  100. echo " $langCode"
  101. done
  102. ;;
  103. *)
  104. echo "$HELPTEXT"
  105. exit 1
  106. esac
  107. exit 0