chiark / gitweb /
8dbe9bd1e719dab4064f598a6b50ce4492bdf547
[cura.git] / Cura / util / resources.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import os
5 import sys
6
7 #Cura/util classes should not depend on wx...
8 import wx
9 import gettext
10
11 if sys.platform.startswith('darwin'):
12         try:
13                 #Foundation import can crash on some MacOS installs
14                 from Foundation import *
15         except:
16                 pass
17
18 if sys.platform.startswith('darwin'):
19         if hasattr(sys, 'frozen'):
20                 try:
21                         resourceBasePath = NSBundle.mainBundle().resourcePath()
22                 except:
23                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
24         else:
25                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
26 else:
27         if hasattr(sys, 'frozen'):
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 setupLocalization(selectedLanguage = None):
48         try:
49                 if sys.platform.startswith('darwin'):
50                         languages = NSLocale.preferredLanguages()
51                 else:
52                         #Using wx.Locale before you created wx.App seems to cause an nasty exception. So default to 'en' at the moment.
53                         languages = [wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName()]
54         except Exception as e:
55                 languages = ['en']
56
57         if selectedLanguage is not None:
58                 for item in getLanguageOptions():
59                         if item[1] == selectedLanguage:
60                                 languages = [item[0]] + languages
61
62         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
63         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
64         translation.install(unicode=True)
65
66 def getLanguageOptions():
67         return [
68                 ['en', 'English'],
69                 ['de', 'Deutsch'],
70                 ['nl', 'Nederlands'],
71         ]