def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set in case the current platform doesn't use them by default (OS X, Window) """ if os.name == "nt": from quodlibet.util.winapi import GetUserDefaultUILanguage, \ GetSystemDefaultUILanguage langs = filter( None, map(locale.windows_locale.get, [GetUserDefaultUILanguage(), GetSystemDefaultUILanguage()])) if langs: os.environ.setdefault('LANG', langs[0]) os.environ.setdefault('LANGUAGE', ":".join(langs)) elif sys.platform == "darwin": from AppKit import NSLocale locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault('LANG', lang) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault('LANGUAGE', ":".join(languages)) else: return
def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set in case the current platform doesn't use them by default (OS X, Window) """ if os.name == "nt": from quodlibet.util.winapi import GetUserDefaultUILanguage, \ GetSystemDefaultUILanguage langs = filter(None, map(locale.windows_locale.get, [GetUserDefaultUILanguage(), GetSystemDefaultUILanguage()])) if langs: os.environ.setdefault('LANG', langs[0]) os.environ.setdefault('LANGUAGE', ":".join(langs)) elif sys.platform == "darwin": from AppKit import NSLocale locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault('LANG', lang) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault('LANGUAGE', ":".join(languages)) else: return
def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set. Covers the cases where the current platform doesn't use them by default (OS X, Windows). In these systems, it will pick up defaults by other OS-specific means. """ if os.name == "nt": # This MyPaint code turns out to be quite portable. # On Win7 Professional, or Win7 Enterprise/Ultimate when there # are no UI language packs installed, it will pick up the user's # Region and Language → Formats setting when the code below # doesn't. langs = [] deflocale = locale.getdefaultlocale() if deflocale: langs.append(deflocale[0]) # Let the user's chosen UI language contribute too. # I think only the chosen UI language affects this. # Not sure what the priority order should be between the two. import ctypes k32 = ctypes.windll.kernel32 for lang_k32 in [ k32.GetUserDefaultUILanguage(), k32.GetSystemDefaultUILanguage() ]: lang = locale.windows_locale.get(lang_k32) if lang not in langs: langs.append(lang) if langs: os.environ.setdefault('LANG', langs[0]) os.environ.setdefault('LANGUAGE', ":".join(langs)) logger.info("Windows: LANG=%r", os.environ.get("LANG")) logger.info("Windows: LANGUAGE=%r", os.environ.get("LANGUAGE")) elif sys.platform == "darwin": from AppKit import NSLocale locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault('LANG', lang) logger.info("OSX: LANG=%r", os.environ.get("LANG")) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault('LANGUAGE', ":".join(languages)) logger.info("OSX: LANGUAGE=%r", os.environ.get("LANGUAGE")) else: logger.info("POSIX: LANG=%r", os.environ.get("LANG")) logger.info("POSIX: LANGUAGE=%r", os.environ.get("LANGUAGE"))
def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set. Covers the cases where the current platform doesn't use them by default (OS X, Windows). In these systems, it will pick up defaults by other OS-specific means. """ if os.name == "nt": # This MyPaint code turns out to be quite portable. # On Win7 Professional, or Win7 Enterprise/Ultimate when there # are no UI language packs installed, it will pick up the user's # Region and Language → Formats setting when the code below # doesn't. langs = [] deflocale = locale.getdefaultlocale() if deflocale: langs.append(deflocale[0]) # Let the user's chosen UI language contribute too. # I think only the chosen UI language affects this. # Not sure what the priority order should be between the two. import ctypes k32 = ctypes.windll.kernel32 for lang_k32 in [k32.GetUserDefaultUILanguage(), k32.GetSystemDefaultUILanguage()]: lang = locale.windows_locale.get(lang_k32) if lang not in langs: langs.append(lang) if langs: os.environ.setdefault("LANG", langs[0]) os.environ.setdefault("LANGUAGE", ":".join(langs)) logger.info("Windows: LANG=%r", os.environ.get("LANG")) logger.info("Windows: LANGUAGE=%r", os.environ.get("LANGUAGE")) elif sys.platform == "darwin": from AppKit import NSLocale locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault("LANG", lang) logger.info("OSX: LANG=%r", os.environ.get("LANG")) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault("LANGUAGE", ":".join(languages)) logger.info("OSX: LANGUAGE=%r", os.environ.get("LANGUAGE")) else: logger.info("POSIX: LANG=%r", os.environ.get("LANG")) logger.info("POSIX: LANGUAGE=%r", os.environ.get("LANGUAGE"))
def language_suffixes(): for lang in NSLocale.preferredLanguages(): while True: yield "_" + lang if lang != "en" else "" if "-" in lang: lang = lang[: lang.rfind("-")] else: break yield ""
def language_suffixes(): for lang in NSLocale.preferredLanguages(): while True: yield "_" + lang if lang != 'en' else '' if '-' in lang: lang = lang[:lang.rfind('-')] else: break yield ''
def setLastUpdateCheck(self): date = self.app.last_update_check if date: formatter = NSDateFormatter.alloc().init() # use current user locale for 'last update' timestamp formatter.setLocale_(NSLocale.currentLocale()) # use user-definable 'medium style' formats formatter.setTimeStyle_(NSDateFormatterMediumStyle) formatter.setDateStyle_(NSDateFormatterMediumStyle) date = formatter.stringFromDate_(date) self.lastUpdateCheck.setStringValue_(date)
def get_default_lang(): if os.name == "nt": import ctypes windll = ctypes.windll.kernel32 return locale.windows_locale[windll.GetUserDefaultUILanguage()] if sys.platform == "darwin": from AppKit import NSLocale # FIXME: This returns a two letter language code (en, de, fr) # We need a way to get en_US, de_DE etc. return NSLocale.currentLocale().languageCode() return locale.getdefaultlocale()[0] or 'en'
def get_language(): env_vars = ['LANGUAGE'] if sys.platform.startswith('linux'): env_vars.extend(('LC_ALL', 'LC_MESSAGES', 'LANG')) for var in env_vars: lang = os.environ.get(var) if lang is not None: return lang if sys.platform.startswith('linux'): lang, enc = locale.getdefaultlocale() elif sys.platform.startswith('darwin'): from AppKit import NSLocale lang_list = NSLocale.preferredLanguages() lang = lang_list[0] if lang_list else None elif sys.platform.startswith('win'): from ctypes import windll lang = locale.windows_locale[windll.kernel32.GetUserDefaultUILanguage()] if lang is None: lang = 'en' return lang
def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set. Covers the cases where the current platform doesn't use them by default (OS X, Windows). In these systems, it will pick up defaults by other OS-specific means. """ if os.name == "nt": logger.debug("Windows: figuring out fallbacks for gettext's " "LANG and LANGUAGE vars. Setting those will override " "this logic.") langs = [] # Let the user's chosen UI language be the default, unless # overridden by a POSIX-style LANG or LANGUAGE var. try: import ctypes k32 = ctypes.windll.kernel32 for lang_k32 in [ k32.GetUserDefaultUILanguage(), k32.GetSystemDefaultUILanguage() ]: lang = locale.windows_locale.get(lang_k32) if lang not in langs: langs.append(lang) logger.debug("Windows: found UI language %r", lang) except: logger.exception("Windows: couldn't get UI language via ctypes") # On Win7 Professional, or Win7 Enterprise/Ultimate when there # are no UI language packs installed, this will pick up the # user's Region and Language → Formats setting when the code # above doesn't set anything up. Note that some users have to # fudge their "Formats" setting in order to get 3rd party # software to work without crashing. deflocale = getdefaultlocale() if deflocale: lang = deflocale[0] if lang not in langs: langs.append(lang) logger.debug( "Windows: found fallback language %r, " "inferred from Region & Languages -> Formats.", lang, ) # The POSIX-style LANG and LANGUAGE environment variables still # override all of the above. Since MSYS2 shells are going to use # them, that's reasonable. if langs: os.environ.setdefault('LANG', langs[0]) os.environ.setdefault('LANGUAGE', ":".join(langs)) logger.info("Windows: LANG=%r", os.environ.get("LANG")) logger.info("Windows: LANGUAGE=%r", os.environ.get("LANGUAGE")) elif sys.platform == "darwin": try: from AppKit import NSLocale except ImportError: logger.exception("OSX: failed to import AppKit.NSLocale") logger.warning("OSX: falling back to POSIX mechanisms.") else: logger.info("OSX: imported AppKit.NSLocale OK. " "Will use for LANG, and for LANGUAGE order.") locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault('LANG', lang) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault('LANGUAGE', ":".join(languages)) logger.info("OSX: LANG=%r", os.environ.get("LANG")) logger.info("OSX: LANGUAGE=%r", os.environ.get("LANGUAGE")) else: logger.info("POSIX: LANG=%r", os.environ.get("LANG")) logger.info("POSIX: LANGUAGE=%r", os.environ.get("LANGUAGE"))
def format(self, fmt, locale=None, timezone=None): self.formatter.setDateFormat_(fmt) if locale: self.formatter.setLocale_( NSLocale.alloc().initWithLocaleIdentifier_(locale)) return self.formatter.stringFromDate_(self.nsdate).encode('utf-8')
def format(self, fmt, locale = None, timezone = None): self.formatter.setDateFormat_(fmt) if locale: self.formatter.setLocale_(NSLocale.alloc().initWithLocaleIdentifier_(locale)) return self.formatter.stringFromDate_(self.nsdate).encode('utf-8')
def set_i18n_envvars(): """Set the LANG/LANGUAGE environment variables if not set. Covers the cases where the current platform doesn't use them by default (OS X, Windows). In these systems, it will pick up defaults by other OS-specific means. """ if os.name == "nt": logger.debug( "Windows: figuring out fallbacks for gettext's " "LANG and LANGUAGE vars. Setting those will override " "this logic." ) langs = [] # Let the user's chosen UI language be the default, unless # overridden by a POSIX-style LANG or LANGUAGE var. try: import ctypes k32 = ctypes.windll.kernel32 for lang_k32 in [k32.GetUserDefaultUILanguage(), k32.GetSystemDefaultUILanguage()]: lang = locale.windows_locale.get(lang_k32) if lang not in langs: langs.append(lang) logger.debug("Windows: found UI language %r", lang) except: logger.exception("Windows: couldn't get UI language via ctypes") # On Win7 Professional, or Win7 Enterprise/Ultimate when there # are no UI language packs installed, this will pick up the # user's Region and Language → Formats setting when the code # above doesn't set anything up. Note that some users have to # fudge their "Formats" setting in order to get 3rd party # software to work without crashing. deflocale = locale.getdefaultlocale() if deflocale: lang = deflocale[0] if lang not in langs: langs.append(lang) logger.debug( "Windows: found fallback language %r, " "inferred from Region & Languages -> Formats.", lang, ) # The POSIX-style LANG and LANGUAGE environment variables still # override all of the above. Since MSYS2 shells are going to use # them, that's reasonable. if langs: os.environ.setdefault('LANG', langs[0]) os.environ.setdefault('LANGUAGE', ":".join(langs)) logger.info("Windows: LANG=%r", os.environ.get("LANG")) logger.info("Windows: LANGUAGE=%r", os.environ.get("LANGUAGE")) elif sys.platform == "darwin": try: from AppKit import NSLocale except ImportError: logger.exception("OSX: failed to import AppKit.NSLocale") logger.warning("OSX: falling back to POSIX mechanisms.") else: logger.info( "OSX: imported AppKit.NSLocale OK. " "Will use for LANG, and for LANGUAGE order." ) locale_id = NSLocale.currentLocale().localeIdentifier() lang = osx_locale_id_to_lang(locale_id) os.environ.setdefault('LANG', lang) preferred_langs = NSLocale.preferredLanguages() if preferred_langs: languages = map(bcp47_to_language, preferred_langs) os.environ.setdefault('LANGUAGE', ":".join(languages)) logger.info("OSX: LANG=%r", os.environ.get("LANG")) logger.info("OSX: LANGUAGE=%r", os.environ.get("LANGUAGE")) else: logger.info("POSIX: LANG=%r", os.environ.get("LANG")) logger.info("POSIX: LANGUAGE=%r", os.environ.get("LANGUAGE"))