Commit 2919488b authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

add 'policy' property in message catalog.

so that we can disable adding new entries or we log the backtrace that
causes a new entry.
parent 50d79dcc
...@@ -27,6 +27,7 @@ from hashlib import md5 ...@@ -27,6 +27,7 @@ from hashlib import md5
from re import compile from re import compile
from time import gmtime, strftime, time from time import gmtime, strftime, time
from urllib import quote from urllib import quote
from traceback import format_list, extract_stack
# Import from polib # Import from polib
import polib import polib
...@@ -44,6 +45,7 @@ from zope.component import getSiteManager ...@@ -44,6 +45,7 @@ from zope.component import getSiteManager
from zope.i18n import interpolate from zope.i18n import interpolate
from zope.i18n.interfaces import ITranslationDomain from zope.i18n.interfaces import ITranslationDomain
from zope.interface import implements from zope.interface import implements
from zLOG import LOG, INFO
# Import from Localizer # Import from Localizer
from interfaces import IMessageCatalog from interfaces import IMessageCatalog
...@@ -161,10 +163,16 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -161,10 +163,16 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
security = ClassSecurityInfo() security = ClassSecurityInfo()
POLICY_ADD_FALSE = 0
POLICY_ADD_TRUE = 1
POLICY_ADD_LOG = 2
def __init__(self, id, title, sourcelang, languages): def __init__(self, id, title, sourcelang, languages):
self.id = id self.id = id
self.title = title self.title = title
self.policy = self.POLICY_ADD_TRUE
# Language Manager data # Language Manager data
self._languages = tuple(languages) self._languages = tuple(languages)
...@@ -260,7 +268,7 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -260,7 +268,7 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
security.declarePublic('gettext') security.declarePublic('gettext')
def gettext(self, message, lang=None, add=1, default=None): def gettext(self, message, lang=None, add=None, default=None):
"""Returns the message translation from the database if available. """Returns the message translation from the database if available.
If add=1, add any unknown message to the database. If add=1, add any unknown message to the database.
...@@ -280,7 +288,11 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -280,7 +288,11 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
message = self.get_message_key(message) or to_unicode(message) message = self.get_message_key(message) or to_unicode(message)
# Add it if it's not in the dictionary # Add it if it's not in the dictionary
if add and not self._messages.has_key(message) and message: if add is None:
add = getattr(self, 'policy', self.POLICY_ADD_TRUE)
if add != self.POLICY_ADD_FALSE and not self._messages.has_key(message) and message:
if add == self.POLICY_ADD_LOG:
LOG('New entry added to message catalog %s :' % self.id, INFO, '%s\n%s' % (message, ''.join(format_list(extract_stack()[:-1]))))
self._messages[message] = PersistentMapping() self._messages[message] = PersistentMapping()
# Get the string # Get the string
...@@ -485,10 +497,11 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -485,10 +497,11 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
security.declareProtected('View management screens', 'manage_properties') security.declareProtected('View management screens', 'manage_properties')
def manage_properties(self, title, REQUEST=None, RESPONSE=None): def manage_properties(self, title, policy, REQUEST=None, RESPONSE=None):
"""Change the Message Catalog properties. """Change the Message Catalog properties.
""" """
self.title = title self.title = title
self.policy = int(policy)
if RESPONSE is not None: if RESPONSE is not None:
RESPONSE.redirect('manage_propertiesForm') RESPONSE.redirect('manage_propertiesForm')
...@@ -543,6 +556,19 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -543,6 +556,19 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
manage_Import_form = LocalDTMLFile('ui/MC_Import_form', globals()) manage_Import_form = LocalDTMLFile('ui/MC_Import_form', globals())
security.declarePublic('get_policies')
def get_policies(self):
""" """
if not hasattr(self, 'policy'):
self.policy = self.POLICY_ADD_TRUE
policies = [
[self.POLICY_ADD_FALSE, "Never add new entries automatically"],
[self.POLICY_ADD_TRUE, "Add new entries automatically if missing"],
[self.POLICY_ADD_LOG, "Add new entries automatically if missing and log the backtrace"],
]
return policies
security.declarePublic('get_charsets') security.declarePublic('get_charsets')
def get_charsets(self): def get_charsets(self):
""" """ """ """
......
...@@ -23,6 +23,21 @@ ...@@ -23,6 +23,21 @@
<input type="text" name="title" value="<dtml-var title>"> <input type="text" name="title" value="<dtml-var title>">
</td> </td>
</tr> </tr>
<tr>
<th align="right">
<dtml-var "gettext('Policy')">
</th>
<td>
<select name="policy">
<dtml-in get_policies prefix=sequence>
<option value="<dtml-var "sequence_item[0]">"
<dtml-if "policy == sequence_item[0]">selected</dtml-if>>
<dtml-var "sequence_item[1]">
</option>
</dtml-in>
</select>
</td>
</tr>
<tr> <tr>
<th></th> <th></th>
<td> <td>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment