chiark / gitweb /
add Chinese language
[cura.git] / Cura / util / resources.py
1 #coding:utf8
2 """
3 Helper module to get easy access to the path where resources are stored.
4 This is because the resource location is depended on the packaging method and OS
5 """
6 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
7
8 import os
9 import sys
10 import glob
11
12 import gettext
13
14 if sys.platform.startswith('darwin'):
15         try:
16                 #Foundation import can crash on some MacOS installs
17                 from Foundation import *
18         except:
19                 pass
20
21 if sys.platform.startswith('darwin'):
22         if hasattr(sys, 'frozen'):
23                 try:
24                         resourceBasePath = NSBundle.mainBundle().resourcePath()
25                 except:
26                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
27         else:
28                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
29 else:
30         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
31
32 def getPathForResource(dir, subdir, resource_name):
33         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
34         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
35         assert os.path.isfile(path), "{p} is not a file.".format(p=path)
36         return path
37
38 def getPathForImage(name):
39         return getPathForResource(resourceBasePath, 'images', name)
40
41 def getPathForMesh(name):
42         return getPathForResource(resourceBasePath, 'meshes', name)
43
44 def getPathForFirmware(name):
45         return getPathForResource(resourceBasePath, 'firmware', name)
46
47 def getDefaultMachineProfiles():
48         path = os.path.normpath(os.path.join(resourceBasePath, 'machine_profiles', '*.ini'))
49         return glob.glob(path)
50
51 def setupLocalization(selectedLanguage = None):
52         #Default to english
53         languages = ['zh']
54
55         if selectedLanguage is not None:
56                 for item in getLanguageOptions():
57                         if item[1] == selectedLanguage and item[0] is not None:
58                                 languages = [item[0]]
59
60         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
61         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
62         translation.install(unicode=True)
63
64 def getLanguageOptions():
65         return [
66                 ['en', 'English'],
67                 ['de', 'Deutsch'],
68                 ['fr', 'French'],
69                 ['zh', 'Chinese'],
70                 # ['nl', 'Nederlands'],
71                 # ['es', 'Spanish'],
72                 # ['po', 'Polish']
73         ]