def update_branch_for_legalcode(self, repo, legalcode, branch_object): """ Pull down the latest translation for the legalcode and update the local .po and .mo files. Assumes the correct branch has already been checked out. Adds the updated files to the index. """ resource_slug = legalcode.license.resource_slug self.say(2, f"\tUpdating {resource_slug} {legalcode.language_code}") last_tx_update = iso8601.parse_date(self.stats[resource_slug][ legalcode.language_code]["translated"]["last_activity"]) legalcode.translation_last_update = last_tx_update branch_object.legalcodes.add(legalcode) if (branch_object.last_transifex_update is None or branch_object.last_transifex_update < last_tx_update): branch_object.last_transifex_update = last_tx_update # Get the updated translation. We don't write it to a file yet. # We'll do all the updates for a branch at once in the next section. pofile_path = legalcode.translation_filename() pofile_content = self.transifex_get_pofile_content( resource_slug, legalcode.language_code) filenames = save_content_as_pofile_and_mofile(pofile_path, pofile_content) relpaths = [ os.path.relpath(filename, settings.TRANSLATION_REPOSITORY_DIRECTORY) for filename in filenames ] repo.index.add(relpaths)
def test_save_content_as_pofile_and_mofile(self): path = "/foo/bar.po" content = b"xxxxxyyyyy" with mock.patch("i18n.utils.polib") as mock_polib: return_value = save_content_as_pofile_and_mofile(path, content) self.assertEqual(("/foo/bar.po", "/foo/bar.mo"), return_value) mock_polib.pofile.assert_called_with(pofile=content.decode(), encoding="utf-8") pofile = mock_polib.pofile.return_value pofile.save.assert_called_with(path) pofile.save_as_mofile.assert_called_with("/foo/bar.mo")