update-translations.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  16. source "${KUBE_ROOT}/hack/lib/util.sh"
  17. KUBECTL_FILES="pkg/kubectl/cmd/*.go pkg/kubectl/cmd/*/*.go"
  18. generate_pot="false"
  19. generate_mo="false"
  20. while getopts "hf:xg" opt; do
  21. case ${opt} in
  22. h)
  23. echo "$0 [-f files] [-x] [-g]"
  24. echo " -f <file-path>: Files to process"
  25. echo " -x extract strings to a POT file"
  26. echo " -g sort .po files and generate .mo files"
  27. exit 0
  28. ;;
  29. f)
  30. KUBECTL_FILES="${OPTARG}"
  31. ;;
  32. x)
  33. generate_pot="true"
  34. ;;
  35. g)
  36. generate_mo="true"
  37. ;;
  38. \?)
  39. echo "[-f <files>] -x -g" >&2
  40. exit 1
  41. ;;
  42. esac
  43. done
  44. if ! which go-xgettext > /dev/null; then
  45. echo 'Can not find go-xgettext, install with:'
  46. echo 'go get github.com/gosexy/gettext/go-xgettext'
  47. exit 1
  48. fi
  49. if ! which msgfmt > /dev/null; then
  50. echo 'Can not find msgfmt, install with:'
  51. echo 'apt-get install gettext'
  52. exit 1
  53. fi
  54. if [[ "${generate_pot}" == "true" ]]; then
  55. echo "Extracting strings to POT"
  56. go-xgettext -k=i18n.T "${KUBECTL_FILES}" > tmp.pot
  57. perl -pi -e 's/CHARSET/UTF-8/' tmp.pot
  58. perl -pi -e 's/\\\(/\\\\\(/g' tmp.pot
  59. perl -pi -e 's/\\\)/\\\\\)/g' tmp.pot
  60. kube::util::ensure-temp-dir
  61. if msgcat -s tmp.pot > "${KUBE_TEMP}/template.pot"; then
  62. mv "${KUBE_TEMP}/template.pot" translations/kubectl/template.pot
  63. rm tmp.pot
  64. else
  65. echo "Failed to update template.pot"
  66. exit 1
  67. fi
  68. fi
  69. if [[ "${generate_mo}" == "true" ]]; then
  70. echo "Generating .po and .mo files"
  71. for x in translations/*/*/*/*.po; do
  72. msgcat -s "${x}" > tmp.po
  73. mv tmp.po "${x}"
  74. echo "generating .mo file for: ${x}"
  75. msgfmt "${x}" -o "$(dirname "${x}")/$(basename "${x}" .po).mo"
  76. done
  77. fi
  78. ./hack/generate-bindata.sh