locale_tool.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 locales/*/ ; 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 locales/searx-qt.pot ]; then
  14. rm locales/searx-qt.pot
  15. fi
  16. touch locales/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 locales/searx-qt.pot {} \;
  19. #/usr/lib/python3.8/Tools/i18n/pygettext.py -d searx-qt -o locales/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/" locales/searx-qt.pot
  25. }
  26. function updateLanguage ( ) {
  27. local language="$1"
  28. echo -n "$language: "
  29. msgmerge -N "locales/$language/LC_MESSAGES/searx-qt.po" locales/searx-qt.pot > "locales/$language/LC_MESSAGES/new.po"
  30. rm "locales/$language/LC_MESSAGES/searx-qt.po"
  31. mv "locales/$language/LC_MESSAGES/new.po" "locales/$language/LC_MESSAGES/searx-qt.po"
  32. }
  33. function updateAllLanguages ( ) {
  34. for langCode in ${LANGUAGES[@]:1} ; do
  35. updateLanguage "$langCode"
  36. done
  37. }
  38. function compileLanguage ( ) {
  39. local language="$1"
  40. echo -n "$language: "
  41. msgfmt -o "locales/$language/LC_MESSAGES/searx-qt.mo" "locales/$language/LC_MESSAGES/searx-qt"
  42. echo "done."
  43. }
  44. function compileAllLanguages ( ) {
  45. for langCode in ${LANGUAGES[@]:1} ; do
  46. compileLanguage "$langCode"
  47. done
  48. }
  49. function requireValidLanguageArg ( ) {
  50. if [ ! $# -eq 2 ]; then
  51. echo "Please supply a valid second argument"
  52. exit 2
  53. fi
  54. if [[ ! " ${LANGUAGES[@]} " =~ " ${2} " ]]; then
  55. echo "Language '$2' not found."
  56. exit 2
  57. fi
  58. }
  59. HELPTEXT='Valid options:
  60. -c --create-template
  61. Create searx-qt.pot
  62. -u --update-language LANGUAGE
  63. Update the .po file of a specific LANGUAGE.
  64. Set LANGUAGE to "all" to update all languages.
  65. -m --compile-language LANGUAGE
  66. Compile .mo file for a specific LANGUAGE.
  67. Set LANGUAGE to "all" to compile all languages.
  68. -l --list
  69. Prints all available LANGUAGEs.
  70. '
  71. option="$1"
  72. case $option in
  73. -c|--create-template)
  74. echo "[Create tempalte]"
  75. createTemplate
  76. echo "Done."
  77. ;;
  78. -u|--update-language)
  79. requireValidLanguageArg $@
  80. echo "[Update language] $2"
  81. if [ "$2" == "all" ]; then
  82. updateAllLanguages
  83. else
  84. updateLanguage "$2"
  85. fi
  86. ;;
  87. -m|--compile-language)
  88. requireValidLanguageArg $@
  89. echo "[Compile language] $2"
  90. if [ "$2" == "all" ]; then
  91. compileAllLanguages
  92. else
  93. compileLanguage "$2"
  94. fi
  95. ;;
  96. -l|--list)
  97. echo "Supported languages:"
  98. for langCode in ${LANGUAGES[@]:1}; do
  99. echo " $langCode"
  100. done
  101. ;;
  102. *)
  103. echo "$HELPTEXT"
  104. exit 1
  105. esac
  106. exit 0