chiark / gitweb /
Auto detect language based on locale
authorYouness Alaoui <kakaroto@kakaroto.homelinux.net>
Wed, 28 Jan 2015 00:25:04 +0000 (19:25 -0500)
committerYouness Alaoui <kakaroto@kakaroto.homelinux.net>
Wed, 28 Jan 2015 00:28:55 +0000 (19:28 -0500)
This wasn't tested on Windows, but the code is in a try/except and
will fallback to using the default locale if it fails.
Fixes issue #29

Cura/util/profile.py
Cura/util/resources.py

index 93572c274502a80004e6b4c892abb27e21f1f1e4..afdc764c84cc172e5b3a1231ecf4dd7187b01a8c 100644 (file)
@@ -486,7 +486,7 @@ setting('check_for_updates', 'False', bool, 'preference', 'hidden').setLabel(_("
 setting('submit_slice_information', 'False', bool, 'preference', 'hidden').setLabel(_("Send usage statistics"), _("Submit anonymous usage information to improve future versions of Cura"))
 setting('youmagine_token', '', str, 'preference', 'hidden')
 setting('filament_physical_density', '1240', float, 'preference', 'hidden').setRange(500.0, 3000.0).setLabel(_("Density (kg/m3)"), _("Weight of the filament per m3. Around 1240 for PLA. And around 1040 for ABS. This value is used to estimate the weight if the filament used for the print."))
-setting('language', 'English', str, 'preference', 'hidden').setLabel(_('Language'), _('Change the language in which Cura runs. Switching language requires a restart of Cura'))
+setting('language', 'AUTO', str, 'preference', 'hidden').setLabel(_('Language'), _('Change the language in which Cura runs. Switching language requires a restart of Cura'))
 setting('active_machine', '0', int, 'preference', 'hidden')
 
 setting('model_colour', '#C9E240', str, 'preference', 'hidden').setLabel(_('Model colour'), _('Display color for first extruder'))
index d92eb2aee8572468e2b21744bf979f658ff2b66f..e5e429091e728c2f99b216e709ba8adf145af359 100644 (file)
@@ -8,6 +8,8 @@ __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AG
 import os
 import sys
 import glob
+import platform
+import locale
 
 import gettext
 
@@ -53,7 +55,13 @@ def setupLocalization(selectedLanguage = None):
        #Default to english
        languages = ['en']
 
-       if selectedLanguage is not None:
+       if selectedLanguage is None or selectedLanguage == 'AUTO':
+               defaultLocale = getDefaultLocale()
+               if defaultLocale is not None:
+                       for item in getLanguageOptions():
+                               if item[0] is not None and defaultLocale.startswith(item[0]):
+                                       languages = [item[0]]
+       else:
                for item in getLanguageOptions():
                        if item[1] == selectedLanguage and item[0] is not None:
                                languages = [item[0]]
@@ -75,3 +83,25 @@ def getLanguageOptions():
                # ['es', 'Spanish'],
                # ['po', 'Polish']
        ]
+
+def getDefaultLocale():
+       defaultLocale = None
+
+       # On Windows, we look for the actual UI language, as someone could have
+       # an english windows but use a non-english locale.
+       if platform.system() == "Windows":
+               try:
+                       import ctypes
+
+                       windll = ctypes.windll.kernel32
+                       defaultLocale = locale.windows_locale[windll.GetUserDefaultUILanguage()]
+               except:
+                       pass
+
+       if defaultLocale is None:
+               try:
+                       defaultLocale = locale.getdefaultlocale()[0]
+               except:
+                       pass
+
+       return defaultLocale