Skip to content

Commit

Permalink
Add proper support for files without namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Dec 9, 2013
1 parent 8ad35c1 commit d11eb2c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
1 change: 0 additions & 1 deletion i18n/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
'error_on_missing_plural': False,
'encoding': 'utf-8',
'namespace_delimiter': '.',
'dir_as_namespace': True,
'plural_few': 5
}

Expand Down
27 changes: 19 additions & 8 deletions i18n/resource_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ 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}')]
except ValueError:
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):
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions i18n/tests/loader_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 5 additions & 0 deletions i18n/tests/resources/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"en": {
"foo": "FooBar"
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit d11eb2c

Please sign in to comment.