lib_sxng_weblate.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. weblate.help(){
  4. cat <<EOF
  5. weblate.:
  6. push.translations: push translation changes from SearXNG to Weblate's counterpart
  7. to.translations: Update 'translations' branch with last additions from Weblate.
  8. EOF
  9. }
  10. TRANSLATIONS_WORKTREE="$CACHE/translations"
  11. weblate.translations.worktree() {
  12. # Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  13. # 'translations' from Weblate's counterpart (weblate) of the SearXNG
  14. # (origin).
  15. #
  16. # remote weblate https://translate.codeberg.org/git/searxng/searxng/
  17. ( set -e
  18. if ! git remote get-url weblate 2> /dev/null; then
  19. git remote add weblate https://translate.codeberg.org/git/searxng/searxng/
  20. fi
  21. if [ -d "${TRANSLATIONS_WORKTREE}" ]; then
  22. pushd .
  23. cd "${TRANSLATIONS_WORKTREE}"
  24. git reset --hard HEAD
  25. git pull origin translations
  26. popd
  27. else
  28. mkdir -p "${TRANSLATIONS_WORKTREE}"
  29. git worktree add "${TRANSLATIONS_WORKTREE}" translations
  30. fi
  31. )
  32. }
  33. weblate.to.translations() {
  34. # Update 'translations' branch of SearXNG (origin) with last additions from
  35. # Weblate.
  36. # 1. Check if Weblate is locked, if not die with error message
  37. # 2. On Weblate's counterpart (weblate), pull master and translations branch
  38. # from SearXNG (origin).
  39. # 3. Commit changes made in a Weblate object on Weblate's counterpart
  40. # (weblate).
  41. # 4. In translations worktree, merge changes of branch 'translations' from
  42. # remote 'weblate' and push it on branch 'translations' of 'origin'
  43. ( set -e
  44. pyenv.activate
  45. if [ "$(wlc lock-status)" != "locked: True" ]; then
  46. die 1 "weblate must be locked, currently: $(wlc lock-status)"
  47. fi
  48. # weblate: commit pending changes
  49. wlc pull
  50. wlc commit
  51. # get the translations in a worktree
  52. weblate.translations.worktree
  53. pushd "${TRANSLATIONS_WORKTREE}"
  54. git remote update weblate
  55. git merge weblate/translations
  56. git push
  57. popd
  58. )
  59. dump_return $?
  60. }
  61. weblate.translations.commit() {
  62. # Update 'translations' branch of SearXNG (origin) with last additions from
  63. # Weblate. Copy the changes to the master branch, compile translations and
  64. # create a commit in the local branch (master)
  65. local existing_commit_hash commit_body commit_message exitcode
  66. ( set -e
  67. pyenv.activate
  68. # lock change on weblate
  69. wlc lock
  70. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  71. weblate.translations.worktree
  72. existing_commit_hash=$(cd "${TRANSLATIONS_WORKTREE}"; git log -n1 --pretty=format:'%h')
  73. # pull weblate commits
  74. weblate.to.translations
  75. # copy the changes to the master branch
  76. cp -rv --preserve=mode,timestamps "${TRANSLATIONS_WORKTREE}/searx/translations" "searx"
  77. # compile translations
  78. build_msg BABEL 'compile translation catalogs into binary MO files'
  79. pybabel compile --statistics \
  80. -d "searx/translations"
  81. # update searx/data/translation_labels.json
  82. data.locales
  83. # git add/commit (no push)
  84. commit_body=$(cd "${TRANSLATIONS_WORKTREE}"; git log --pretty=format:'%h - %as - %aN <%ae>' "${existing_commit_hash}..HEAD")
  85. commit_message=$(echo -e "[l10n] update translations from Weblate\n\n${commit_body}")
  86. git add searx/translations
  87. git add searx/data/locales.json
  88. git commit -m "${commit_message}"
  89. )
  90. exitcode=$?
  91. ( # make sure to always unlock weblate
  92. set -e
  93. pyenv.cmd wlc unlock
  94. )
  95. dump_return $exitcode
  96. }
  97. weblate.push.translations() {
  98. # Push *translation changes* from SearXNG (origin) to Weblate's counterpart
  99. # (weblate).
  100. # In branch master of SearXNG (origin) check for meaningful changes in
  101. # folder 'searx/translations', commit changes on branch 'translations' and
  102. # at least, pull updated branches on Weblate's counterpart (weblate).
  103. # 1. Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  104. # 'translations' from remote 'weblate'.
  105. # 2. Stop if there is no meaningful change in the 'master' branch (origin),
  106. # compared to the 'translations' branch (weblate), otherwise ...
  107. # 3. Update 'translations' branch of SearXNG (origin) with last additions
  108. # from Weblate.
  109. # 5. Notify Weblate to pull updated 'master' & 'translations' branch.
  110. local messages_pot diff_messages_pot last_commit_hash last_commit_detail \
  111. exitcode
  112. messages_pot="${TRANSLATIONS_WORKTREE}/searx/translations/messages.pot"
  113. ( set -e
  114. pyenv.activate
  115. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  116. weblate.translations.worktree
  117. # update messages.pot in the master branch
  118. build_msg BABEL 'extract messages from source files and generate POT file'
  119. pybabel extract -F babel.cfg \
  120. -o "${messages_pot}" \
  121. "searx/"
  122. # stop if there is no meaningful change in the master branch
  123. diff_messages_pot=$(cd "${TRANSLATIONS_WORKTREE}";\
  124. git diff -- "searx/translations/messages.pot")
  125. if ! echo "$diff_messages_pot" | grep -qE "[\+\-](msgid|msgstr)"; then
  126. build_msg BABEL 'no changes detected, exiting'
  127. return 42
  128. fi
  129. return 0
  130. )
  131. exitcode=$?
  132. if [ "$exitcode" -eq 42 ]; then
  133. return 0
  134. fi
  135. if [ "$exitcode" -gt 0 ]; then
  136. return $exitcode
  137. fi
  138. (
  139. set -e
  140. pyenv.activate
  141. # lock change on weblate
  142. # weblate may add commit(s) since the call to "weblate.translations.worktree".
  143. # this is not a problem because after this line, "weblate.to.translations"
  144. # calls again "weblate.translations.worktree" which calls "git pull"
  145. wlc lock
  146. # save messages.pot in the translations branch for later
  147. pushd "${TRANSLATIONS_WORKTREE}"
  148. git stash push
  149. popd
  150. # merge weblate commits into the translations branch
  151. weblate.to.translations
  152. # restore messages.pot in the translations branch
  153. pushd "${TRANSLATIONS_WORKTREE}"
  154. git stash pop
  155. popd
  156. # update messages.po files in the master branch
  157. build_msg BABEL 'update existing message catalogs from POT file'
  158. pybabel update -N \
  159. -i "${messages_pot}" \
  160. -d "${TRANSLATIONS_WORKTREE}/searx/translations"
  161. # git add/commit/push
  162. last_commit_hash=$(git log -n1 --pretty=format:'%h')
  163. last_commit_detail=$(git log -n1 --pretty=format:'%h - %as - %aN <%ae>' "${last_commit_hash}")
  164. pushd "${TRANSLATIONS_WORKTREE}"
  165. git add searx/translations
  166. git commit \
  167. -m "[translations] update messages.pot and messages.po files" \
  168. -m "From ${last_commit_detail}"
  169. git push
  170. popd
  171. # notify weblate to pull updated master & translations branch
  172. wlc pull
  173. )
  174. exitcode=$?
  175. ( # make sure to always unlock weblate
  176. set -e
  177. pyenv.activate
  178. wlc unlock
  179. )
  180. dump_return $exitcode
  181. }