chiark / gitweb /
Disable all languages except english for an official release.
[cura.git] / Cura / util / resources.py
index 0db52c119c5e14794c870fd11d7bf74cfcac29a0..6eb90cc6f86d35a761d0e5bc99f65fd1bb57b89a 100644 (file)
@@ -1,36 +1,80 @@
-# coding=utf-8
 from __future__ import absolute_import
+__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
+
 import os
 import sys
+import glob
 
-__all__ = ['getPathForResource', 'getPathForImage', 'getPathForMesh']
+#Cura/util classes should not depend on wx...
+import wx
+import gettext
 
+if sys.platform.startswith('darwin'):
+       try:
+               #Foundation import can crash on some MacOS installs
+               from Foundation import *
+       except:
+               pass
 
 if sys.platform.startswith('darwin'):
        if hasattr(sys, 'frozen'):
-               from Foundation import *
-               imagesPath = os.path.join(NSBundle.mainBundle().resourcePath(), 'images')
-               meshesPath = os.path.join(NSBundle.mainBundle().resourcePath(), 'images')
+               try:
+                       resourceBasePath = NSBundle.mainBundle().resourcePath()
+               except:
+                       resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
        else:
-               imagesPath = os.path.join(os.path.dirname(__file__), "../images")
-               meshesPath = os.path.join(os.path.dirname(__file__), "../images")
+               resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
 else:
        if hasattr(sys, 'frozen'):
-               imagesPath = os.path.join(os.path.dirname(__file__), "../../images")
-               meshesPath = os.path.join(os.path.dirname(__file__), "../../images")
+               resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
        else:
-               imagesPath = os.path.join(os.path.dirname(__file__), "../images")
-               meshesPath = os.path.join(os.path.dirname(__file__), "../images")
-
+               resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
 
-def getPathForResource(dir, resource_name):
+def getPathForResource(dir, subdir, resource_name):
        assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
-       path = os.path.normpath(os.path.join(dir, resource_name))
+       path = os.path.normpath(os.path.join(dir, subdir, resource_name))
        assert os.path.isfile(path), "{p} is not a file.".format(p=path)
        return path
 
 def getPathForImage(name):
-       return getPathForResource(imagesPath, name)
+       return getPathForResource(resourceBasePath, 'images', name)
 
 def getPathForMesh(name):
-       return getPathForResource(meshesPath, name)
+       return getPathForResource(resourceBasePath, 'meshes', name)
+
+def getPathForFirmware(name):
+       return getPathForResource(resourceBasePath, 'firmware', name)
+
+def getDefaultMachineProfiles():
+       path = os.path.normpath(os.path.join(resourceBasePath, 'machine_profiles', '*.ini'))
+       return glob.glob(path)
+
+def setupLocalization(selectedLanguage = None):
+       try:
+               if sys.platform.startswith('darwin'):
+                       languages = NSLocale.preferredLanguages()
+               else:
+                       #Using wx.Locale before you created wx.App seems to cause an nasty exception. So default to 'en' at the moment.
+                       languages = [wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName()]
+       except Exception as e:
+               languages = ['en']
+
+       if selectedLanguage is not None:
+               for item in getLanguageOptions():
+                       if item[1] == selectedLanguage and item[0] is not None:
+                               languages = [item[0]]
+
+       locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
+       translation = gettext.translation('Cura', locale_path, languages, fallback=True)
+       translation.install(unicode=True)
+
+def getLanguageOptions():
+       return [
+               # [None, 'System default'],
+               ['en', 'English'],
+               # ['de', 'Deutsch'],
+               # ['fr', 'French'],
+               # ['nl', 'Nederlands'],
+               # ['sp', 'Spanish'],
+               # ['po', 'Polish']
+       ]