update-translations.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. # Copyright 2017 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script updates `translations/kubectl/template.pot` for
  16. # `pkg/kubectl/cmd/*.go pkg/kubectl/cmd/*/*.go`.
  17. # Usage: `update-translations.sh`.
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/util.sh"
  20. KUBECTL_FILES="pkg/kubectl/cmd/*.go pkg/kubectl/cmd/*/*.go"
  21. generate_pot="false"
  22. generate_mo="false"
  23. while getopts "hf:xg" opt; do
  24. case ${opt} in
  25. h)
  26. echo "$0 [-f files] [-x] [-g]"
  27. echo " -f <file-path>: Files to process"
  28. echo " -x extract strings to a POT file"
  29. echo " -g sort .po files and generate .mo files"
  30. exit 0
  31. ;;
  32. f)
  33. KUBECTL_FILES="${OPTARG}"
  34. ;;
  35. x)
  36. generate_pot="true"
  37. ;;
  38. g)
  39. generate_mo="true"
  40. ;;
  41. \?)
  42. echo "[-f <files>] -x -g" >&2
  43. exit 1
  44. ;;
  45. esac
  46. done
  47. if ! which go-xgettext > /dev/null; then
  48. echo 'Can not find go-xgettext, install with:'
  49. echo 'go get github.com/gosexy/gettext/go-xgettext'
  50. exit 1
  51. fi
  52. if ! which msgfmt > /dev/null; then
  53. echo 'Can not find msgfmt, install with:'
  54. echo 'apt-get install gettext'
  55. exit 1
  56. fi
  57. if [[ "${generate_pot}" == "true" ]]; then
  58. echo "Extracting strings to POT"
  59. go-xgettext -k=i18n.T "${KUBECTL_FILES}" > tmp.pot
  60. perl -pi -e 's/CHARSET/UTF-8/' tmp.pot
  61. perl -pi -e 's/\\\(/\\\\\(/g' tmp.pot
  62. perl -pi -e 's/\\\)/\\\\\)/g' tmp.pot
  63. kube::util::ensure-temp-dir
  64. if msgcat -s tmp.pot > "${KUBE_TEMP}/template.pot"; then
  65. mv "${KUBE_TEMP}/template.pot" translations/kubectl/template.pot
  66. rm tmp.pot
  67. else
  68. echo "Failed to update template.pot"
  69. exit 1
  70. fi
  71. fi
  72. if [[ "${generate_mo}" == "true" ]]; then
  73. echo "Generating .po and .mo files"
  74. for x in translations/*/*/*/*.po; do
  75. msgcat -s "${x}" > tmp.po
  76. mv tmp.po "${x}"
  77. echo "generating .mo file for: ${x}"
  78. msgfmt "${x}" -o "$(dirname "${x}")/$(basename "${x}" .po).mo"
  79. done
  80. fi
  81. ./hack/generate-bindata.sh