[r21640]GrampsLocale: Reduce circular imports

Lets one import grampslocale directly in a test script:
>>> from gramps.gen.utils.grampslocale import GrampsLocale
>>> import os
>>> gl = GrampsLocale(lang='fr_FR.UTF-8', localedir=os.path.join('build', 'mo'))
>>> tr = gl.get_translation().gettext
>>> tr("List of known family trees in your database path\n")
u'Liste des arbres familiaux connus dans votre chemin de base de donn\xe9es\n'

svn: r21647
This commit is contained in:
John Ralls 2013-03-14 23:07:40 +00:00
parent 0fda971b4c
commit 2629bf3dee
3 changed files with 17 additions and 35 deletions

View File

@ -80,17 +80,6 @@ APP_GRAMPS_PKG = "application/x-gramps-package"
APP_GENEWEB = "application/x-geneweb" APP_GENEWEB = "application/x-geneweb"
APP_VCARD = ["text/x-vcard", "text/x-vcalendar"] APP_VCARD = ["text/x-vcard", "text/x-vcalendar"]
#-------------------------------------------------------------------------
#
# Platforms
# Never test on LINUX, handle Linux in the else statement as default
#
#-------------------------------------------------------------------------
LINUX = ["Linux", "linux", "linux2"]
MACOS = ["Darwin", "darwin"]
WINDOWS = ["Windows", "win32"]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Determine the home directory. According to Wikipedia, most UNIX like # Determine the home directory. According to Wikipedia, most UNIX like
@ -176,7 +165,7 @@ WEBSTUFF_IMAGE_DIR = os.path.join(WEBSTUFF_DIR, "images")
USE_TIPS = False USE_TIPS = False
if os.sys.platform in WINDOWS: if sys.platform == 'win32':
USE_THUMBNAILER = False USE_THUMBNAILER = False
else: else:
USE_THUMBNAILER = True USE_THUMBNAILER = True
@ -188,7 +177,6 @@ else:
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.utils.resourcepath import ResourcePath from gramps.gen.utils.resourcepath import ResourcePath
_resources = ResourcePath() _resources = ResourcePath()
LOCALE_DIR = _resources.locale_dir
DATA_DIR = _resources.data_dir DATA_DIR = _resources.data_dir
IMAGE_DIR = _resources.image_dir IMAGE_DIR = _resources.image_dir
@ -206,7 +194,7 @@ LICENSE_FILE = os.path.join(_resources.doc_dir, 'COPYING')
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.utils.grampslocale import GrampsLocale from gramps.gen.utils.grampslocale import GrampsLocale
GRAMPS_LOCALE = GrampsLocale() GRAMPS_LOCALE = GrampsLocale(localedir=_resources.locale_dir)
_ = GRAMPS_LOCALE.get_translation().sgettext _ = GRAMPS_LOCALE.get_translation().sgettext
#------------------------------------------------------------------------- #-------------------------------------------------------------------------

View File

@ -37,10 +37,13 @@ import sys
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Gramps modules # Platforms
# Never test on LINUX, handle Linux in the else statement as default
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from .const import WINDOWS, MACOS, LINUX LINUX = ["Linux", "linux", "linux2"]
MACOS = ["Darwin", "darwin"]
WINDOWS = ["Windows", "win32"]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #

View File

@ -50,7 +50,6 @@ except ImportError:
# gramps modules # gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..const import LOCALE_DIR
from ..constfunc import mac, win, UNITYPE from ..constfunc import mac, win, UNITYPE
#------------------------------------------------------------------------ #------------------------------------------------------------------------
@ -114,7 +113,7 @@ class GrampsLocale(object):
except TypeError: except TypeError:
LOG.warning('Unable to determine your Locale, using English') LOG.warning('Unable to determine your Locale, using English')
lang = 'C.UTF-8' lang = 'C.UTF-8'
self.lang = lang self.lang = lang
if not language or len(language) == 0: if not language or len(language) == 0:
if "LANGUAGE" in os.environ: if "LANGUAGE" in os.environ:
@ -159,7 +158,7 @@ class GrampsLocale(object):
LOG.warning("Localization library libintl not on %PATH%, localization will be incomplete") LOG.warning("Localization library libintl not on %PATH%, localization will be incomplete")
def __init_first_instance(self, localedir=None, lang=None, def __init_first_instance(self, localedir, lang=None,
domain=None, language=None): domain=None, language=None):
#First, globally set the locale to what's in the environment: #First, globally set the locale to what's in the environment:
@ -171,21 +170,13 @@ class GrampsLocale(object):
if localedir and os.path.exists(localedir): if localedir and os.path.exists(localedir):
self.localedir = localedir self.localedir = localedir
else: else:
if ("GRAMPSI18N" in os.environ if not lang:
and os.path.exists(os.environ["GRAMPSI18N"])): lang = os.environ.get('LANG', 'en')
self.localedir = os.environ["GRAMPSI18N"] if lang and lang[:2] == 'en':
elif os.path.exists(LOCALE_DIR): pass # No need to display warning, we're in English
self.localedir = LOCALE_DIR
elif os.path.exists(os.path.join(sys.prefix, "share", "locale")):
self.localedir = os.path.join(sys.prefix, "share", "locale")
else: else:
if not lang: LOG.warning('Locale dir does not exist at %s', localedir)
lang = os.environ.get('LANG', 'en') LOG.warning('Running python setup.py install --prefix=YourPrefixDir might fix the problem')
if lang and lang[:2] == 'en':
pass # No need to display warning, we're in English
else:
LOG.warning('Locale dir does not exist at %s', LOCALE_DIR)
LOG.warning('Running python setup.py install --prefix=YourPrefixDir might fix the problem')
if not self.localedir: if not self.localedir:
#No localization files, no point in continuing #No localization files, no point in continuing
@ -248,7 +239,7 @@ class GrampsLocale(object):
self.initialized = True self.initialized = True
def __init__(self, lang=None, localedir=None, domain=None, languages=None): def __init__(self, localedir=None, lang=None, domain=None, languages=None):
""" """
Init a GrampsLocale. Run __init_first_instance() to set up the Init a GrampsLocale. Run __init_first_instance() to set up the
environement if this is the first run. Return __first_instance environement if this is the first run. Return __first_instance
@ -256,7 +247,7 @@ class GrampsLocale(object):
""" """
if self == self._GrampsLocale__first_instance: if self == self._GrampsLocale__first_instance:
if not self.initialized: if not self.initialized:
self._GrampsLocale__init_first_instance(lang, localedir, self._GrampsLocale__init_first_instance(localedir, lang,
domain, languages) domain, languages)
else: else:
return return