2011-01-14 02:06:25 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
import os.path
|
2011-12-05 08:11:29 -06:00
|
|
|
from appy.gen.wrappers import AbstractWrapper
|
2011-01-14 02:06:25 -06:00
|
|
|
from appy.gen.po import PoFile, PoMessage
|
|
|
|
from appy.shared.utils import getOsTempFolder
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class TranslationWrapper(AbstractWrapper):
|
2011-09-06 14:46:57 -05:00
|
|
|
def label(self, field):
|
2011-01-14 02:06:25 -06:00
|
|
|
'''The label for a text to translate displays the text of the
|
|
|
|
corresponding message in the source translation.'''
|
|
|
|
tool = self.tool
|
|
|
|
sourceLanguage = self.o.getProductConfig().sourceLanguage
|
|
|
|
sourceTranslation = getattr(tool.o, sourceLanguage).appy()
|
|
|
|
# p_field is the Computed field. We need to get the name of the
|
|
|
|
# corresponding field holding the translation message.
|
|
|
|
fieldName = field.name[:-6]
|
|
|
|
# If we are showing the source translation, we do not repeat the message
|
|
|
|
# in the label.
|
|
|
|
if self.id == sourceLanguage:
|
|
|
|
sourceMsg = ''
|
|
|
|
else:
|
|
|
|
sourceMsg = getattr(sourceTranslation,fieldName)
|
|
|
|
# When editing the value, we don't want HTML code to be interpreted.
|
2011-04-26 13:49:33 -05:00
|
|
|
# This way, the translator sees the HTML tags and can reproduce them
|
|
|
|
# in the translation.
|
|
|
|
url = self.request['URL']
|
2011-11-25 11:01:20 -06:00
|
|
|
if url.endswith('/ui/edit') or url.endswith('/do'):
|
2011-01-14 02:06:25 -06:00
|
|
|
sourceMsg = sourceMsg.replace('<','<').replace('>','>')
|
|
|
|
sourceMsg = sourceMsg.replace('\n', '<br/>')
|
|
|
|
return '<div class="translationLabel"><acronym title="%s">' \
|
2011-12-01 13:53:13 -06:00
|
|
|
'<img src="ui/help.png"/></acronym>%s</div>' % \
|
2011-01-14 02:06:25 -06:00
|
|
|
(fieldName, sourceMsg)
|
|
|
|
|
2011-09-06 14:46:57 -05:00
|
|
|
def show(self, field):
|
2011-01-14 02:06:25 -06:00
|
|
|
'''We show a field (or its label) only if the corresponding source
|
|
|
|
message is not empty.'''
|
|
|
|
tool = self.tool
|
|
|
|
if field.type == 'Computed': name = field.name[:-6]
|
|
|
|
else: name = field.name
|
|
|
|
# Get the source message
|
|
|
|
sourceLanguage = self.o.getProductConfig().sourceLanguage
|
|
|
|
sourceTranslation = getattr(tool.o, sourceLanguage).appy()
|
|
|
|
sourceMsg = getattr(sourceTranslation, name)
|
|
|
|
if field.isEmptyValue(sourceMsg): return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
poReplacements = ( ('\r\n', '<br/>'), ('\n', '<br/>'), ('"', '\\"') )
|
|
|
|
def getPoFile(self):
|
|
|
|
'''Computes and returns the PO file corresponding to this
|
|
|
|
translation.'''
|
|
|
|
tool = self.tool
|
|
|
|
fileName = os.path.join(getOsTempFolder(),
|
|
|
|
'%s-%s.po' % (tool.o.getAppName(), self.id))
|
|
|
|
poFile = PoFile(fileName)
|
|
|
|
for field in self.fields:
|
|
|
|
if (field.name == 'title') or (field.type != 'String'): continue
|
|
|
|
# Adds the PO message corresponding to this field
|
|
|
|
msg = field.getValue(self.o) or ''
|
|
|
|
for old, new in self.poReplacements:
|
|
|
|
msg = msg.replace(old, new)
|
|
|
|
poFile.addMessage(PoMessage(field.name, msg, ''))
|
|
|
|
poFile.generate()
|
|
|
|
return True, file(fileName)
|
|
|
|
|
|
|
|
def validate(self, new, errors):
|
|
|
|
# Call a custom "validate" if any.
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('validate', new, errors)
|
2011-01-14 02:06:25 -06:00
|
|
|
|
|
|
|
def onEdit(self, created):
|
|
|
|
# Call a custom "onEdit" if any.
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('onEdit', created)
|
2011-01-14 02:06:25 -06:00
|
|
|
|
|
|
|
def onDelete(self):
|
|
|
|
# Call a custom "onDelete" if any.
|
2011-02-15 08:59:55 -06:00
|
|
|
self.log('Translation "%s" deleted by "%s".' % (self.id, self.user.id))
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('onDelete')
|
2011-01-14 02:06:25 -06:00
|
|
|
# ------------------------------------------------------------------------------
|