def test_it_raises_an_exepction_when_the_useTranslation_is_not_initialized( self): with self.assertRaises(BaseException) as context: find_translations_in_string("t('lol')") self.assertTrue( "The translations are not initialized" in str(context.exception))
def run(path, config: dict[str: str]): used_translations = {} result = list(Path(path).rglob("*.tsx")) for p in result: with open(p, 'r') as f: source_code = f.read() if (has_translations(source_code)): translations = find_translations_in_string(source_code, config['default_namespace']) for t in translations: if not t.namespance + '.' + t.key in used_translations: used_translations[t.namespance + '.' + t.key] = 1 used_translations[t.namespance + '.' + t.key] = used_translations[t.namespance + '.' + t.key] + 1 translations = list(Path(config['translations_folder']).rglob("*.json")) translations_in_json = [] for tf in translations: with open(tf, 'r') as translations_file: content = json.load(translations_file) translations_in_json += get_translation_in_json(content, tf.name.replace(".json", "")) with open(config['report_path'], 'w+') as f: for ut in used_translations: if ut not in translations_in_json: f.write(ut) f.write("\n") print('done!')
def test_get_namespace_from_t_func(self): result = find_translations_in_string(""" const { t } = useTranslation(); ... {t('test.lol')} """) self.assertTrue(len(result) == 1) self.assertEqual('test', result[0].namespance)
def test_get_chained_key(self): result = find_translations_in_string(""" const { t } = useTranslation(); ... {t('test.foo.bar')} """) self.assertTrue(len(result) == 1) self.assertEqual('test', result[0].namespance) self.assertEqual('foo.bar', result[0].key)
def test_is_returns_a_translations_with_t_with_type(self): result = find_translations_in_string(""" const { t: tBase } = useTranslation('product'); ... {t('lol')} ... {t('xpto')} """) self.assertTrue(len(result) == 2)
def test_get_with_default_namespace(self): result = find_translations_in_string( """ const { t } = useTranslation(); ... {t('bar')} """, "test") self.assertTrue(len(result) == 1) self.assertEqual('test', result[0].namespance) self.assertEqual('bar', result[0].key)
def test_supports_multiple_destructuring(self): result = find_translations_in_string( """ const { t, i18n } = useTranslation() ... {t('bar')} """, "test") self.assertTrue(len(result) == 1) self.assertEqual('test', result[0].namespance) self.assertEqual('bar', result[0].key)