Fix plural handling, put teeth in msgfmt -c test.
This commit is contained in:
@@ -99,20 +99,6 @@ class Dummy(Converter):
|
||||
"""replaces the final char of string with #"""
|
||||
return string[:-1] + '#'
|
||||
|
||||
def init_msgs(self, msgs):
|
||||
"""
|
||||
Make sure the first msg in msgs has a plural property.
|
||||
msgs is list of instances of polib.POEntry
|
||||
"""
|
||||
if not msgs:
|
||||
return
|
||||
headers = msgs[0].get_property('msgstr')
|
||||
has_plural = any(header.startswith('Plural-Forms:') for header in headers)
|
||||
if not has_plural:
|
||||
# Apply declaration for English pluralization rules
|
||||
plural = "Plural-Forms: nplurals=2; plural=(n != 1);\\n"
|
||||
headers.append(plural)
|
||||
|
||||
def convert_msg(self, msg):
|
||||
"""
|
||||
Takes one POEntry object and converts it (adds a dummy translation to it)
|
||||
@@ -128,8 +114,10 @@ class Dummy(Converter):
|
||||
# translate singular and plural
|
||||
foreign_single = self.convert(source)
|
||||
foreign_plural = self.convert(plural)
|
||||
plural = {'0': self.final_newline(source, foreign_single),
|
||||
'1': self.final_newline(plural, foreign_plural)}
|
||||
plural = {
|
||||
'0': self.final_newline(source, foreign_single),
|
||||
'1': self.final_newline(plural, foreign_plural),
|
||||
}
|
||||
msg.msgstr_plural = plural
|
||||
else:
|
||||
foreign = self.convert(source)
|
||||
|
||||
@@ -60,9 +60,12 @@ def merge(locale, target='django.po', fail_if_missing=True):
|
||||
def clean_metadata(file):
|
||||
"""
|
||||
Clean up redundancies in the metadata caused by merging.
|
||||
This reads in a PO file and simply saves it back out again.
|
||||
"""
|
||||
pofile(file).save()
|
||||
# Reading in the .po file and saving it again fixes redundancies.
|
||||
pomsgs = pofile(file)
|
||||
# The msgcat tool marks the metadata as fuzzy, but it's ok as it is.
|
||||
pomsgs.metadata_is_fuzzy = False
|
||||
pomsgs.save()
|
||||
|
||||
|
||||
def validate_files(dir, files_to_merge):
|
||||
|
||||
@@ -38,9 +38,15 @@ def main(file, locale):
|
||||
raise IOError('File does not exist: %s' % file)
|
||||
pofile = polib.pofile(file)
|
||||
converter = Dummy()
|
||||
converter.init_msgs(pofile.translated_entries())
|
||||
for msg in pofile:
|
||||
converter.convert_msg(msg)
|
||||
|
||||
# If any message has a plural, then the file needs plural information.
|
||||
# Apply declaration for English pluralization rules so that ngettext will
|
||||
# do something reasonable.
|
||||
if any(m.msgid_plural for m in pofile):
|
||||
pofile.metadata['Plural-Forms'] = 'nplurals=2; plural=(n != 1);'
|
||||
|
||||
new_file = new_filename(file, locale)
|
||||
create_dir_if_necessary(new_file)
|
||||
pofile.save(new_file)
|
||||
|
||||
@@ -12,9 +12,9 @@ def test_po_files(root=LOCALE_DIR):
|
||||
log = logging.getLogger(__name__)
|
||||
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
||||
|
||||
for (dirpath, dirnames, filenames) in os.walk(root):
|
||||
for dirpath, __, filenames in os.walk(root):
|
||||
for name in filenames:
|
||||
(base, ext) = os.path.splitext(name)
|
||||
__, ext = os.path.splitext(name)
|
||||
if ext.lower() == '.po':
|
||||
yield validate_po_file, os.path.join(dirpath, name), log
|
||||
|
||||
@@ -26,6 +26,8 @@ def validate_po_file(filename, log):
|
||||
"""
|
||||
# Use relative paths to make output less noisy.
|
||||
rfile = os.path.relpath(filename, LOCALE_DIR)
|
||||
(out, err) = call(['msgfmt','-c', rfile], working_directory=LOCALE_DIR)
|
||||
out, err = call(['msgfmt', '-c', rfile], working_directory=LOCALE_DIR)
|
||||
if err != '':
|
||||
log.warn('\n'+err)
|
||||
log.info('\n' + out)
|
||||
log.warn('\n' + err)
|
||||
assert not err
|
||||
|
||||
Reference in New Issue
Block a user