chiark / gitweb /
Merge pull request #557 from GreatFruitOmsk/i18n
[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 import wx
8 import gettext
9
10 if sys.platform.startswith('darwin'):
11         try:
12                 #Foundation import can crash on some MacOS installs
13                 from Foundation import *
14         except:
15                 pass
16
17 if sys.platform.startswith('darwin'):
18         if hasattr(sys, 'frozen'):
19                 try:
20                         resourceBasePath = NSBundle.mainBundle().resourcePath()
21                 except:
22                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
23         else:
24                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
25 else:
26         if hasattr(sys, 'frozen'):
27                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
28         else:
29                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
30
31 def getPathForResource(dir, subdir, resource_name):
32         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
33         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
34         assert os.path.isfile(path), "{p} is not a file.".format(p=path)
35         return path
36
37 def getPathForImage(name):
38         return getPathForResource(resourceBasePath, 'images', name)
39
40 def getPathForMesh(name):
41         return getPathForResource(resourceBasePath, 'meshes', name)
42
43 def getPathForFirmware(name):
44         return getPathForResource(resourceBasePath, 'firmware', name)
45
46 def setupLocalization():
47         try:
48                 if sys.platform.startswith('darwin'):
49                         languages = NSLocale.preferredLanguages()
50                 else:
51                         languages = [wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName()]
52         except Exception as e:
53                 languages = ['en']
54
55         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
56         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
57         translation.install(unicode=True)