translate 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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}.mo ]; then
  83. if [ -f locale/${lang}/${COMMAND_NAME}.json ]; then
  84. i18next-conv -l ${lang} -s locale/${lang}/${COMMAND_NAME}.json -t locale/${lang}/${COMMAND_NAME}.mo
  85. fi
  86. fi
  87. fi
  88. # install the mo
  89. if [ -f locale/${lang}/${COMMAND_NAME}.mo ]; then
  90. cp locale/${lang}/${COMMAND_NAME}.mo /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  91. fi
  92. done
  93. done
  94. }
  95. function uninstall_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. if [ -f /usr/share/locale/${lang}/${COMMAND_NAME}.mo ]; then
  102. rm /usr/share/locale/${lang}/${COMMAND_NAME}.mo
  103. fi
  104. done
  105. done
  106. }
  107. if [[ $1 == "make" ]]; then
  108. install_i18next-conv
  109. create_translation_files
  110. exit 0
  111. fi
  112. if [[ $1 == "install" ]]; then
  113. install_i18next-conv
  114. install_translations
  115. exit 0
  116. fi
  117. if [[ $1 == "uninstall" ]]; then
  118. uninstall_translations
  119. exit 0
  120. fi
  121. exit 1