functions 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. # Midnight Commander - common functions for playing with translations.
  3. #
  4. # Copyright (C) 2013
  5. # The Free Software Foundation, Inc.
  6. #
  7. # Written by:
  8. # Slava Zanko <slavazanko@gmail.com>, 2013
  9. #
  10. # This file is part of the Midnight Commander.
  11. #
  12. # The Midnight Commander is free software: you can redistribute it
  13. # and/or modify it under the terms of the GNU General Public License as
  14. # published by the Free Software Foundation, either version 3 of the License,
  15. # or (at your option) any later version.
  16. #
  17. # The Midnight Commander is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. #*** include section (source functions, for example) *******************
  25. #*** file scope functions **********************************************
  26. # ----------------------------------------------------------------------
  27. ## Returns name of working directory.
  28. ##
  29. ## @param sync_file_name file name which will be sync'ed with Transifex
  30. ## @return name of working directory
  31. getSyncDirName() {
  32. sync_file_name=$1; shift
  33. echo "${SYNC_TX_CURRENT_DIR}/var.d/${sync_file_name}"
  34. }
  35. # ----------------------------------------------------------------------
  36. ## Returns config file name as full path.
  37. ##
  38. ## @param sync_file_name file name which will be sync'ed with Transifex
  39. ## @param config_file_name config file name
  40. ## @return config file name
  41. getConfigFile() {
  42. sync_file_name=$1; shift
  43. config_file_name=$1; shift
  44. echo "${MC_SOURCE_ROOT_DIR}/maint/utils/sync-transifex/config.d/${sync_file_name}/${config_file_name}"
  45. }
  46. # ----------------------------------------------------------------------
  47. ## Returns name of working directory. Init the directory, if needed.
  48. ##
  49. ## @param sync_file_name file name which will be sync'ed with Transifex
  50. ## @return name of working directory
  51. initSyncDirIfNeeded() {
  52. sync_file_name=$1; shift
  53. sync_dir_name=$(getSyncDirName "${sync_file_name}")
  54. [ ! -e "${sync_dir_name}" ] && mkdir -p "${sync_dir_name}/.tx"
  55. cp \
  56. "$(getConfigFile ${sync_file_name} 'tx.config')" \
  57. "${sync_dir_name}/.tx/config"
  58. echo "${sync_dir_name}"
  59. }
  60. # ----------------------------------------------------------------------
  61. ## Convert file from plain text to POT-file format.
  62. ##
  63. ## @param source_file path to plain text file
  64. ## @param destination_file path to po-file
  65. convertFromTextToPo() {
  66. source_file=$1; shift
  67. destination_file=$1; shift
  68. po4a-gettextize -f text -m "${source_file}" -p "${destination_file}"
  69. }
  70. # ----------------------------------------------------------------------
  71. ## Send POT-file to Transifex.
  72. ##
  73. ## @param tx_work_dir working directory where placed the POT file and
  74. ## a config-file for tx utility
  75. sendSourceToTransifex() {
  76. tx_work_dir=$1; shift
  77. tx -r "${tx_work_dir}" push -s
  78. }
  79. # ----------------------------------------------------------------------
  80. ## Reveive translations from Transifex.
  81. ##
  82. ## @param tx_work_dir working directory where placed the POT file and
  83. ## a config-file for tx utility
  84. receiveTranslationsFromTransifex() {
  85. tx_work_dir=$1; shift
  86. pushd "${tx_work_dir}" >/dev/null
  87. tx pull --all --force
  88. popd >/dev/null
  89. }
  90. # ----------------------------------------------------------------------
  91. ## Create po4a.cfg file.
  92. ##
  93. ## @param sync_file_name file name which will be sync'ed with Transifex
  94. createPo4A() {
  95. sync_file_name=$1; shift
  96. sync_dir_name=$(getSyncDirName "${sync_file_name}")
  97. [ ! -e "${sync_dir_name}" ] && mkdir -p "${sync_dir_name}"
  98. pushd $sync_dir_name >/dev/null
  99. translations=$(ls *.po| sed -e 's/.po//g'| tr '\n' ' ')
  100. popd >/dev/null
  101. sed -e 's!@srcdir@!'"${MC_SOURCE_ROOT_DIR}"'!g' \
  102. -e 's!@translations@!'"${translations}"'!g' \
  103. "$(getConfigFile ${sync_file_name} 'po4a.cfg')" \
  104. > "${sync_dir_name}/po4a.cfg"
  105. }
  106. # ----------------------------------------------------------------------
  107. ## Create po4a.cfg file.
  108. ##
  109. ## @param sync_dir_name working directory where placed translations
  110. ## @param sync_file_name file name which will be sync'ed with Transifex
  111. convertFromPoToText() {
  112. sync_dir_name=$1; shift
  113. sync_file_name=$1; shift
  114. po4a -k 0 -q \
  115. -M UTF-8 -L UTF-8 \
  116. --variable docfile="${sync_file_name}" \
  117. "${sync_dir_name}/po4a.cfg"
  118. }
  119. # ----------------------------------------------------------------------
  120. #*** main code *********************************************************
  121. SYNC_TX_CURRENT_DIR=${SYNC_TX_CURRENT_DIR:-$(pwd)}