Skip to content

Commit

Permalink
Update extraction script, sort messages, add .pot file.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Feb 23, 2017
1 parent 17375fc commit be26836
Show file tree
Hide file tree
Showing 16 changed files with 3,890 additions and 2,897 deletions.
76 changes: 76 additions & 0 deletions hack/update-translations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

KUBECTL_FILES="pkg/kubectl/cmd/*.go pkg/kubectl/cmd/*/*.go"

generate_pot="false"
generate_mo="false"

while getopts "hf:xg" opt; do
case $opt in
h)
echo "$0 [-f files] [-x] [-g]"
echo " -f <file-path>: Files to process"
echo " -x extract strings to a POT file"
echo " -g sort .po files and generate .mo files"
exit 0
;;
f)
KUBECTL_FILES="${OPTARG}"
;;
x)
generate_pot="true"
;;
g)
generate_mo="true"
;;
\?)
echo "[-f <files>] -x -g" >&2
exit 1
;;
esac
done

if ! which go-xgettext > /dev/null; then
echo 'Can not find go-xgettext, install with:'
echo 'go get github.com/gosexy/gettext/go-xgettext'
exit 1
fi

if ! which msgfmt > /dev/null; then
echo 'Can not find msgfmt, install with:'
echo 'apt-get install gettext'
exit 1
fi

if [[ "${generate_pot}" == "true" ]]; then
echo "Extracting strings to POT"
go-xgettext -k=i18n.T ${KUBECTL_FILES} > tmp.pot
msgcat -s tmp.pot > translations/kubectl/template.pot
rm tmp.pot
fi

if [[ "${generate_mo}" == "true" ]]; then
echo "Generating .po and .mo files"
for x in translations/*/*/*/*.po; do
msgcat -s $x > tmp.po
mv tmp.po $x
echo "generating .mo file for: $x"
msgfmt $x -o "$(dirname $x)/$(basename $x .po).mo"
done
fi

./hack/generate-bindata.sh
3,405 changes: 1,932 additions & 1,473 deletions pkg/generated/bindata.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/util/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ func LoadTranslations(root string, getLanguageFn func() string) error {
// and plural translation is used.
func T(defaultValue string, args ...int) string {
if len(args) == 0 {
return gettext.PGettext(defaultValue, defaultValue)
return gettext.PGettext("", defaultValue)
}
return fmt.Sprintf(gettext.PNGettext(defaultValue, defaultValue, defaultValue+".plural", args[0]),
return fmt.Sprintf(gettext.PNGettext("", defaultValue, defaultValue+".plural", args[0]),
args[0])
}

Expand Down
27 changes: 23 additions & 4 deletions translations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,35 @@ no need to update `translations/test/...` which is only used for unit tests.

There is an example [PR here](https://github.com/kubernetes/kubernetes/pull/40645) which adds support for French.

Move on to Adding new translations
Once you've added a new language, you'll need to register it in
`pkg/util/i18n/i18n.go` by adding it to the `knownTranslations` map.

## Wrapping strings
There is a simple script in `translations/extract.py` that performs
simple regular expression based wrapping of strings. It can always
use improvements to understand additional strings.

## Extracting strings
Once the strings are wrapped, you can extract strings from go files using
the `go-xgettext` command which can be installed with:

```console
go get github.com/gosexy/gettext/tree/master/go-xgettext
```

Once that's installed you can run `hack/update-translations.sh`, which
will extract and sort any new strings.

## Adding new translations
Edit the appropriate `k8s.po` file, `poedit` is a popular open source tool
for translations.
for translations. You can load the `translations/kubectl/template.pot` file
to find messages that might be missing.

Once you are done with your `.po` file, generate the corresponding `k8s.mo`
file. `poedit` does this automatically on save.
file. `poedit` does this automatically on save, but you can also use
`hack/update-translations.sh` to perform the `po` to `mo` translation.

We use the English translation as both the `msg_id` and the `msg_context`.
We use the English translation as the `msgid`.

## Regenerating the bindata file
Run `./hack/generate-bindata.sh, this will turn the translation files
Expand Down
32 changes: 10 additions & 22 deletions translations/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ def __init__(self, regex, replace_fn):
self.regex = re.compile(regex)
self.replace_fn = replace_fn

# global holding all strings discovered
STRINGS = []

def short_replace(match, file, line_number):
"""Replace a Short: ... cobra command description with an internationalization
"""
sys.stdout.write('{}i18n.T({}),\n'.format(match.group(1), match.group(2)))
STRINGS.append((match.group(2), file, line_number))

SHORT_MATCH = MatchHandler(r'(\s+Short:\s+)("[^"]+"),', short_replace)

Expand All @@ -54,6 +50,14 @@ def import_replace(match, file, line_number):

IMPORT_MATCH = MatchHandler('(.*"k8s.io/kubernetes/pkg/kubectl/cmd/util")', import_replace)


def string_flag_replace(match, file, line_number):
"""Replace a cmd.Flags().String("...", "", "...") with an internationalization
"""
sys.stdout.write('{}i18n.T("{})"))\n'.format(match.group(1), match.group(2)))

STRING_FLAG_MATCH = MatchHandler('(\s+cmd\.Flags\(\).String\("[^"]*", "[^"]*", )"([^"]*)"\)', string_flag_replace)

def replace(filename, matchers):
"""Given a file and a set of matchers, run those matchers
across the file and replace it with the results.
Expand All @@ -75,22 +79,6 @@ def replace(filename, matchers):

# gofmt the file again
from subprocess import call
call(["gofmt", "-s", "-w", filename])

# update the translation files
translation_files = [
"translations/kubectl/default/LC_MESSAGES/k8s.po",
"translations/kubectl/en_US/LC_MESSAGES/k8s.po",
]

for translation_filename in translation_files:
with open(translation_filename, "a") as tfile:
for translation_string in STRINGS:
msg_string = translation_string[0]
tfile.write('\n')
tfile.write('# https://github.com/kubernetes/kubernetes/blob/master/{}#L{}\n'.format(translation_string[1], translation_string[2]))
tfile.write('msgctxt {}\n'.format(msg_string))
tfile.write('msgid {}\n'.format(msg_string))
tfile.write('msgstr {}\n'.format(msg_string))
call(["goimports", "-w", filename])

replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH])
replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH, STRING_FLAG_MATCH])
Binary file modified translations/kubectl/default/LC_MESSAGES/k8s.mo
Binary file not shown.
Loading

0 comments on commit be26836

Please sign in to comment.