def test_translations_in_generated_files(self): all_translations = { 'sheet1': list(translations), 'sheet2': list(translations), } po_file_generator = PoFileGenerator(all_translations, {}) file_paths = [] try: for file_name, file_path in po_file_generator.generated_files: file_paths.append(file_path) list_of_translations = polib.pofile(file_path) # assure translations self.assertEqual(list_of_translations[0].msgid, 'hello') self.assertEqual(list_of_translations[0].msgstr, 'नमस्ते') self.assertEqual(list_of_translations[0].msgctxt, '0:occurrence-hello') self.assertEqual(list_of_translations[0].occurrences, [('occurrence-hello', '')]) self.assertEqual(list_of_translations[1].msgid, 'bye') self.assertEqual(list_of_translations[1].msgstr, 'अलविदा') self.assertEqual(list_of_translations[1].msgctxt, '0:occurrence-bye') self.assertEqual(list_of_translations[1].occurrences, [('occurrence-bye', '')]) finally: po_file_generator.cleanup() # assure files are cleaned for file_path in file_paths: self.assertFalse(os.path.exists(file_path))
def _generate_po_file(self, worksheet): """ extract translations from worksheet and converts to a po file :return: PoFileGenerator object """ translations = self._generate_translations_for_po(worksheet) po_file_generator = PoFileGenerator(translations, {}) return po_file_generator
def test_metadata(self): all_translations = {'sheet1': list(translations)} metadata = { 'MIME-Version': '1.0', 'Content-Type': 'text/plain; charset=utf-8', 'Language': 'hin', } po_file_generator = PoFileGenerator(all_translations, metadata) try: for file_name, file_path in po_file_generator.generated_files: # ensure meta data with open(file_path, encoding='utf-8') as f: file_content = f.read() self.assertIn("Language: hin", file_content) self.assertIn("MIME-Version: 1.0", file_content) self.assertIn("Content-Type: text/plain; charset=utf-8", file_content) finally: po_file_generator.cleanup()
def _generate_po_content(self, worksheet): """ extract translations from worksheet and converts to a po file :return: content of file generated """ translations = self._generate_translations_for_po(worksheet) with PoFileGenerator(translations, {}) as po_file_generator: generated_files = po_file_generator.generate_translation_files() with open(generated_files[0].path, 'r', encoding="utf-8") as f: return f.read()
def send_translation_files(self): """ submit translation files to transifex """ app_trans_generator = AppTranslationsGenerator( self.domain, self.app_id_to_build, self.version, self.key_lang, self.source_lang, self.lang_prefix, self.exclude_if_default, self.use_version_postfix) with PoFileGenerator( app_trans_generator.translations, app_trans_generator.metadata) as po_file_generator: generated_files = po_file_generator.generate_translation_files() return self._send_files_to_transifex(generated_files, app_trans_generator)