123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. SUDO=''
  35. if [ -f /usr/bin/sudo ]; then
  36. SUDO='sudo'
  37. fi
  38. if [ -f /usr/sbin/sudo ]; then
  39. SUDO='sudo'
  40. fi
  41. if [ ! -f /usr/bin/i18next-conv ]; then
  42. ${SUDO} apt-get install -y curl npm
  43. curl -sL https://deb.nodesource.com/setup_0.12 | ${SUDO} bash -
  44. ${SUDO} apt-get install -y nodejs
  45. ${SUDO} npm install i18next-conv -g
  46. fi
  47. }
  48. function create_translation_files {
  49. if [ ! -d /tmp/${PROJECT_NAME} ]; then
  50. mkdir -p /tmp/${PROJECT_NAME}
  51. fi
  52. for f in $COMMAND_FILES
  53. do
  54. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  55. bash --dump-po-strings src/${COMMAND_NAME} | xgettext -L PO -o /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -
  56. if [ -f /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot ]; then
  57. for lang in "${language[@]}"
  58. do
  59. if [ ! -d locale/${lang} ]; then
  60. mkdir -p locale/${lang}
  61. fi
  62. if [ ! -f locale/${lang}/${COMMAND_NAME}.json ]; then
  63. # create po file
  64. echo "Creating ${lang} Translation file for ${COMMAND_NAME}..."
  65. msginit -l ${lang} -i /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot -o locale/${lang}/${COMMAND_NAME}.po
  66. # convert po to json
  67. if [ -f /usr/bin/i18next-conv ]; then
  68. if [ -f locale/${lang}/${COMMAND_NAME}.po ]; then
  69. if [ ! -f locale/${lang}/${COMMAND_NAME}.json ]; then
  70. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.po -t locale/${lang}/${COMMAND_NAME}.json
  71. fi
  72. fi
  73. fi
  74. rm locale/${lang}/${COMMAND_NAME}.po
  75. fi
  76. done
  77. rm /tmp/${PROJECT_NAME}/${COMMAND_NAME}.pot
  78. fi
  79. done
  80. }
  81. function install_translations {
  82. for f in $COMMAND_FILES
  83. do
  84. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  85. for lang in "${language[@]}"
  86. do
  87. # convert json to mo
  88. if [ -f /usr/bin/i18next-conv ]; then
  89. if [ ! -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  90. if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
  91. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t locale/${lang}/${COMMAND_NAME}.mo
  92. fi
  93. fi
  94. fi
  95. # install the mo
  96. if [ -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  97. cp locale/${lang}/${COMMAND_NAME}.mo /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  98. fi
  99. done
  100. done
  101. }
  102. function uninstall_translations {
  103. for f in $COMMAND_FILES
  104. do
  105. COMMAND_NAME=$(echo $f | awk -F '/' '{print $2}')
  106. for lang in "${language[@]}"
  107. do
  108. if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
  109. rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  110. fi
  111. done
  112. done
  113. }
  114. if [[ $1 == "make" ]]; then
  115. install_i18next-conv
  116. create_translation_files
  117. exit 0
  118. fi
  119. if [[ $1 == "install" ]]; then
  120. install_translations
  121. exit 0
  122. fi
  123. if [[ $1 == "uninstall" ]]; then
  124. uninstall_translations
  125. exit 0
  126. fi
  127. exit 1