Add a script to extract strings for translation and a sample of what it does.

pull/6/head
Brendan Burns 2016-12-24 21:09:03 -08:00
parent 28c439dcfb
commit e8b0f59ed2
5 changed files with 219 additions and 1 deletions

View File

@ -1,5 +1,6 @@
// Code generated by go-bindata.
// sources:
// translations/extract.py
// translations/kubectl/default/LC_MESSAGES/k8s.mo
// translations/kubectl/default/LC_MESSAGES/k8s.po
// translations/kubectl/en_US/LC_MESSAGES/k8s.mo
@ -52,6 +53,104 @@ func (fi bindataFileInfo) Sys() interface{} {
return nil
}
var _translationsExtractPy = []byte(`#!/usr/bin/python
"""Extract strings from command files and externalize into translation files.
Expects to be run from the root directory of the repository.
Usage:
extract.py pkg/kubectl/cmd/apply.go
"""
import fileinput
import sys
import re
class MatchHandler(object):
""" Simple holder for a regular expression and a function
to run if that regular expression matches a line.
The function should expect (re.match, file, linenumber) as parameters
"""
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)
def import_replace(match, file, line_number):
"""Add an extra import for the i18n library.
Doesn't try to be smart and detect if it's already present, assumes a
gofmt round wil fix things.
"""
sys.stdout.write('{}\n"k8s.io/kubernetes/pkg/util/i18n"\n'.format(match.group(1)))
IMPORT_MATCH = MatchHandler('(.*"k8s.io/kubernetes/pkg/kubectl/cmd/util")', import_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.
"""
# Run all the matchers
line_number = 0
for line in fileinput.input(filename, inplace=True):
line_number += 1
matched = False
for matcher in matchers:
match = matcher.regex.match(line)
if match:
matcher.replace_fn(match, filename, line_number)
matched = True
break
if not matched:
sys.stdout.write(line)
sys.stdout.flush()
# 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))
replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH])
`)
func translationsExtractPyBytes() ([]byte, error) {
return _translationsExtractPy, nil
}
func translationsExtractPy() (*asset, error) {
bytes, err := translationsExtractPyBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "translations/extract.py", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _translationsKubectlDefaultLc_messagesK8sMo = []byte("\xde\x12\x04\x95\x00\x00\x00\x00\x03\x00\x00\x00\x1c\x00\x00\x004\x00\x00\x00\x05\x00\x00\x00L\x00\x00\x00\x00\x00\x00\x00`\x00\x00\x00I\x00\x00\x00a\x00\x00\x00(\x01\x00\x00\xab\x00\x00\x00\xac\x01\x00\x00\xd4\x01\x00\x00$\x00\x00\x00\x81\x03\x00\x00\xc3\x00\x00\x00\xa6\x03\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00Update the annotations on a resource\x04Update the annotations on a resource\x00watch is only supported on individual resources and resource collections - %d resources were found\x04watch is only supported on individual resources and resource collections - %d resources were found\x00watch is only supported on individual resources and resource collections - %d resources were found\x00Project-Id-Version: gettext-go-examples-hello\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2013-12-12 20:03+0000\nPO-Revision-Date: 2016-12-13 21:54-0800\nLast-Translator: Brendan Burns <brendan.d.burns@gmail.com>\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nX-Generator: Poedit 1.6.10\nX-Poedit-SourceCharset: UTF-8\nLanguage-Team: \nPlural-Forms: nplurals=2; plural=(n != 1);\nLanguage: en\n\x00Update the annotations on a resource\x00watch is only supported on individual resources and resource collections - %d resource was found\x00watch is only supported on individual resources and resource collections - %d resources were found\x00")
func translationsKubectlDefaultLc_messagesK8sMoBytes() ([]byte, error) {
@ -109,6 +208,11 @@ msgstr[0] ""
msgstr[1] ""
"watch is only supported on individual resources and resource collections - "
"%d resources were found"
# https://github.com/kubernetes/kubernetes/blob/masterpkg/kubectl/cmd/apply.go#L98
msgctxt "Apply a configuration to a resource by filename or stdin"
msgid "Apply a configuration to a resource by filename or stdin"
msgstr "Apply a configuration to a resource by filename or stdin"
`)
func translationsKubectlDefaultLc_messagesK8sPoBytes() ([]byte, error) {
@ -183,6 +287,11 @@ msgstr[0] ""
msgstr[1] ""
"watch is only supported on individual resources and resource collections - "
"%d resources were found"
# https://github.com/kubernetes/kubernetes/blob/masterpkg/kubectl/cmd/apply.go#L98
msgctxt "Apply a configuration to a resource by filename or stdin"
msgid "Apply a configuration to a resource by filename or stdin"
msgstr "Apply a configuration to a resource by filename or stdin"
`)
func translationsKubectlEn_usLc_messagesK8sPoBytes() ([]byte, error) {
@ -380,6 +489,7 @@ func AssetNames() []string {
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"translations/extract.py": translationsExtractPy,
"translations/kubectl/default/LC_MESSAGES/k8s.mo": translationsKubectlDefaultLc_messagesK8sMo,
"translations/kubectl/default/LC_MESSAGES/k8s.po": translationsKubectlDefaultLc_messagesK8sPo,
"translations/kubectl/en_US/LC_MESSAGES/k8s.mo": translationsKubectlEn_usLc_messagesK8sMo,
@ -432,6 +542,7 @@ type bintree struct {
var _bintree = &bintree{nil, map[string]*bintree{
"translations": {nil, map[string]*bintree{
"extract.py": {translationsExtractPy, map[string]*bintree{}},
"kubectl": {nil, map[string]*bintree{
"default": {nil, map[string]*bintree{
"LC_MESSAGES": {nil, map[string]*bintree{

View File

@ -42,6 +42,7 @@ import (
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/util/i18n"
)
type ApplyOptions struct {
@ -96,7 +97,7 @@ func NewCmdApply(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "apply -f FILENAME",
Short: "Apply a configuration to a resource by filename or stdin",
Short: i18n.T("Apply a configuration to a resource by filename or stdin"),
Long: apply_long,
Example: apply_example,
Run: func(cmd *cobra.Command, args []string) {

96
translations/extract.py Normal file
View File

@ -0,0 +1,96 @@
#!/usr/bin/env python
# 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.
"""Extract strings from command files and externalize into translation files.
Expects to be run from the root directory of the repository.
Usage:
extract.py pkg/kubectl/cmd/apply.go
"""
import fileinput
import sys
import re
class MatchHandler(object):
""" Simple holder for a regular expression and a function
to run if that regular expression matches a line.
The function should expect (re.match, file, linenumber) as parameters
"""
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)
def import_replace(match, file, line_number):
"""Add an extra import for the i18n library.
Doesn't try to be smart and detect if it's already present, assumes a
gofmt round wil fix things.
"""
sys.stdout.write('{}\n"k8s.io/kubernetes/pkg/util/i18n"\n'.format(match.group(1)))
IMPORT_MATCH = MatchHandler('(.*"k8s.io/kubernetes/pkg/kubectl/cmd/util")', import_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.
"""
# Run all the matchers
line_number = 0
for line in fileinput.input(filename, inplace=True):
line_number += 1
matched = False
for matcher in matchers:
match = matcher.regex.match(line)
if match:
matcher.replace_fn(match, filename, line_number)
matched = True
break
if not matched:
sys.stdout.write(line)
sys.stdout.flush()
# 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))
replace(sys.argv[1], [SHORT_MATCH, IMPORT_MATCH])

View File

@ -38,3 +38,8 @@ msgstr[0] ""
msgstr[1] ""
"watch is only supported on individual resources and resource collections - "
"%d resources were found"
# https://github.com/kubernetes/kubernetes/blob/masterpkg/kubectl/cmd/apply.go#L98
msgctxt "Apply a configuration to a resource by filename or stdin"
msgid "Apply a configuration to a resource by filename or stdin"
msgstr "Apply a configuration to a resource by filename or stdin"

View File

@ -38,3 +38,8 @@ msgstr[0] ""
msgstr[1] ""
"watch is only supported on individual resources and resource collections - "
"%d resources were found"
# https://github.com/kubernetes/kubernetes/blob/masterpkg/kubectl/cmd/apply.go#L98
msgctxt "Apply a configuration to a resource by filename or stdin"
msgid "Apply a configuration to a resource by filename or stdin"
msgstr "Apply a configuration to a resource by filename or stdin"