Add the pseudo translation generating tool
Use command tools/pseudo.py [input pot/po file] [output po file] [language] --string pseudo_string to generate a pseudo translation po file. Change-Id: I08afd318b8484ed107ea837bfd34d8dc1f00f384
This commit is contained in:
parent
1411a9ce58
commit
46689dc1ec
80
tools/pseudo.py
Executable file
80
tools/pseudo.py
Executable file
@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# Copyright 2015 IBM Corp.
|
||||
# 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.
|
||||
|
||||
import argparse
|
||||
|
||||
import babel.messages.catalog as catalog
|
||||
import babel.messages.pofile as pofile
|
||||
|
||||
|
||||
def translate(segment,translation):
|
||||
prefix = u""
|
||||
# When the id starts with a newline the mo compiler enforces that
|
||||
# the translated message must also start with a newline. Make
|
||||
# sure that doesn't get broken when prepending the bracket.
|
||||
if segment.startswith('\n'):
|
||||
prefix = u"\n"
|
||||
orig_size = len(segment)
|
||||
# Add extra expansion space based on recommenation from
|
||||
# http://www-01.ibm.com/software/globalization/guidelines/a3.html
|
||||
if orig_size < 20:
|
||||
multiplier = 1
|
||||
elif orig_size < 30:
|
||||
multiplier = 0.8
|
||||
elif orig_size < 50:
|
||||
multiplier = 0.6
|
||||
elif orig_size < 70:
|
||||
multiplier = 0.4
|
||||
else:
|
||||
multiplier = 0.3
|
||||
extra_length = int(max(0, (orig_size * multiplier) - 10))
|
||||
extra_chars = "~" * extra_length
|
||||
return u"{0}[~{1}~{2}{3}]".format(prefix, segment, translation.decode('utf-8'), extra_chars)
|
||||
|
||||
|
||||
def main():
|
||||
# Check arguments
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('pot_filename', type=argparse.FileType('r'))
|
||||
parser.add_argument('po_filename', type=argparse.FileType('w'))
|
||||
parser.add_argument('locale')
|
||||
parser.add_argument('--string', default='您好яшçあ', help='pseudo string')
|
||||
args = parser.parse_args()
|
||||
|
||||
# read POT file
|
||||
pot_cat = pofile.read_po(args.pot_filename, ignore_obsolete=True)
|
||||
|
||||
# Create the new Catalog
|
||||
new_cat = catalog.Catalog(locale=args.locale,
|
||||
last_translator="pseudo.py",
|
||||
charset="utf-8")
|
||||
num_plurals = new_cat.num_plurals
|
||||
|
||||
# Process messages from template
|
||||
for msg in pot_cat:
|
||||
if msg.pluralizable:
|
||||
msg.string = [translate(u"{}:{}".format(i, msg.id[0]), args.string)
|
||||
for i in range(num_plurals)]
|
||||
else:
|
||||
msg.string = translate(msg.id, args.string)
|
||||
new_cat[msg.id] = msg
|
||||
|
||||
# Write "translated" PO file
|
||||
pofile.write_po(args.po_filename, new_cat, ignore_obsolete=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user