ya2 · news · projects · code · about

instructions
[pmachines.git] / lib / engine / lang.py
1 from logging import info
2 from os.path import join, exists, dirname
3 from gettext import translation
4 from pathlib import Path
5 from lib.gameobject import GameObject
6 import sys
7
8
9 def is_runtime(): return not exists('main.py')
10
11
12 def is_appimage():
13 par_path = str(Path(__file__).parent.absolute())
14 is_appimage = par_path.startswith('/tmp/.mount_Pmachines')
15 return is_appimage and par_path.endswith('/usr/bin')
16
17
18 def curr_path():
19 if not is_runtime(): return ''
20 if sys.platform == 'darwin':
21 return dirname(__file__) + '/../Resources/'
22 # return dirname(__file__)
23 par_path = str(Path(__file__).parent.absolute())
24 if is_appimage():
25 return str(Path(par_path).absolute())
26 is_snap = par_path.startswith('/snap/')
27 is_snap = is_snap and par_path.endswith('/x1')
28 if is_snap:
29 return str(Path(par_path).absolute())
30 #return getcwd()
31 curr_path = dirname(__file__)
32 info('current path: %s' % curr_path)
33 return curr_path + '/'
34
35
36 class LangMgr(GameObject):
37
38 def __init__(self, lang, domain, dpath):
39 GameObject.__init__(self)
40 self.lang = lang
41 self.domain = domain
42 self.dpath = join(curr_path(), dpath)
43 info('language: %s, %s' % (self.domain, self.dpath))
44 self.set_lang(lang)
45
46 @property
47 def lang_codes(self):
48 return [lang[1] for lang in self.eng.cfg.lang_cfg.languages]
49
50 def set_lang(self, lang):
51 self.lang = lang
52 args = lang, self.domain, self.dpath
53 info('setting language %s, %s, %s' % args)
54 tra = translation(self.domain, self.dpath, [lang], fallback=True)
55 tra.install()