translate 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # A script to create and install translations for commands
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. # languages to translate into
  31. language=( fr de es )
  32. COMMAND_FILES=src/${PROJECT_NAME}*
  33. function install_i18next-conv {
  34. if [ ! -f /usr/bin/i18next-conv ]; then
  35. sudo apt-get install -y curl npm
  36. curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
  37. sudo apt-get install -y nodejs
  38. sudo npm install i18next-conv -g
  39. fi
  40. }
  41. function create_translation_files {
  42. if [ ! -d /tmp/${PROJECT_NAME} ]; then
  43. mkdir -p /tmp/${PROJECT_NAME}
  44. fi
  45. for f in $COMMAND_FILES
  46. do
  47. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  48. bash --dump-po-strings src/${COMMAND_NAME} | xgettext -L PO -o /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -
  49. if [ -f /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot ]; then
  50. for lang in "${language[@]}"
  51. do
  52. if [ ! -d locale/${lang} ]; then
  53. mkdir -p locale/${lang}
  54. fi
  55. if [ ! -f locale/${lang}/${COMMAND_NAME}.json ]; then
  56. # create po file
  57. echo "Creating ${lang} Translation file for ${COMMAND_NAME}..."
  58. msginit -l ${lang} -i /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -o locale/${lang}/${COMMAND_NAME}.po
  59. # convert po to json
  60. if [ -f /usr/bin/i18next-conv ]; then
  61. if [ -f locale/${lang}/${COMMAND_NAME}.po ]; then
  62. if [ ! -f locale/${lang}/${COMMAND_NAME}.json ]; then
  63. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.po -t locale/${lang}/${COMMAND_NAME}.json
  64. fi
  65. fi
  66. fi
  67. rm locale/${lang}/${COMMAND_NAME}.po
  68. fi
  69. done
  70. rm /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot
  71. fi
  72. done
  73. }
  74. function install_translations {
  75. for f in $COMMAND_FILES
  76. do
  77. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  78. for lang in "${language[@]}"
  79. do
  80. # convert json to mo
  81. if [ -f /usr/bin/i18next-conv ]; then
  82. if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
  83. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  84. fi
  85. fi
  86. done
  87. done
  88. }
  89. function uninstall_translations {
  90. for f in $COMMAND_FILES
  91. do
  92. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  93. for lang in "${language[@]}"
  94. do
  95. if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
  96. rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  97. fi
  98. done
  99. done
  100. }
  101. if [[ $1 == "make" ]]; then
  102. install_i18next-conv
  103. create_translation_files
  104. exit 0
  105. fi
  106. if [[ $1 == "install" ]]; then
  107. install_i18next-conv
  108. install_translations
  109. exit 0
  110. fi
  111. if [[ $1 == "uninstall" ]]; then
  112. uninstall_translations
  113. exit 0
  114. fi
  115. exit 1