def test_string_freeze_breakage_outsourced(self): """Check string breakage for outsourced projects.""" timestamp = datetime.datetime.now() - datetime.timedelta(hours=1) timestamp2 = datetime.datetime.now() + datetime.timedelta(hours=1) self.release.stringfreeze_date = timestamp self.release.develfreeze_date = timestamp2 self.release.save() ## Loading POT (en_US) into the self.resource3 handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) # Resource 3 - Outsourced handler.bind_resource(self.resource3) # We are listing to the post_save_translation signal raised in # _post_save2db method. handler.save2db(is_source=True) # Sorted mails list mails = key_sort(mail.outbox, 'to') self.assertEqual(len(mails), 3) self.assertEqual( mails[0].subject, self._gen_assert_msg('[localhost] Release string ' 'freeze breakage: Release1')) self.assertEqual(mails[0].to, ['maintainer3@localhost']) self.assertEqual(mails[1].to, ['maintainer@localhost']) self.assertEqual(mails[2].to, ['team_coordinator@localhost'])
def test_pot_parser(self): """POT file tests.""" # Parsing POT file handler = POHandler('%s/tests.pot' % os.path.split(__file__)[0]) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) self.stringset = handler.stringset entities = 0 # POT has no associated language self.assertEqual(self.stringset.target_language, None) for s in self.stringset: # Testing if source entity and translation are the same if not s.pluralized: self.assertEqual(s.source_entity, s.translation) # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) # Counting number of entities if s.rule == 5: entities += 1 # Asserting number of entities - POT file has 3 entries. self.assertEqual(entities, 6)
def test_string_freeze_breakage_outsourced(self): """Check string breakage for outsourced projects.""" timestamp = datetime.datetime.now() - datetime.timedelta(hours=1) timestamp2 = datetime.datetime.now() + datetime.timedelta(hours=1) self.release.stringfreeze_date = timestamp self.release.develfreeze_date = timestamp2 self.release.save() ## Loading POT (en_US) into the self.resource3 handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) # Resource 3 - Outsourced handler.bind_resource(self.resource3) # We are listing to the post_save_translation signal raised in # _post_save2db method. handler.save2db(is_source=True) # Sorted mails list mails = key_sort(mail.outbox, 'to') self.assertEqual(len(mails), 3) self.assertEqual(mails[0].subject, self._gen_assert_msg( '[localhost] Release string ' 'freeze breakage: Release1')) self.assertEqual(mails[0].to, ['maintainer3@localhost']) self.assertEqual(mails[1].to, ['maintainer@localhost']) self.assertEqual(mails[2].to, ['team_coordinator@localhost'])
def test_order(self): handler = POHandler(os.path.join( os.path.dirname(__file__), 'copyright.po') ) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) cr = Copyright.objects.assign( language=self.language_en, resource=self.resource, owner='CC', year='2014') cr = Copyright.objects.assign( language=self.language_en, resource=self.resource, owner='ZZ', year='2014') cr = Copyright.objects.assign( language=self.language_en, resource=self.resource, owner='BA', year='2015') handler.compile() lines_iterator = handler.compiled_template.split("\n") for n, line in enumerate(lines_iterator): if line == "## Translators:": break line = lines_iterator[n + 1] self.assertTrue('AB' in line) line = lines_iterator[n + 3] self.assertTrue('BA' in line) line = lines_iterator[n + 4] self.assertTrue('CC' in line) line = lines_iterator[n + 6] self.assertTrue('ZZ' in line)
def test_pot_parser(self): """POT file tests.""" # Parsing POT file handler = POHandler('%s/tests.pot' % os.path.split(__file__)[0]) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) self.stringset = handler.stringset entities = 0 # POT has no associated language self.assertEqual(self.stringset.target_language, None) for s in self.stringset.strings: # Testing if source entity and translation are the same if not s.pluralized: self.assertEqual(s.source_entity, s.translation) # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) # Counting number of entities if s.rule == 5: entities += 1 # Asserting number of entities - POT file has 3 entries. self.assertEqual(entities, 6)
def test_po_parser_ar(self): """Tests for ar PO file.""" handler = POHandler('%s/ar.po' % os.path.split(__file__)[0]) handler.set_language(self.language_ar) handler.parse_file() self.stringset = handler.stringset nplurals = 0 for s in self.stringset.strings: # Testing if source entity and translation are NOT the same self.assertNotEqual(s.source_entity, s.translation) # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) if s.source_entity == '{0} result' and s.pluralized: nplurals += 1 # Asserting nplurals based on the number of plurals of the # '{0 results}' entity - ar has nplurals=6. self.assertEqual(nplurals, 6)
def test_po_save2db(self): """Test creating source strings from a PO/POT file works""" handler = POHandler('%s/tests.pot' % os.path.split(__file__)[0]) l = Language.objects.get(code='en_US') handler.set_language(l) handler.parse_file(is_source=True) r = self.resource handler.bind_resource(r) handler.save2db(is_source=True) self.assertEqual( SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len(Translation.objects.filter(source_entity__resource=r, language=l)), 7) handler.bind_file('%s/ar.po' % os.path.split(__file__)[0]) l = Language.objects.by_code_or_alias('ar') handler.set_language(l) handler.parse_file() handler.save2db() self.assertEqual( SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len(Translation.objects.filter(source_entity__resource=r, language=l)), 11) r.delete()
def test_order(self): handler = POHandler( os.path.join(os.path.dirname(__file__), 'copyright.po')) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) cr = Copyright.objects.assign(language=self.language_en, resource=self.resource, owner='CC', year='2014') cr = Copyright.objects.assign(language=self.language_en, resource=self.resource, owner='ZZ', year='2014') cr = Copyright.objects.assign(language=self.language_en, resource=self.resource, owner='BA', year='2015') compiled_template = handler.compile() lines_iterator = compiled_template.split("\n") for n, line in enumerate(lines_iterator): if line == "# Translators:": break line = lines_iterator[n + 1] self.assertTrue('AB' in line) line = lines_iterator[n + 3] self.assertTrue('BA' in line) line = lines_iterator[n + 4] self.assertTrue('CC' in line) line = lines_iterator[n + 6] self.assertTrue('ZZ' in line)
def test_copyright_on_save(self): handler = POHandler( os.path.join(os.path.dirname(__file__), 'copyright.po')) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) self.assertIn("AB", handler.compile())
def test_po_parser_pt_BR_with_warning_messages(self): """ Tests if nplural warning is raised for 'pt_BR' PO file loaded as an 'ar' language. """ handler = POHandler('%s/pt_BR.po' % os.path.split(__file__)[0]) handler.set_language(self.language_ar) handler.parse_file() self.assertTrue('nplural' in handler.warning_messages.keys())
def test_copyright_on_save(self): handler = POHandler(os.path.join( os.path.dirname(__file__), 'copyright.po') ) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) self.assertIn("AB", handler.compile())
def test_po_parser_pt_BR_with_warning_messages(self): """ Tests if nplural warning is raised for 'pt_BR' PO file loaded as an 'ar' language. """ handler = POHandler('%s/pt_BR.po' % os.path.split(__file__)[0]) handler.set_language(self.language_ar) handler.parse_file() self.assertTrue('nplural' in handler.warning_messages.keys())
def _load_pot(self): test_file = os.path.join(TEST_FILES_PATH, 'test.pot') # First empty our resource self.resource.entities.all().delete() # Load file handler = POHandler(test_file) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) return handler
def test_general_po(self): """ Test with a PO file containing multiple different 'breakable' cases. """ # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Import file with two senteces handler = POHandler('%s/general/test.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) handler.compile() exported_file = polib.pofile(handler.compiled_template) for entry in exported_file: se = SourceEntity.objects.get( string = entry.msgid, context = entry.msgctxt or 'None', resource = self.resource ) if se.pluralized: plurals = Translation.objects.filter( source_entity__resource = self.resource, language = self.resource.source_language, source_entity__string = entry.msgid ).order_by('rule') plural_keys = {} # last rule excluding other(5) lang_rules = self.resource.source_language.get_pluralrules_numbers() # Initialize all plural rules up to the last for p,n in enumerate(lang_rules): plural_keys[str(p)] = "" for n,p in enumerate(plurals): plural_keys[str(n)] = p.string self.assertEqual(entry.msgstr_plural, plural_keys) else: trans = se.get_translation( self.resource.source_language.code, rule=5 ) self.assertEqual(entry.msgstr, trans.string.encode('utf-8'), "Source '%s'"\ " differs from translation %s" % (entry.msgstr, trans.string.encode('utf-8')))
def _load_pot(self): test_file = os.path.join(TEST_FILES_PATH, 'test.pot') # First empty our resource self.resource.entities.all().delete() # Load file handler = POHandler(test_file) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) return handler
def test_headers_on_pot(self): handler = POHandler(os.path.join( os.path.dirname(__file__), 'tests.pot') ) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) handler.compile() self.assertFalse("FIRST AUTHOR" in handler.compiled_template) handler.compile_pot() self.assertTrue("FIRST AUTHOR" in handler.compiled_template)
def test_poheader_load_soureclang(self): """Test load of existing PO file with copyright headers.""" test_file = os.path.join(settings.TX_ROOT, './resources/tests/lib/pofile/copyright.po') handler = POHandler(test_file) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) c = Copyright.objects.filter(resource=self.resource, language=self.resource.source_language) self.assertEquals(len(c), 3)
def test_poheader_load_soureclang(self): """Test load of existing PO file with copyright headers.""" test_file = os.path.join(settings.TX_ROOT, './resources/tests/lib/pofile/copyright.po') handler = POHandler(test_file) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) c = Copyright.objects.filter( resource=self.resource, language=self.resource.source_language ) self.assertEquals(len(c), 3)
def test_headers_on_pot(self): handler = POHandler( os.path.join(os.path.dirname(__file__), 'tests.pot')) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) self.assertNotIn("FIRST AUTHOR", handler.compile()) handler = POTHandler( os.path.join(os.path.dirname(__file__), 'tests.pot')) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) self.assertIn("FIRST AUTHOR", handler.compile())
def test_convert_to_suggestions(self): """Test convert to suggestions when importing new source files""" # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Make sure that we have no suggestions to begin with self.assertEqual(Suggestion.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 0) # Import file with two senteces handler = POHandler('%s/suggestions/tests.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # import pt_BR translation handler = POHandler('%s/suggestions/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual(Translation.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 5) # import source with small modifications handler = POHandler('%s/suggestions/tests-diff.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # Make sure that all suggestions were added self.assertEqual(Suggestion.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 2) # Make both strings are now untranslated self.assertEqual(Translation.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 3) # import pt_BR translation again handler = POHandler('%s/suggestions/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual(Translation.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 3)
def test_general_po(self): """ Test with a PO file containing multiple different 'breakable' cases. """ # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Import file with two senteces handler = POHandler('%s/general/test.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) exported_file = polib.pofile(handler.compile()) for entry in exported_file: se = SourceEntity.objects.get(string=entry.msgid, context=entry.msgctxt or 'None', resource=self.resource) if se.pluralized: plurals = Translation.objects.filter( source_entity__resource=self.resource, language=self.resource.source_language, source_entity__string=entry.msgid).order_by('rule') plural_keys = {} # last rule excluding other(5) lang_rules = self.resource.source_language.get_pluralrules_numbers( ) # Initialize all plural rules up to the last for p, n in enumerate(lang_rules): plural_keys[str(p)] = "" for n, p in enumerate(plurals): plural_keys[str(n)] = p.string self.assertEqual(entry.msgstr_plural, plural_keys) else: trans = se.get_translation(self.resource.source_language.code, rule=5) self.assertEqual(entry.msgstr, trans.string.encode('utf-8'), "Source '%s'"\ " differs from translation %s" % (entry.msgstr, trans.string.encode('utf-8')))
def test_logical_ids(self): """Test po files with logical ids instead of normal strings""" # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Make sure that we have no suggestions to begin with self.assertEqual( Suggestion.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 0) # Import file with two senteces handler = POHandler('%s/logical_ids/tests.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # import pt_BR translation handler = POHandler('%s/logical_ids/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual( Translation.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 2) source = SourceEntity.objects.get(resource=self.resource) en_trans = Translation.objects.get( source_entity__resource=self.resource, language=self.resource.source_language) pt_trans = Translation.objects.get( source_entity__resource=self.resource, language=self.language) # Check to see that the correct strings appear as the translations and # not the logical id self.assertEqual(en_trans.string, "Hello, World!") self.assertEqual(pt_trans.string, "Holas, Amigos!") self.assertEqual(source.string, "source_1")
def test_logical_ids(self): """Test po files with logical ids instead of normal strings""" # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Make sure that we have no suggestions to begin with self.assertEqual(Suggestion.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 0) # Import file with two senteces handler = POHandler('%s/logical_ids/tests.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # import pt_BR translation handler = POHandler('%s/logical_ids/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual(Translation.objects.filter(source_entity__in= SourceEntity.objects.filter(resource=self.resource).values('id')).count(), 2) source = SourceEntity.objects.get(resource=self.resource) en_trans = Translation.objects.get(source_entity__resource=self.resource, language = self.resource.source_language) pt_trans = Translation.objects.get(source_entity__resource=self.resource, language = self.language) # Check to see that the correct strings appear as the translations and # not the logical id self.assertEqual(en_trans.string, "Hello, World!") self.assertEqual(pt_trans.string, "Holas, Amigos!") self.assertEqual(source.string, "source_1")
def _test_po_save2db(self): """Test creating source strings from a PO/POT file works""" handler = POHandler('%s/tests.pot' % os.path.split(__file__)[0]) l = Language.objects.get(code='en_US') handler.set_language(l) handler.parse_file(is_source=True) r = self.resource handler.bind_resource(r) handler.save2db(is_source=True) self.assertEqual(SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len( Translation.objects.filter(source_entity__resource=r, language=l)), 7) handler.bind_file('%s/ar.po' % os.path.split(__file__)[0]) l = Language.objects.by_code_or_alias('ar') handler.set_language(l) handler.parse_file() handler.save2db() self.assertEqual(SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len( Translation.objects.filter(source_entity__resource=r, language=l)), 11) self._mark_translation_as_reviewed(self.resource, [ '{0} result', 'Location', ], self.language_ar, 7) #update resource with the same source file and #check that the resource.last_update does not #change from its previous value last_update = self.resource.last_update handler.bind_file('%s/tests.pot' % os.path.split(__file__)[0]) l = Language.objects.get(code='en_US') handler.set_language(l) handler.parse_file(True) handler.save2db(is_source=True) self.assertEqual(self.resource.last_update, last_update) self.assertEqual(SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len( Translation.objects.filter(source_entity__resource=r, language=l)), 7) return handler
def test_po_parser_pt_BR(self): """Tests for pt_BR PO file.""" handler = POHandler('%s/pt_BR.po' % os.path.split(__file__)[0]) handler.set_language(self.language) handler.parse_file() self.stringset = handler.stringset nplurals = 0 for s in self.stringset: # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) if s.source_entity == '{0} result' and s.pluralized: nplurals += 1 # Asserting nplurals based on the number of plurals of the # '{0 results}' entity - pt_BR has nplurals=2 self.assertEqual(nplurals, 2)
def _test_po_save2db(self): """Test creating source strings from a PO/POT file works""" handler = POHandler('%s/tests.pot' % os.path.split(__file__)[0]) l = Language.objects.get(code='en_US') handler.set_language(l) handler.parse_file(is_source=True) r = self.resource handler.bind_resource(r) handler.save2db(is_source=True) self.assertEqual( SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len(Translation.objects.filter(source_entity__resource=r, language=l)), 7) handler.bind_file('%s/ar.po' % os.path.split(__file__)[0]) l = Language.objects.by_code_or_alias('ar') handler.set_language(l) handler.parse_file() handler.save2db() self.assertEqual( SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len(Translation.objects.filter(source_entity__resource=r, language=l)), 11) self._mark_translation_as_reviewed(self.resource, [ '{0} result', 'Location', ], self.language_ar, 7 ) #update resource with the same source file and #check that the resource.last_update does not #change from its previous value last_update = self.resource.last_update handler.bind_file('%s/tests.pot' % os.path.split(__file__)[0]) l = Language.objects.get(code='en_US') handler.set_language(l) handler.parse_file(True) handler.save2db(is_source=True) self.assertEqual(self.resource.last_update, last_update) self.assertEqual( SourceEntity.objects.filter(resource=r).count(), 6) self.assertEqual( len(Translation.objects.filter(source_entity__resource=r, language=l)), 7) return handler
def test_string_freeze_breakage(self): """Check string breakage for non-outsourced projects.""" timestamp = datetime.datetime.now() - datetime.timedelta(hours=1) timestamp2 = datetime.datetime.now() + datetime.timedelta(hours=1) self.release.stringfreeze_date = timestamp self.release.develfreeze_date = timestamp2 self.release.save() ## Loading POT (en_US) into the self.resource4 handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) # Resource 4 - Not outsourced handler.bind_resource(self.resource4) # We are listing to the post_save_translation signal raised in # _post_save2db method. handler.save2db(is_source=True) # Should not send any notification once the project do not outsources # its teams self.assertEqual(len(mail.outbox), 0)
def test_string_freeze_breakage(self): """Check string breakage for non-outsourced projects.""" timestamp = datetime.datetime.now() - datetime.timedelta(hours=1) timestamp2 = datetime.datetime.now() + datetime.timedelta(hours=1) self.release.stringfreeze_date = timestamp self.release.develfreeze_date = timestamp2 self.release.save() ## Loading POT (en_US) into the self.resource4 handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) # Resource 4 - Not outsourced handler.bind_resource(self.resource4) # We are listing to the post_save_translation signal raised in # _post_save2db method. handler.save2db(is_source=True) # Should not send any notification once the project do not outsources # its teams self.assertEqual(len(mail.outbox), 0)
def setUp(self): self.current_path = os.path.split(__file__)[0] super(APIBaseTests, self).setUp() # Opening JSON data for pushing through the API self.data = ORIGINAL self.trans = TRANSLATION self.pofile_path = os.path.join(settings.TX_ROOT, 'resources/tests/lib/pofile') # Loading POT (en_US) into the resource handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) handler.bind_resource(self.resource) handler.save2db(is_source=True) # Loading PO (pt_BR) into the resource handler.bind_file('%s/pt_BR.po' % self.pofile_path) handler.set_language(self.language) handler.parse_file() handler.save2db()
def setUp(self): self.current_path = os.path.split(__file__)[0] super(APIBaseTests, self).setUp() # Opening JSON data for pushing through the API self.data = ORIGINAL self.trans = TRANSLATION self.pofile_path = os.path.join( settings.TX_ROOT, 'resources/tests/lib/pofile' ) # Loading POT (en_US) into the resource handler = POHandler('%s/tests.pot' % self.pofile_path) handler.set_language(self.language_en) handler.parse_file(is_source=True) handler.bind_resource(self.resource) handler.save2db(is_source=True) # Loading PO (pt_BR) into the resource handler.bind_file('%s/pt_BR.po' % self.pofile_path) handler.set_language(self.language) handler.parse_file() handler.save2db()
def test_po_parser_ar(self): """Tests for ar PO file.""" handler = POHandler('%s/ar.po' % os.path.split(__file__)[0]) handler.set_language(self.language_ar) handler.parse_file() self.stringset = handler.stringset nplurals = 0 for s in self.stringset: # Testing if source entity and translation are NOT the same self.assertNotEqual(s.source_entity, s.translation) # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) if s.source_entity == '{0} result' and s.pluralized: nplurals += 1 # Asserting nplurals based on the number of plurals of the # '{0 results}' entity - ar has nplurals=6. self.assertEqual(nplurals, 6)
def test_po_parser_pt_BR(self): """Tests for pt_BR PO file.""" handler = POHandler('%s/pt_BR.po' % os.path.split(__file__)[0]) handler.set_language(self.language) handler.parse_file() self.stringset = handler.stringset nplurals = 0 for s in self.stringset.strings: # Testing plural number if s.source_entity == '{0} results': self.assertEqual(s.rule, 5) if s.source_entity == '{0} result' and s.pluralized: nplurals += 1 # Asserting nplurals based on the number of plurals of the # '{0 results}' entity - pt_BR has nplurals=2 self.assertEqual(nplurals, 2)
def test_convert_to_suggestions(self): """Test convert to suggestions when importing new source files""" # Empty our resource SourceEntity.objects.filter(resource=self.resource).delete() # Make sure that we have no suggestions to begin with self.assertEqual( Suggestion.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 0) # Import file with two senteces handler = POHandler('%s/suggestions/tests.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # import pt_BR translation handler = POHandler('%s/suggestions/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual( Translation.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 5) # import source with small modifications handler = POHandler('%s/suggestions/tests-diff.pot' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.resource.source_language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # Make sure that all suggestions were added self.assertEqual( Suggestion.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 2) # Make both strings are now untranslated self.assertEqual( Translation.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 3) # import pt_BR translation again handler = POHandler('%s/suggestions/pt_BR.po' % os.path.split(__file__)[0]) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file() handler.save2db() # Make sure that we have all translations in the db self.assertEqual( Translation.objects.filter( source_entity__in=SourceEntity.objects.filter( resource=self.resource).values('id')).count(), 3)
def handle(self, *args, **options): # OMG!1! Dirty fix for circular importing issues. Didn't want to dig # into it because it's probably not worth, once it's a tmp code. from transifex.resources.formats import get_i18n_type_from_file from transifex.resources.formats.pofile import POHandler from transifex.languages.models import Language from transifex.projects.models import Project from transifex.resources.models import Resource force = options.get('force') if settings.DEBUG: msg = "You are running this command with DEBUG=True. Please " \ "change it to False in order to avoid problems with " \ "allocating memory." raise CommandError(msg) msg = None if len(args) == 0: jsonmaps = JSONMap.objects.all() else: jsonmaps = JSONMap.objects.filter(project__slug__in=args) if not jsonmaps: msg = "No mapping found for given project slug(s): %s" % ', '.join(args) if not jsonmaps: raise CommandError(msg or "No mapping found in the database.") for jsonmap in jsonmaps: jm = jsonmap.loads(True) # Check whether the map was already migrated or not if jm['meta'].get('_migrated', None) and not force: logger.debug("Project '%s' was already migrated." % jsonmap.project) continue for r in jm['resources']: logger.debug("Pushing resource: %s" % r.get('resource_slug')) project = jsonmap.project # Path for cached files of project.component path = os.path.join(settings.MSGMERGE_DIR, '%s.%s' % (project.slug, jsonmap.slug)) if os.path.exists(path): resource_slug = r['resource_slug'] language = Language.objects.by_code_or_alias_or_none( r['source_lang']) # Create resource and load source language if language: resource, created = Resource.objects.get_or_create( slug = resource_slug, source_language = language, project = project) if created: resource.name = '%s - %s' % (jsonmap.slug, r['source_file']) source_file = os.path.join(path, r['source_file']) resource.i18n_type = get_i18n_type_from_file(source_file) resource.save() resource.url_info = URLInfo(source_file_url=r['autofetch_url']) logger.debug("Inserting source strings from %s (%s) to " "'%s'." % (r['source_file'], language.code, resource.full_name)) fhandler = POHandler(filename=source_file) fhandler.bind_resource(resource) fhandler.set_language(language) try: fhandler.contents_check(fhandler.filename) fhandler.parse_file(True) strings_added, strings_updated = fhandler.save2db(True) except Exception, e: resource.delete() sys.stdout.write((u"Resource not created! Could not import " "file '%s': %s.\n" % (source_file, str(e))).encode('UTF-8')) # Skip adding translations, as the resource # wasn't created. continue logger.debug("Inserting translations for '%s' (%s)." % (resource.slug, project)) # Load translations for code, f in r['translations'].items(): language = Language.objects.by_code_or_alias_or_none(code) if language: translation_file = os.path.join(path, f['file']) try: fhandler = POHandler(filename=translation_file) fhandler.set_language(language) fhandler.bind_resource(resource) fhandler.contents_check(fhandler.filename) fhandler.parse_file() strings_added, strings_updated = fhandler.save2db() except Exception, e: sys.stdout.write((u"Could not import file '%s': %s" % (translation_file, str(e))).encode('UTF-8')) else: logger.debug("Mapping '%s' does not have cached files " "under %s." % (jsonmap, path))