From b06cf9cfbb8624b590b47b30e75b081e88ccaccb Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 3 Feb 2014 14:27:31 -0500 Subject: [PATCH] Make i18n tests work with a clean checkout --- i18n/dummy.py | 126 +++++++++++++++++++++++++++--------- i18n/make_dummy.py | 66 ------------------- i18n/tests/test_generate.py | 11 +++- rakelib/i18n.rake | 2 +- 4 files changed, 107 insertions(+), 98 deletions(-) diff --git a/i18n/dummy.py b/i18n/dummy.py index ff68c68089..cd4e063ca7 100644 --- a/i18n/dummy.py +++ b/i18n/dummy.py @@ -1,33 +1,33 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- -r""" -Creates new localization properties files in a dummy language. - -Each property file is derived from the equivalent en_US file, with these -transformations applied: - -1. Every vowel is replaced with an equivalent with extra accent marks. - -2. Every string is padded out to +30% length to simulate verbose languages - (such as German) to see if layout and flows work properly. - -3. Every string is terminated with a '#' character to make it easier to detect - truncation. - -Example use:: - - >>> from dummy import Dummy - >>> c = Dummy() - >>> c.convert("My name is Bond, James Bond") - u'M\xfd n\xe4m\xe9 \xefs B\xf8nd, J\xe4m\xe9s B\xf8nd \u2360\u03c3\u044f\u0454\u043c \u03b9\u03c1#' - >>> print c.convert("My name is Bond, James Bond") - Mý nämé ïs Bønd, Jämés Bønd Ⱡσяєм ιρ# - >>> print c.convert("don't convert tag ids") - døn't çønvért täg ïds Ⱡσяєм ιρѕυ# - >>> print c.convert("don't convert %(name)s tags on %(date)s") - døn't çønvért %(name)s tägs øn %(date)s Ⱡσяєм ιρѕ# """ +Generate test translation files from human-readable po files. +Dummy language is specified in configuration file (see config.py) +two letter language codes reference: +see http://www.loc.gov/standards/iso639-2/php/code_list.php + +Django will not localize in languages that django itself has not been +localized for. So we are using a well-known language (default='eo'). +Django languages are listed in django.conf.global_settings.LANGUAGES + +po files can be generated with this: +django-admin.py makemessages --all --extension html -l en + +Usage: + +$ ./dummy.py + +generates output conf/locale/$DUMMY_LOCALE/LC_MESSAGES, +where $DUMMY_LOCALE is the dummy_locale value set in the i18n config +""" + +import os +import polib + +from i18n.config import CONFIGURATION +from i18n.execute import create_dir_if_necessary from i18n.converter import Converter # Substitute plain characters with accented lookalikes. @@ -74,10 +74,32 @@ PAD_FACTOR = 1.33 class Dummy(Converter): - """ - A string converter that generates dummy strings with fake accents - and lorem ipsum padding. + r""" + Creates new localization properties files in a dummy language. + Each property file is derived from the equivalent en_US file, with these + transformations applied: + + 1. Every vowel is replaced with an equivalent with extra accent marks. + + 2. Every string is padded out to +30% length to simulate verbose languages + (such as German) to see if layout and flows work properly. + + 3. Every string is terminated with a '#' character to make it easier to detect + truncation. + + Example use:: + + >>> from dummy import Dummy + >>> c = Dummy() + >>> c.convert("My name is Bond, James Bond") + u'M\xfd n\xe4m\xe9 \xefs B\xf8nd, J\xe4m\xe9s B\xf8nd \u2360\u03c3\u044f\u0454\u043c \u03b9\u03c1#' + >>> print c.convert("My name is Bond, James Bond") + Mý nämé ïs Bønd, Jämés Bønd Ⱡσяєм ιρ# + >>> print c.convert("don't convert tag ids") + døn't çønvért täg ïds Ⱡσяєм ιρѕυ# + >>> print c.convert("don't convert %(name)s tags on %(date)s") + døn't çønvért %(name)s tägs øn %(date)s Ⱡσяєм ιρѕ# """ def convert(self, string): result = Converter.convert(self, string) @@ -134,3 +156,49 @@ class Dummy(Converter): if original[-1] == '\n' and translated[-1] != '\n': translated += '\n' return translated + + +def make_dummy(file, locale): + """ + Takes a source po file, reads it, and writes out a new po file + in :param locale: containing a dummy translation. + """ + if not os.path.exists(file): + raise IOError('File does not exist: %s' % file) + pofile = polib.pofile(file) + converter = Dummy() + for msg in pofile: + converter.convert_msg(msg) + + # Apply declaration for English pluralization rules so that ngettext will + # do something reasonable. + 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) + + +def new_filename(original_filename, new_locale): + """Returns a filename derived from original_filename, using new_locale as the locale""" + orig_dir = os.path.dirname(original_filename) + msgs_dir = os.path.basename(orig_dir) + orig_file = os.path.basename(original_filename) + return os.path.abspath(os.path.join(orig_dir, '../..', new_locale, msgs_dir, orig_file)) + + +def main(): + """ + Generate dummy strings for all source po files. + """ + LOCALE = CONFIGURATION.dummy_locale + SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir + print "Processing source language files into dummy strings:" + for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'): + print ' ', source_file.relpath() + make_dummy(SOURCE_MSGS_DIR.joinpath(source_file), LOCALE) + print + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/i18n/make_dummy.py b/i18n/make_dummy.py index 421f799b36..e69de29bb2 100755 --- a/i18n/make_dummy.py +++ b/i18n/make_dummy.py @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -# Generate test translation files from human-readable po files. -# -# Dummy language is specified in configuration file (see config.py) -# two letter language codes reference: -# see http://www.loc.gov/standards/iso639-2/php/code_list.php -# -# Django will not localize in languages that django itself has not been -# localized for. So we are using a well-known language (default='eo'). -# Django languages are listed in django.conf.global_settings.LANGUAGES -# -# po files can be generated with this: -# django-admin.py makemessages --all --extension html -l en -# -# Usage: -# -# $ ./make_dummy.py -# -# generates output conf/locale/$DUMMY_LOCALE/LC_MESSAGES, -# where $DUMMY_LOCALE is the dummy_locale value set in the i18n config - -import os, sys -import polib - -from i18n.dummy import Dummy -from i18n.config import CONFIGURATION -from i18n.execute import create_dir_if_necessary - - -def main(file, locale): - """ - Takes a source po file, reads it, and writes out a new po file - in :param locale: containing a dummy translation. - """ - if not os.path.exists(file): - raise IOError('File does not exist: %s' % file) - pofile = polib.pofile(file) - converter = Dummy() - for msg in pofile: - converter.convert_msg(msg) - - # Apply declaration for English pluralization rules so that ngettext will - # do something reasonable. - 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) - - -def new_filename(original_filename, new_locale): - """Returns a filename derived from original_filename, using new_locale as the locale""" - orig_dir = os.path.dirname(original_filename) - msgs_dir = os.path.basename(orig_dir) - orig_file = os.path.basename(original_filename) - return os.path.abspath(os.path.join(orig_dir, '../..', new_locale, msgs_dir, orig_file)) - -if __name__ == '__main__': - LOCALE = CONFIGURATION.dummy_locale - SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir - print "Processing source language files into dummy strings:" - for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'): - print ' ', source_file.relpath() - main(SOURCE_MSGS_DIR.joinpath(source_file), LOCALE) - print diff --git a/i18n/tests/test_generate.py b/i18n/tests/test_generate.py index 6fa67c16ca..f77acad48a 100644 --- a/i18n/tests/test_generate.py +++ b/i18n/tests/test_generate.py @@ -3,13 +3,15 @@ import os import string import random import re -from unittest import TestCase +from unittest import TestCase +from mock import patch from polib import pofile from pytz import UTC from i18n import extract from i18n import generate +from i18n import dummy from i18n.config import CONFIGURATION @@ -19,6 +21,11 @@ class TestGenerate(TestCase): """ generated_files = ('django-partial.po', 'djangojs-partial.po', 'mako.po') + @classmethod + def setUpClass(cls): + extract.main() + dummy.main() + def setUp(self): # Subtract 1 second to help comparisons with file-modify time succeed, # since os.path.getmtime() is not millisecond-accurate @@ -28,12 +35,12 @@ class TestGenerate(TestCase): """ Tests merge script on English source files. """ - extract.main() filename = os.path.join(CONFIGURATION.source_messages_dir, random_name()) generate.merge(CONFIGURATION.source_locale, target=filename) self.assertTrue(os.path.exists(filename)) os.remove(filename) + @patch.object(CONFIGURATION, 'locales', ['eo']) def test_main(self): """ Runs generate.main() which should merge source files, diff --git a/rakelib/i18n.rake b/rakelib/i18n.rake index e5ff74c1d6..3bd8de77d0 100644 --- a/rakelib/i18n.rake +++ b/rakelib/i18n.rake @@ -14,7 +14,7 @@ namespace :i18n do desc "Simulate international translation by generating dummy strings corresponding to source strings." task :dummy => "i18n:extract" do - sh(File.join(REPO_ROOT, "i18n", "make_dummy.py")) + sh(File.join(REPO_ROOT, "i18n", "dummy.py")) end namespace :validate do