translate 4.6KB

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