123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2016 Bob Mottram <bob@freedombone.net>
  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=( af sq ar eu be bs bg ca hr zh_cn zh_tw cs da nl en_us et fa fil fi fr fr_CH fr_BE fr_ca ga gl ka de de_du el gu he hi hu is id it ja kn km ko lo lt lv ml ms mi_tn mi_wwow mn no no_gr nn pl pt pt_br ro ru sm sr sk sl so es sv tl ta th to tr uk vi )
  32. MY_EMAIL_ADDRESS='bob@freedombone.net'
  33. COMMAND_FILES=src/${PROJECT_NAME}*
  34. function install_i18next-conv {
  35. SUDO=''
  36. if [ -f /usr/bin/sudo ]; then
  37. SUDO='sudo'
  38. fi
  39. if [ -f /usr/sbin/sudo ]; then
  40. SUDO='sudo'
  41. fi
  42. if [ ! -f /usr/bin/i18next-conv ]; then
  43. ${SUDO} apt-get install -y curl npm
  44. curl -sL https://deb.nodesource.com/setup_0.12 | ${SUDO} bash -
  45. ${SUDO} apt-get install -y nodejs
  46. ${SUDO} npm install i18next-conv -g
  47. fi
  48. }
  49. function create_translation_files {
  50. create_arg=$1
  51. if [ ! -d /tmp/${PROJECT_NAME} ]; then
  52. mkdir -p /tmp/${PROJECT_NAME}
  53. fi
  54. for f in $COMMAND_FILES
  55. do
  56. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  57. bash --dump-po-strings src/${COMMAND_NAME} | xgettext --msgid-bugs-address=$MY_EMAIL_ADDRESS -L PO -o /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -
  58. if [ -f /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot ]; then
  59. for lang in "${language[@]}"
  60. do
  61. if [ ! -d locale/${lang} ]; then
  62. mkdir -p locale/${lang}
  63. fi
  64. if [[ ! -f locale/${lang}/${COMMAND_NAME}.json || "$create_arg" == "overwrite" ]]; then
  65. # create po file
  66. echo "Creating ${lang} Translation file for ${COMMAND_NAME}..."
  67. msginit --no-translator -l ${lang} -i /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -o locale/${lang}/${COMMAND_NAME}.po
  68. echo 'testing'
  69. # convert po to json
  70. if [ -f /usr/bin/i18next-conv ]; then
  71. if [ -f locale/${lang}/${COMMAND_NAME}.po ]; then
  72. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.po -t locale/${lang}/${COMMAND_NAME}.json
  73. git add locale/${lang}/${COMMAND_NAME}.json
  74. fi
  75. fi
  76. rm locale/${lang}/${COMMAND_NAME}.po
  77. fi
  78. done
  79. rm /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot
  80. fi
  81. done
  82. }
  83. function add_all_translations {
  84. for lang in "${language[@]}"
  85. do
  86. git add locale/${lang}/*
  87. done
  88. }
  89. function remove_all_translations {
  90. for lang in "${language[@]}"
  91. do
  92. rm locale/${lang}/*
  93. done
  94. }
  95. function install_translations {
  96. for f in $COMMAND_FILES
  97. do
  98. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  99. for lang in "${language[@]}"
  100. do
  101. # convert json to mo
  102. if [ -f /usr/bin/i18next-conv ]; then
  103. if [ ! -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  104. if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
  105. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t locale/${lang}/${COMMAND_NAME}.mo
  106. git add locale/${lang}/${COMMAND_NAME}.mo
  107. fi
  108. fi
  109. fi
  110. # install the mo
  111. if [ -d /usr/share/locale/${lang} ]; then
  112. if [ -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  113. cp locale/${lang}/${COMMAND_NAME}.mo /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  114. fi
  115. fi
  116. done
  117. done
  118. }
  119. function uninstall_translations {
  120. for f in $COMMAND_FILES
  121. do
  122. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  123. for lang in "${language[@]}"
  124. do
  125. if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
  126. rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  127. fi
  128. done
  129. done
  130. }
  131. if [[ $1 == "translation"* ]]; then
  132. install_i18next-conv
  133. create_translation_files overwrite
  134. add_all_translations
  135. exit 0
  136. fi
  137. if [[ $1 == "remove"* ]]; then
  138. remove_all_translations
  139. exit 0
  140. fi
  141. if [[ $1 == "make" ]]; then
  142. install_i18next-conv
  143. create_translation_files
  144. add_all_translations
  145. exit 0
  146. fi
  147. if [[ $1 == "install" ]]; then
  148. install_translations
  149. exit 0
  150. fi
  151. if [[ $1 == "uninstall" ]]; then
  152. uninstall_translations
  153. exit 0
  154. fi
  155. exit 1