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
|
2013-01-09 03:46:14 -06:00
|
|
|
# Get the source language: either defined on the translation itself,
|
|
|
|
# either from the config object.
|
|
|
|
sourceLanguage = self.sourceLanguage
|
|
|
|
if not sourceLanguage:
|
2013-07-24 08:53:19 -05:00
|
|
|
sourceLanguage = self.o.getProductConfig(True).sourceLanguage
|
2011-01-14 02:06:25 -06:00
|
|
|
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']
|
2013-08-21 05:35:30 -05:00
|
|
|
if url.endswith('/edit') or url.endswith('/do'):
|
2011-01-14 02:06:25 -06:00
|
|
|
sourceMsg = sourceMsg.replace('<','<').replace('>','>')
|
|
|
|
sourceMsg = sourceMsg.replace('\n', '<br/>')
|
2013-01-09 03:46:14 -06:00
|
|
|
return '<div class="translationLabel"><acronym title="%s" ' \
|
2013-08-21 05:35:30 -05:00
|
|
|
'style="margin-right: 5px"><img src="help.png"/></acronym>' \
|
2013-01-09 03:46:14 -06:00
|
|
|
'%s</div>' % (fieldName, sourceMsg)
|
2011-01-14 02:06:25 -06:00
|
|
|
|
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
|
2013-07-24 08:53:19 -05:00
|
|
|
sourceLanguage = self.o.getProductConfig(True).sourceLanguage
|
2011-01-14 02:06:25 -06:00
|
|
|
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:
|
2013-01-09 03:46:14 -06:00
|
|
|
# Ignore labels and appy-specific fields in page 'actions'.
|
|
|
|
if (field.page.name == 'actions') or (field.type != 'String'):
|
|
|
|
continue
|
2011-01-14 02:06:25 -06:00
|
|
|
# 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
|
|
|
# ------------------------------------------------------------------------------
|