...

Text file src/k8s.io/kubernetes/hack/update-translations.sh

Documentation: k8s.io/kubernetes/hack

     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
    21KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    22source "${KUBE_ROOT}/hack/lib/util.sh"
    23
    24TRANSLATIONS="staging/src/k8s.io/kubectl/pkg/util/i18n/translations"
    25KUBECTL_FILES=()
    26KUBECTL_DEFAULT_LOCATIONS=(
    27  "pkg/kubectl/cmd"
    28  "staging/src/k8s.io/kubectl/pkg/cmd"
    29)
    30KUBECTL_IGNORE_FILES_REGEX="cmd/kustomize/kustomize.go"
    31
    32generate_pot="false"
    33generate_mo="false"
    34fix_translations="false"
    35
    36while 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
    63done
    64
    65if [[ ${#KUBECTL_FILES} -eq 0 ]]; then
    66  KUBECTL_FILES+=("${KUBECTL_DEFAULT_LOCATIONS[@]}")
    67fi
    68
    69if ! 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
    73fi
    74
    75if ! which msgfmt > /dev/null; then
    76  echo 'Can not find msgfmt, install with:'
    77  echo 'apt-get install gettext'
    78  exit 1
    79fi
    80
    81if [[ "${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
    95fi
    96
    97if [[ "${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
   112fi
   113
   114if [[ "${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
   122fi

View as plain text