Skip to content

Commit be26836

Browse files
committed
Update extraction script, sort messages, add .pot file.
1 parent 17375fc commit be26836

File tree

16 files changed

+3890
-2897
lines changed

16 files changed

+3890
-2897
lines changed

hack/update-translations.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/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+
KUBECTL_FILES="pkg/kubectl/cmd/*.go pkg/kubectl/cmd/*/*.go"
18+
19+
generate_pot="false"
20+
generate_mo="false"
21+
22+
while getopts "hf:xg" opt; do
23+
case $opt in
24+
h)
25+
echo "$0 [-f files] [-x] [-g]"
26+
echo " -f <file-path>: Files to process"
27+
echo " -x extract strings to a POT file"
28+
echo " -g sort .po files and generate .mo files"
29+
exit 0
30+
;;
31+
f)
32+
KUBECTL_FILES="${OPTARG}"
33+
;;
34+
x)
35+
generate_pot="true"
36+
;;
37+
g)
38+
generate_mo="true"
39+
;;
40+
\?)
41+
echo "[-f <files>] -x -g" >&2
42+
exit 1
43+
;;
44+
esac
45+
done
46+
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+
53+
if ! which msgfmt > /dev/null; then
54+
echo 'Can not find msgfmt, install with:'
55+
echo 'apt-get install gettext'
56+
exit 1
57+
fi
58+
59+
if [[ "${generate_pot}" == "true" ]]; then
60+
echo "Extracting strings to POT"
61+
go-xgettext -k=i18n.T ${KUBECTL_FILES} > tmp.pot
62+
msgcat -s tmp.pot > translations/kubectl/template.pot
63+
rm tmp.pot
64+
fi
65+
66+
if [[ "${generate_mo}" == "true" ]]; then
67+
echo "Generating .po and .mo files"
68+
for x in translations/*/*/*/*.po; do
69+
msgcat -s $x > tmp.po
70+
mv tmp.po $x
71+
echo "generating .mo file for: $x"
72+
msgfmt $x -o "$(dirname $x)/$(basename $x .po).mo"
73+
done
74+
fi
75+
76+
./hack/generate-bindata.sh

pkg/generated/bindata.go

Lines changed: 1932 additions & 1473 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/i18n/i18n.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ func LoadTranslations(root string, getLanguageFn func() string) error {
120120
// and plural translation is used.
121121
func T(defaultValue string, args ...int) string {
122122
if len(args) == 0 {
123-
return gettext.PGettext(defaultValue, defaultValue)
123+
return gettext.PGettext("", defaultValue)
124124
}
125-
return fmt.Sprintf(gettext.PNGettext(defaultValue, defaultValue, defaultValue+".plural", args[0]),
125+
return fmt.Sprintf(gettext.PNGettext("", defaultValue, defaultValue+".plural", args[0]),
126126
args[0])
127127
}
128128

translations/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,35 @@ no need to update `translations/test/...` which is only used for unit tests.
1010

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

13-
Move on to Adding new translations
13+
Once you've added a new language, you'll need to register it in
14+
`pkg/util/i18n/i18n.go` by adding it to the `knownTranslations` map.
15+
16+
## Wrapping strings
17+
There is a simple script in `translations/extract.py` that performs
18+
simple regular expression based wrapping of strings. It can always
19+
use improvements to understand additional strings.
20+
21+
## Extracting strings
22+
Once the strings are wrapped, you can extract strings from go files using
23+
the `go-xgettext` command which can be installed with:
24+
25+
```console
26+
go get github.com/gosexy/gettext/tree/master/go-xgettext
27+
```
28+
29+
Once that's installed you can run `hack/update-translations.sh`, which
30+
will extract and sort any new strings.
1431

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

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

22-
We use the English translation as both the `msg_id` and the `msg_context`.
41+
We use the English translation as the `msgid`.
2342

2443
## Regenerating the bindata file
2544
Run `./hack/generate-bindata.sh, this will turn the translation files

translations/extract.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ def __init__(self, regex, replace_fn):
3434
self.regex = re.compile(regex)
3535
self.replace_fn = replace_fn
3636

37-
# global holding all strings discovered
38-
STRINGS = []
39-
4037
def short_replace(match, file, line_number):
4138
"""Replace a Short: ... cobra command description with an internationalization
4239
"""
4340
sys.stdout.write('{}i18n.T({}),\n'.format(match.group(1), match.group(2)))
44-
STRINGS.append((match.group(2), file, line_number))
4541

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

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

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

53+
54+
def string_flag_replace(match, file, line_number):
55+
"""Replace a cmd.Flags().String("...", "", "...") with an internationalization
56+
"""
57+
sys.stdout.write('{}i18n.T("{})"))\n'.format(match.group(1), match.group(2)))
58+
59+
STRING_FLAG_MATCH = MatchHandler('(\s+cmd\.Flags\(\).String\("[^"]*", "[^"]*", )"([^"]*)"\)', string_flag_replace)
60+
5761
def replace(filename, matchers):
5862
"""Given a file and a set of matchers, run those matchers
5963
across the file and replace it with the results.
@@ -75,22 +79,6 @@ def replace(filename, matchers):
7579

7680
# gofmt the file again
7781
from subprocess import call
78-
call(["gofmt", "-s", "-w", filename])
79-
80-
# update the translation files
81-
translation_files = [
82-
"translations/kubectl/default/LC_MESSAGES/k8s.po",
83-
"translations/kubectl/en_US/LC_MESSAGES/k8s.po",
84-
]
85-
86-
for translation_filename in translation_files:
87-
with open(translation_filename, "a") as tfile:
88-
for translation_string in STRINGS:
89-
msg_string = translation_string[0]
90-
tfile.write('\n')
91-
tfile.write('# https://github.com/kubernetes/kubernetes/blob/master/{}#L{}\n'.format(translation_string[1], translation_string[2]))
92-
tfile.write('msgctxt {}\n'.format(msg_string))
93-
tfile.write('msgid {}\n'.format(msg_string))
94-
tfile.write('msgstr {}\n'.format(msg_string))
82+
call(["goimports", "-w", filename])
9583

96-
replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH])
84+
replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH, STRING_FLAG_MATCH])
-3.25 KB
Binary file not shown.

0 commit comments

Comments
 (0)