export-strings.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. # SPDX-License-Identifier: MIT
  3. #
  4. # Copyright © 2012-2022 David Woodhouse <dwmw2@infradead.org>
  5. #
  6. # OpenConnect export-strings.sh
  7. #
  8. # Export strings for translation in NetworkManager-openconnect
  9. #
  10. OUTFILE="$1"
  11. POTFILE="$2"
  12. if [ -z "${POTFILE}" -o -z "${OUTFILE}" ]; then
  13. echo "Usage: $0 <NetworkManager-openconnect/openconnect-strings.txt> <openconnect.pot>"
  14. exit 1
  15. fi
  16. if ! grep -q "This file contains strings from the OpenConnect VPN client" "${OUTFILE}"; then
  17. echo "Error: ${OUTFILE} is not the NetworkManager-openconnect openconnect-strings.txt file."
  18. exit 1
  19. fi
  20. if ! grep -q "Project-Id-Version: openconnect " "${POTFILE}"; then
  21. echo "Error: ${POTFILE} is not a valid openconnect .pot file."
  22. exit 1
  23. fi
  24. COMMIT="$(git rev-parse HEAD)"
  25. if ! echo ${COMMIT} | egrep -q "[a-f0-9]{40}"; then
  26. echo "Error: Failed to fetch commit ID from git"
  27. exit 1
  28. fi
  29. # Truncate to ten characters
  30. COMMIT="${COMMIT:0:10}"
  31. GITWEB="https://git.infradead.org/users/dwmw2/openconnect.git/blob/${COMMIT}:/"
  32. GENFILE=$(mktemp "${OUTFILE}.XXXXXX")
  33. if [ -z "${GENFILE}" ]; then
  34. echo "Error: Failed to create temporary file"
  35. exit 1
  36. fi
  37. trap 'rm -f "${GENFILE}"' EXIT
  38. cat >$GENFILE <<EOF
  39. This file contains strings from the OpenConnect VPN client, found at
  40. https://www.infradead.org/openconnect/ and browsable in gitweb at
  41. https://git.infradead.org/users/dwmw2/openconnect.git
  42. We do this because NetworkManager-openconnect authentication dialog
  43. uses a lot of strings from libopenconnect, which also need to be
  44. translated too if the user is to have a fully localised experience.
  45. For translators looking to see source comments in their original context
  46. in order to translate them properly, the URLs by each one will give a
  47. link to the original source code.
  48. EOF
  49. cat "${POTFILE}" |
  50. while read -r line; do
  51. case "$line" in
  52. "#:"*)
  53. echo >>$GENFILE
  54. # FIXME: If it was already in openconnect-strings.txt can we keep the
  55. # previous URL instead of using the latest commit, to reduce churn?
  56. for src in ${line###: }; do
  57. echo "// ${GITWEB}${src%%:*}#l${src##*:}" >>$GENFILE
  58. done
  59. real_strings=yes
  60. ;;
  61. "msgid "*)
  62. if [ "$real_strings" = "yes" ]; then
  63. echo -n "_(${line##msgid }" >>$GENFILE
  64. in_msgid=yes
  65. fi
  66. ;;
  67. "msgstr "*|"")
  68. if [ "$in_msgid" = "yes" ]; then
  69. in_msgid=no
  70. echo ");" >>$GENFILE
  71. fi
  72. ;;
  73. *)
  74. if [ "$in_msgid" = "yes" ]; then
  75. echo >>$GENFILE
  76. echo -n "$line" >>$GENFILE
  77. fi
  78. ;;
  79. esac
  80. done
  81. MESSAGES=$(grep -c "^_(" ${GENFILE})
  82. echo "Got $MESSAGES messages from openconnect.pot"
  83. if [ "$MESSAGES" -lt 100 ]; then
  84. echo "Fewer than 100 messages? Something went wrong"
  85. exit 1
  86. fi
  87. # Ignore differences in the gitweb URLs; those will change for every commit.
  88. NEWSHA=$(grep -v ^// ${GENFILE} | sha1sum)
  89. OLDSHA=$(grep -v ^// ${OUTFILE} | sha1sum)
  90. if [ "${NEWSHA}" != "${OLDSHA}" ]; then
  91. echo New strings in openconnect-strings.txt
  92. mv "${GENFILE}" "${OUTFILE}"
  93. else
  94. echo No new strings. Not changing openconnect-strings.txt
  95. fi