translate 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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@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=( 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@robotics.uk.to'
  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 install_translations {
  84. for f in $COMMAND_FILES
  85. do
  86. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  87. for lang in "${language[@]}"
  88. do
  89. # convert json to mo
  90. if [ -f /usr/bin/i18next-conv ]; then
  91. if [ ! -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  92. if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
  93. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t locale/${lang}/${COMMAND_NAME}.mo
  94. git add locale/${lang}/${COMMAND_NAME}.mo
  95. fi
  96. fi
  97. fi
  98. # install the mo
  99. if [ -d /usr/share/locale/${lang} ]; then
  100. if [ -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  101. cp locale/${lang}/${COMMAND_NAME}.mo /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  102. fi
  103. fi
  104. done
  105. done
  106. }
  107. function uninstall_translations {
  108. for f in $COMMAND_FILES
  109. do
  110. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  111. for lang in "${language[@]}"
  112. do
  113. if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
  114. rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  115. fi
  116. done
  117. done
  118. }
  119. if [[ $1 == "translation"* ]]; then
  120. install_i18next-conv
  121. create_translation_files overwrite
  122. exit 0
  123. fi
  124. if [[ $1 == "make" ]]; then
  125. install_i18next-conv
  126. create_translation_files
  127. exit 0
  128. fi
  129. if [[ $1 == "install" ]]; then
  130. install_translations
  131. exit 0
  132. fi
  133. if [[ $1 == "uninstall" ]]; then
  134. uninstall_translations
  135. exit 0
  136. fi
  137. exit 1