Browse Source

Fixed issue with i18n not loading the correct language

Added unittests for i18n
pull/29/head
chriskiehl 10 years ago
parent
commit
61b03d878d
4 changed files with 32 additions and 24 deletions
  1. 9
      gooey/gooey_decorator.py
  2. 12
      gooey/i18n.py
  3. 33
      gooey/i18n_unittest.py
  4. 2
      gooey/mockapplications/mockapp.py

9
gooey/gooey_decorator.py

@ -9,6 +9,7 @@ from functools import partial
import wx import wx
from gooey.gui.component_factory import ComponentFactory from gooey.gui.component_factory import ComponentFactory
import i18n
import i18n_config import i18n_config
import source_parser import source_parser
from gooey.gui.client_app import ClientApp from gooey.gui.client_app import ClientApp
@ -39,7 +40,9 @@ def Gooey(f=None, advanced=True,
# Must be called before anything else # Must be called before anything else
app = wx.App(False) app = wx.App(False)
print i18n_config.LANG
load_language_pack(language) load_language_pack(language)
print i18n_config.LANG
if config: if config:
parser = get_parser(module_path) parser = get_parser(module_path)
@ -84,10 +87,8 @@ def get_caller_path():
return tmp_sys.argv[0] return tmp_sys.argv[0]
def load_language_pack(language): def load_language_pack(language):
if language is not 'english':
i18n_config.LANG = language
import i18n
i18n.load(language)
# i18n_config.LANG = language

12
gooey/i18n.py

@ -16,6 +16,8 @@ __all__ = ['translate']
_LANG = i18n_config.LANG _LANG = i18n_config.LANG
_DEFAULT_DIR = os.path.join(os.path.dirname(__file__), 'languages') _DEFAULT_DIR = os.path.join(os.path.dirname(__file__), 'languages')
_DICTIONARY = None
def get_path(language): def get_path(language):
''' Returns the full path to the language file ''' ''' Returns the full path to the language file '''
filename = language.lower() + '.json' filename = language.lower() + '.json'
@ -25,17 +27,17 @@ def get_path(language):
return lang_file_path return lang_file_path
def load(filepath):
def load(filename):
''' Open and return the supplied json file ''' ''' Open and return the supplied json file '''
global _DICTIONARY
try: try:
with open(filepath, 'rb') as f:
return json.load(f)
json_file = filename + '.json'
with open(os.path.join(_DEFAULT_DIR, json_file), 'rb') as f:
_DICTIONARY = json.load(f)
except IOError: except IOError:
raise IOError('Language file not found. Make sure that your ', raise IOError('Language file not found. Make sure that your ',
'translation file is in the languages directory, ') 'translation file is in the languages directory, ')
_DICTIONARY = load(get_path(_LANG))
def translate(key): def translate(key):
return _DICTIONARY[key] return _DICTIONARY[key]

33
gooey/i18n_unittest.py

@ -5,26 +5,31 @@ Created on Jan 25, 2014
''' '''
import unittest import unittest
import i18n
import i18n_config import i18n_config
# from i18n import I18N # from i18n import I18N
# #
class Test(unittest.TestCase): class Test(unittest.TestCase):
# def setUp(self):
# pass
#
# def testI18nThrowsIOErrorOnBadPath(self):
# with self.assertRaises(IOError):
# I18N('franch')
def test_asfasdfadsf(self):
import i18n
print i18n.CONFIG_LANG
i18n.CONFIG_LANG = "aaaa"
print i18n.CONFIG_LANG
reload(i18n)
print i18n.CONFIG_LANG
def test_i18n_loads_module_by_name(self):
self.assertTrue(i18n._DICTIONARY is None)
i18n.load('english')
self.assertTrue(i18n._DICTIONARY is not None)
self.assertEqual('Cancel', i18n.translate('cancel'))
i18n.load('french')
self.assertEqual('Annuler', i18n.translate('cancel'))
def test_i18n_throws_exception_on_no_lang_file_found(self):
self.assertRaises(IOError, i18n.load, 'chionenglish')
if __name__ == "__main__": if __name__ == "__main__":
pass pass

2
gooey/mockapplications/mockapp.py

@ -12,7 +12,7 @@ from argparse import ArgumentParser
from gooey import Gooey from gooey import Gooey
@Gooey
@Gooey(language='french')
def main(): def main():
my_cool_parser = ArgumentParser(description="Mock application to test Gooey's functionality") my_cool_parser = ArgumentParser(description="Mock application to test Gooey's functionality")
my_cool_parser.add_argument("filename", help="Name of the file you want to read") # positional my_cool_parser.add_argument("filename", help="Name of the file you want to read") # positional

Loading…
Cancel
Save