def read(self, google_doc_name): worksheet = self._get_worksheet(google_doc_name) worksheet_cells = worksheet.get_all_values(returnas='cell', include_empty=False) log_step(str(worksheet_cells)) languages_row = worksheet_cells[0] values_rows = worksheet_cells[1:] languages = self._load_languages(languages_row) entries = self._read_strings(values_rows) dictionary = Dictionary() for i, lang in enumerate(languages): for row in entries: value_idx = i + 1 key = row[0] value = row[value_idx] if value_idx < len(row) else None if len(key) == 0: continue else: if value is None and self.keys_as_values_if_empty: value = key dictionary.add_translation(key, lang, value) return dictionary
class TestAndroidFormatter(unittest.TestCase): def setUp(self): self.dictionary = Dictionary() self.dictionary.add_translation('hello', 'en', 'Hi') self.dictionary.add_translation('welcome', 'en', 'Welcome') def _generate_result(self, elements=()): result = "<resources>" for element in elements: result += '<string name="%s">%s</string>' % element result += "</resources>" return result def test_template(self): formatter = DictionaryToAndroid(dictionary=self.dictionary, use_pretty_xml=False) formatted_string = formatter.format('en') dictionary_result = self._generate_result( (('hello', 'Hi'), ('welcome', 'Welcome'))) self.assertEqual(formatted_string, dictionary_result) def test_empty(self): self.dictionary.clear() formatter = DictionaryToAndroid(dictionary=self.dictionary, use_pretty_xml=False) result = formatter.format('en') expected_result = self._generate_result() self.assertEqual(result, expected_result)
def test_values_insertion(self): dictionary = Dictionary() dictionary.add_translation(self.key_welcome, self.lang_pl, self.pl_hello) self.assertEqual(self.pl_hello, dictionary.get_translation(self.key_welcome, self.lang_pl)) dictionary.add_translation(self.key_welcome, self.lang_pl, self.pl_hello_2) self.assertEqual(self.pl_hello_2, dictionary.get_translation(self.key_welcome, self.lang_pl))
def test_add(self): dict1 = Dictionary() dict2 = Dictionary() dict1.add_translation('hello', 'en', 'Hello') dict2.add_translation('hello', 'pl', 'Czesc') new_dict = dict1 + dict2 self.assertEqual(len(new_dict.languages), 2) self.assertEqual(len(new_dict.keys()), 1) self.assertEqual('Hello', new_dict.get_translation('hello', 'en')) self.assertEqual('Czesc', new_dict.get_translation('hello', 'pl'))
class TestSwiftFormatter(unittest.TestCase): def setUp(self): self.dictionary = Dictionary() self.dictionary.add_translation('hello', 'en', 'Hi') self.dictionary.add_translation('welcome', 'en', 'Welcome') self.dictionary_result = '''"hello" = "Hi";\n"welcome" = "Welcome";\n''' def test_template(self): formatter = DictionaryToSwift(self.dictionary) formatted_string = formatter.format('en') self.assertEqual(formatted_string, self.dictionary_result) def test_empty(self): self.dictionary.clear() formatter = DictionaryToSwift(self.dictionary) formatted_string = formatter.format('en') self.assertTrue(len(formatted_string) == 0)
def test_dictionary_keys(self): dictionary = Dictionary() dictionary.add_translation(self.key_welcome, self.lang_pl, self.pl_hello) dictionary.add_translation(self.key_bye, self.lang_pl, self.pl_bye) self.assertEqual(2, len(dictionary.keys())) self.assertEqual(1, len(dictionary.languages)) dictionary.add_translation(self.key_bye, self.lang_en, self.en_bye) self.assertEqual(2, len(dictionary.keys())) self.assertEqual(2, len(dictionary.languages))