Skip to content

Instantly share code, notes, and snippets.

@ragnarok22
Created September 5, 2017 20:05
Show Gist options
  • Save ragnarok22/978dabe8b708aa0f2f8ecc06e6777f68 to your computer and use it in GitHub Desktop.
Save ragnarok22/978dabe8b708aa0f2f8ecc06e6777f68 to your computer and use it in GitHub Desktop.
settings created by ragnarok22 - https://repl.it/KaLA/68
# esto es un comentario
setting:configuración
language:idioma # esto es otro comentario
import sys
import warnings
class LanguageNotFoundError(FileNotFoundError):
def __init__(self, message):
super(LanguageNotFoundError, self).__init__()
self.message = message
class Language(object):
def __init__(self, lang='en'):
self.lang = lang
def get_lang(self):
return self.lang
def set_lang(self, lang):
self.lang = lang
def read_lang_file(lang):
'''
retorna un str que solo contiene los valores del idioma y la traducción
'''
file = lang + '.lng'
string = ""
try:
with open(file, 'r') as f:
for w in f.readlines():
temp = w.split('#')
string += temp[0]
return string
except FileNotFoundError:
raise LanguageNotFoundError('The language File "{0}" was not found on folder language'.format(file))
except:
print('Error inesperado', sys.exc_info()[0])
def words_dic(str_file):
"""
@params: str_file es un str, es el contenido del fichero
@return: un diccionario con las palabras como llave y su traduccion como valor
"""
result = {}
current = str_file.split('\n')
for i in current:
temp = i.split(':')
result[temp[0]] = temp[1]
return result
def gettext(text, parent):
try:
lang_file = read_lang_file(parent.lang)
except LanguageNotFoundError as e:
warnings.warn("Language not found. {0}".format(e.message), UserWarning)
return text # el idioma especificado no se encontro y se retorna el mismo texto
words = words_dic(lang_file)
result = words.get(text, None)
if result:
return result # si la palabra se encuentra en el fichero se retorna
else:
return text # sino se retorna el mismo texto
import unittest
import language
class TestLanguageClass(unittest.TestCase):
def setUp(self):
class Window(language.Language):
def __init__(self):
super(Window, self).__init__()
self.lang = 'es'
self.btn = language.gettext('setting', self)
self.window = Window()
def test_translate(self):
self.assertEqual(self.window.btn, 'configuración')
class TestLanguageClassInherit(unittest.TestCase):
def setUp(self):
class Window(language.Language):
def __init__(self):
super(Window, self).__init__()
self.btn = language.gettext('setting', self)
self.window = Window()
def test_translate(self):
self.assertEqual(self.window.btn, 'setting')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment