source: WAeUP_SRP/trunk/patches/PatchLocalizerMessageCatalog_gettext.py

Last change on this file was 3673, checked in by jens, 16 years ago
  • unclutter package by moving all patch files into a dedicated subfolder and importing it.
  • Property svn:keywords set to Id
File size: 1.6 KB
Line 
1from types import StringType, UnicodeType
2from AccessControl import ClassSecurityInfo
3from Products.Localizer.Utils import charsets, get_language_name, lang_negotiator
4security = ClassSecurityInfo()
5security.declarePublic('gettext')
6# set default for add = 0
7_marker = []
8def gettext(self, message, lang=None, add=0, default=_marker):
9    """Returns the message translation from the database if available.
10
11    If add=1, add any unknown message to the database.
12    If a default is provided, use it instead of the message id
13    as a translation for unknown messages.
14    """
15
16    if type(message) not in (StringType, UnicodeType):
17        raise TypeError, 'only strings can be translated.'
18
19    message = message.strip()
20
21    if default is _marker:
22        default = message
23
24    # Add it if it's not in the dictionary
25    if add and not self._messages.has_key(message) and message:
26        self._messages[message] = PersistentMapping()
27
28    # Get the string
29    if self._messages.has_key(message):
30        m = self._messages[message]
31
32        if lang is None:
33            # Builds the list of available languages
34            # should the empty translations be filtered?
35            available_languages = list(self._languages)
36
37            # Get the language!
38            lang = lang_negotiator(available_languages)
39
40            # Is it None? use the default
41            if lang is None:
42                lang = self._default_language
43
44        if lang is not None:
45            return m.get(lang) or default
46
47    return default
48from Products.Localizer.MessageCatalog import MessageCatalog
49MessageCatalog.gettext = gettext
50
Note: See TracBrowser for help on using the repository browser.