camelot.camelot.model.i18n

Package camelot :: Package camelot :: Package model :: Module i18n |
[hide private]
[frames] | no frames]

# Source Code for Module camelot.camelot.model.i18n

     1  #  =============================================================================

     2  #

     3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.

     4  #  www.conceptive.be / project-camelot@conceptive.be

     5  #

     6  #  This file is part of the Camelot Library.

     7  #

     8  #  This file may be used under the terms of the GNU General Public

     9  #  License version 2.0 as published by the Free Software Foundation

    10  #  and appearing in the file LICENSE.GPL included in the packaging of

    11  #  this file.  Please review the following information to ensure GNU

    12  #  General Public Licensing requirements will be met:

    13  #  http://www.trolltech.com/products/qt/opensource.html

    14  #

    15  #  If you are unsure which license is appropriate for your use, please

    16  #  review the following information:

    17  #  http://www.trolltech.com/products/qt/licensing.html or contact

    18  #  project-camelot@conceptive.be.

    19  #

    20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

    21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

    22  #

    23  #  For use of this library in commercial applications, please contact

    24  #  project-camelot@conceptive.be

    25  #

    26  #  =============================================================================

    27  """Set of classes to enable internationalization of the user interface"""
    28

    29  from camelot.model import *
    30  __metadata__ = metadata
    31

    32  from camelot.view.elixir_admin import EntityAdmin
    33

    34  import locale
    35  import logging
    36  logger = logging.getLogger('camelot.model.i18n')
    37

    38  locale.setlocale(locale.LC_ALL, '')
    39  language, encoding = locale.getlocale(locale.LC_ALL)
    40

    41  tr = lambda source:Translation.translate_or_register(source, language)
    42

    43 -class Translation(Entity):
    44    using_options(tablename='translation')
    45    language = Field(Unicode(20), index=True)
    46    source = Field(Unicode(500), index=True)
    47    value = Field(Unicode(500))
    48    cid = Field(INT(), default=0, index=True)
    49    uid = Field(INT(), default=0, index=True)
    50

    51    # cache, to prevent too much of the same sql queries

    52    _cache = dict()
    53

    54 -  class Admin(EntityAdmin):
    55      section = 'configuration'
    56      list_display = ['source', 'language', 'value', 'uid']
    57      list_filter = ['language']
    58

    59    @classmethod
    60 -  def translate(cls, source, language):
    61      """Translate source to language, return None if no translation is found"""
    62      if source:
    63        key = (source, language)
    64        if key in cls._cache:
    65          return cls._cache[key]
    66        translation = cls.query.filter_by(source=unicode(source), language=language).filter(Translation.uid!=0).first()
    67        if translation:
    68          cls._cache[key] = translation.value
    69          return translation.value
    70        return None
    71      return ''
    72

    73    @classmethod
    74 -  def translate_or_register(cls, source, language):
    75      """Translate source to language, if no translation is found, register the

    76      source as to be translated and return the source"""
    77      if source:
    78        source = unicode(source)
    79        translation = cls.translate(source, language)
    80        if not translation:
    81          if not cls.query.filter_by(source=source, language=language).first():
    82            if (source, language) not in cls._cache:
    83              from elixir import session
    84              registered_translation = Translation(source=source, language=language)
    85              cls._cache[(source, language)] = source
    86              session.flush([registered_translation])
    87              logger.debug('registed %s with id %s'%(source, registered_translation.id))
    88          return source
    89        return translation
    90      return ''
    91