app/django/utils/translation/trans_real.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     6 import sys
     6 import sys
     7 import gettext as gettext_module
     7 import gettext as gettext_module
     8 from cStringIO import StringIO
     8 from cStringIO import StringIO
     9 
     9 
    10 from django.utils.safestring import mark_safe, SafeData
    10 from django.utils.safestring import mark_safe, SafeData
    11 
    11 from django.utils.thread_support import currentThread
    12 try:
       
    13     import threading
       
    14     hasThreads = True
       
    15 except ImportError:
       
    16     hasThreads = False
       
    17 
       
    18 if hasThreads:
       
    19     currentThread = threading.currentThread
       
    20 else:
       
    21     def currentThread():
       
    22         return 'no threading'
       
    23 
    12 
    24 # Translations are cached in a dictionary for every language+app tuple.
    13 # Translations are cached in a dictionary for every language+app tuple.
    25 # The active translations are stored by threadid to make them thread local.
    14 # The active translations are stored by threadid to make them thread local.
    26 _translations = {}
    15 _translations = {}
    27 _active = {}
    16 _active = {}
   159             except IOError, e:
   148             except IOError, e:
   160                 return None
   149                 return None
   161 
   150 
   162         res = _translation(globalpath)
   151         res = _translation(globalpath)
   163 
   152 
       
   153         # We want to ensure that, for example,  "en-gb" and "en-us" don't share
       
   154         # the same translation object (thus, merging en-us with a local update
       
   155         # doesn't affect en-gb), even though they will both use the core "en"
       
   156         # translation. So we have to subvert Python's internal gettext caching.
       
   157         base_lang = lambda x: x.split('-', 1)[0]
       
   158         if base_lang(lang) in [base_lang(trans) for trans in _translations]:
       
   159             res._info = res._info.copy()
       
   160             res._catalog = res._catalog.copy()
       
   161 
   164         def _merge(path):
   162         def _merge(path):
   165             t = _translation(path)
   163             t = _translation(path)
   166             if t is not None:
   164             if t is not None:
   167                 if res is None:
   165                 if res is None:
   168                     return t
   166                     return t
   244     Returns selected language's BiDi layout.
   242     Returns selected language's BiDi layout.
   245     False = left-to-right layout
   243     False = left-to-right layout
   246     True = right-to-left layout
   244     True = right-to-left layout
   247     """
   245     """
   248     from django.conf import settings
   246     from django.conf import settings
   249     return get_language() in settings.LANGUAGES_BIDI
   247     
       
   248     base_lang = get_language().split('-')[0]
       
   249     return base_lang in settings.LANGUAGES_BIDI
   250 
   250 
   251 def catalog():
   251 def catalog():
   252     """
   252     """
   253     Returns the current active catalog for further processing.
   253     Returns the current active catalog for further processing.
   254     This can be used if you need to modify the catalog or want to access the
   254     This can be used if you need to modify the catalog or want to access the
   381             # need to check again.
   381             # need to check again.
   382             return _accepted[normalized]
   382             return _accepted[normalized]
   383 
   383 
   384         for lang, dirname in ((accept_lang, normalized),
   384         for lang, dirname in ((accept_lang, normalized),
   385                 (accept_lang.split('-')[0], normalized.split('_')[0])):
   385                 (accept_lang.split('-')[0], normalized.split('_')[0])):
   386             if lang not in supported:
   386             if lang.lower() not in supported:
   387                 continue
   387                 continue
   388             langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES',
   388             langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES',
   389                     'django.mo')
   389                     'django.mo')
   390             if os.path.exists(langfile):
   390             if os.path.exists(langfile):
   391                 _accepted[normalized] = lang
   391                 _accepted[normalized] = lang
   392             return lang
   392                 return lang
   393 
   393 
   394     return settings.LANGUAGE_CODE
   394     return settings.LANGUAGE_CODE
   395 
   395 
   396 def get_date_formats():
   396 def get_date_formats():
   397     """
   397     """