k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/update-translations.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2017 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # This script updates `staging/src/k8s.io/kubectl/pkg/util/i18n/translations/kubectl/template.pot` and 18 # generates/fixes .po and .mo files. 19 # Usage: `update-translations.sh`. 20 21 KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. 22 source "${KUBE_ROOT}/hack/lib/util.sh" 23 24 TRANSLATIONS="staging/src/k8s.io/kubectl/pkg/util/i18n/translations" 25 KUBECTL_FILES=() 26 KUBECTL_DEFAULT_LOCATIONS=( 27 "pkg/kubectl/cmd" 28 "staging/src/k8s.io/kubectl/pkg/cmd" 29 ) 30 KUBECTL_IGNORE_FILES_REGEX="cmd/kustomize/kustomize.go" 31 32 generate_pot="false" 33 generate_mo="false" 34 fix_translations="false" 35 36 while getopts "hf:xkg" opt; do 37 case ${opt} in 38 h) 39 echo "$0 [-f files] [-x] [-k] [-g]" 40 echo " -f <file-path>: Files to process" 41 echo " -x extract strings to a POT file" 42 echo " -k fix .po files; deprecate translations by marking them obsolete and supply default messages" 43 echo " -g sort .po files and generate .mo files" 44 exit 0 45 ;; 46 f) 47 KUBECTL_FILES+=("${OPTARG}") 48 ;; 49 x) 50 generate_pot="true" 51 ;; 52 k) 53 fix_translations="true" 54 ;; 55 g) 56 generate_mo="true" 57 ;; 58 \?) 59 echo "[-f <files>] -x -g" >&2 60 exit 1 61 ;; 62 esac 63 done 64 65 if [[ ${#KUBECTL_FILES} -eq 0 ]]; then 66 KUBECTL_FILES+=("${KUBECTL_DEFAULT_LOCATIONS[@]}") 67 fi 68 69 if ! which go-xgettext > /dev/null; then 70 echo 'Can not find go-xgettext, install with:' 71 echo 'go get github.com/gosexy/gettext/go-xgettext' 72 exit 1 73 fi 74 75 if ! which msgfmt > /dev/null; then 76 echo 'Can not find msgfmt, install with:' 77 echo 'apt-get install gettext' 78 exit 1 79 fi 80 81 if [[ "${generate_pot}" == "true" ]]; then 82 echo "Extracting strings to POT" 83 # shellcheck disable=SC2046 84 go-xgettext -k=i18n.T $(grep -lr "i18n.T" "${KUBECTL_FILES[@]}" | grep -vE "${KUBECTL_IGNORE_FILES_REGEX}") > tmp.pot 85 perl -pi -e 's/CHARSET/UTF-8/' tmp.pot 86 perl -pi -e 's/\\(?!n"\n|")/\\\\/g' tmp.pot 87 kube::util::ensure-temp-dir 88 if msgcat -s tmp.pot > "${KUBE_TEMP}/template.pot"; then 89 mv "${KUBE_TEMP}/template.pot" "${TRANSLATIONS}/kubectl/template.pot" 90 rm tmp.pot 91 else 92 echo "Failed to update template.pot" 93 exit 1 94 fi 95 fi 96 97 if [[ "${fix_translations}" == "true" ]]; then 98 echo "Fixing .po files" 99 kube::util::ensure-temp-dir 100 for PO_FILE in "${TRANSLATIONS}"/kubectl/*/LC_MESSAGES/k8s.po; do 101 TMP="${KUBE_TEMP}/fix.po" 102 if [[ "${PO_FILE}" =~ .*/default/.* || "${PO_FILE}" =~ .*/en_US/.* ]]; then 103 # mark obsolete, and set default values for english translations 104 msgen "${TRANSLATIONS}/kubectl/template.pot" | \ 105 msgmerge -s --no-fuzzy-matching "${PO_FILE}" - > "${TMP}" 106 else 107 # mark obsolete, but do not add untranslated messages 108 msgmerge -s --no-fuzzy-matching "${PO_FILE}" "${TRANSLATIONS}/kubectl/template.pot" | msgattrib --translated - > "${TMP}" 109 fi 110 mv "${TMP}" "${PO_FILE}" 111 done 112 fi 113 114 if [[ "${generate_mo}" == "true" ]]; then 115 echo "Generating .po and .mo files" 116 for x in "${TRANSLATIONS}"/*/*/*/*.po; do 117 msgcat -s "${x}" > tmp.po 118 mv tmp.po "${x}" 119 echo "generating .mo file for: ${x}" 120 msgfmt "${x}" -o "$(dirname "${x}")/$(basename "${x}" .po).mo" 121 done 122 fi