[2178] | 1 | from types import StringType, UnicodeType |
---|
| 2 | from AccessControl import ClassSecurityInfo |
---|
| 3 | from Products.Localizer.Utils import charsets, get_language_name, lang_negotiator |
---|
| 4 | security = ClassSecurityInfo() |
---|
| 5 | security.declarePublic('gettext') |
---|
| 6 | # set default for add = 0 |
---|
| 7 | _marker = [] |
---|
| 8 | def 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 |
---|
| 48 | from Products.Localizer.MessageCatalog import MessageCatalog |
---|
| 49 | MessageCatalog.gettext = gettext |
---|
| 50 | |
---|