diff --git a/i18n/config.py b/i18n/config.py index 7151b03..6dc531c 100644 --- a/i18n/config.py +++ b/i18n/config.py @@ -23,7 +23,6 @@ 'error_on_missing_plural': False, 'encoding': 'utf-8', 'namespace_delimiter': '.', - 'dir_as_namespace': True, 'plural_few': 5 } diff --git a/i18n/resource_loader.py b/i18n/resource_loader.py index 2bceb74..1da9f4c 100644 --- a/i18n/resource_loader.py +++ b/i18n/resource_loader.py @@ -43,11 +43,11 @@ def load_config(filename): for key, value in settings_data.items(): config.settings[key] = value -def get_namespace_from_filepath(filepath): - namespace = os.path.dirname(filepath).strip(os.sep).replace(os.sep, config.get('namespace_delimiter')) +def get_namespace_from_filepath(filename): + namespace = os.path.dirname(filename).strip(os.sep).replace(os.sep, config.get('namespace_delimiter')) if '{namespace}' in config.get('filename_format'): try: - splitted_filename = os.path.basename(filepath).split('.') + splitted_filename = os.path.basename(filename).split('.') if namespace: namespace += config.get('namespace_delimiter') namespace += splitted_filename[config.get('filename_format').index('{namespace}')] @@ -55,9 +55,9 @@ def get_namespace_from_filepath(filepath): raise I18nFileLoadError("incorrect file format.") return namespace -def load_translation_file(filepath, base_directory, locale=config.get('locale')): - translations_dic = load_resource(os.path.join(base_directory, filepath), locale) - namespace = get_namespace_from_filepath(filepath) +def load_translation_file(filename, base_directory, locale=config.get('locale')): + translations_dic = load_resource(os.path.join(base_directory, filename), locale) + namespace = get_namespace_from_filepath(filename) load_translation_dic(translations_dic, namespace) def load_translation_dic(dic, namespace): @@ -69,13 +69,24 @@ def load_translation_dic(dic, namespace): else: translations.add(namespace + key, value) +def load_directory(directory, locale=config.get('locale')): + for f in os.listdir(directory): + path = os.path.join(directory, f) + if os.path.isfile(path) and path.endswith(config.get('file_format')): + load_translation_file(f, directory, locale) + + def search_translation(key, locale=config.get('locale')): splitted_key = key.split(config.get('namespace_delimiter')) if not splitted_key: return namespace = splitted_key[:-1] - for directory in config.get('load_path'): - recursive_search_dir(namespace, '', directory, locale) + if not namespace and '{namespace}' not in config.get('filename_format'): + for directory in config.get('load_path'): + load_directory(directory, locale) + else: + for directory in config.get('load_path'): + recursive_search_dir(namespace, '', directory, locale) def recursive_search_dir(splitted_namespace, directory, root_dir, locale=config.get('locale')): if not splitted_namespace: diff --git a/i18n/tests/loader_tests.py b/i18n/tests/loader_tests.py index 2fa0f37..e537248 100644 --- a/i18n/tests/loader_tests.py +++ b/i18n/tests/loader_tests.py @@ -118,6 +118,14 @@ def test_search_translation_json(self): resource_loader.search_translation('bar.baz.qux') self.assertTrue(translations.has('bar.baz.qux')) + @unittest.skipUnless(json_available, "json library not available") + def test_search_translation_without_ns(self): + resource_loader.init_json_loader() + config.set('file_format', 'json') + config.set('filename_format', '{locale}.{format}') + resource_loader.search_translation('foo') + self.assertTrue(translations.has('foo')) + suite = unittest.TestLoader().loadTestsFromTestCase(TestFileLoader) unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/i18n/tests/resources/translations/en.json b/i18n/tests/resources/translations/en.json new file mode 100644 index 0000000..6fd3eab --- /dev/null +++ b/i18n/tests/resources/translations/en.json @@ -0,0 +1,5 @@ +{ + "en": { + "foo": "FooBar" + } +} diff --git a/setup.py b/setup.py index c026b4e..abfb3bb 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='python-i18n', - version='0.1.3', + version='0.2.0', description='Translation library for Python', long_description=open('README.md').read(), author='Daniel Perez',