Added a fallback mechanism for Appy translations. Bugfix while reindexing multivalued Strings.

This commit is contained in:
Gaetan Delannay 2011-01-17 14:49:56 +01:00
parent ead9f7c2de
commit e5cef2b8a4
5 changed files with 36 additions and 11 deletions

View file

@ -1178,11 +1178,16 @@ class String(Type):
(res.startswith('\n') or res.startswith('\r\n')): res = ' ' + res
return res
emptyStringTuple = ('',)
def getIndexValue(self, obj, forSearch=False):
'''For indexing purposes, we return only strings, not unicodes.'''
res = Type.getIndexValue(self, obj, forSearch)
if isinstance(res, unicode):
res = res.encode('utf-8')
# Ugly portal_catalog: if I give an empty tuple as index value,
# portal_catalog keeps the previous value! If I give him a tuple
# containing an empty string, it is ok.
if isinstance(res, tuple) and not res: res = self.emptyStringTuple
return res
def getPossibleValues(self,obj,withTranslations=False,withBlankValue=False):

View file

@ -501,7 +501,7 @@ class PloneInstaller:
self.manageIndexes()
self.manageLanguages()
self.finalizeInstallation()
self.appyTool.log("Installation of %s done." % self.productName)
self.appyTool.log("Installation done.")
def uninstallTool(self):
site = self.ploneSite

View file

@ -393,8 +393,7 @@ class ToolMixin(BaseMixin):
if not self.translationMapping['portal_path']:
self.translationMapping['portal_path'] = \
self.portal_url.getPortalPath()
appName = self.getProductConfig().PROJECTNAME
return self.utranslate(label, self.translationMapping, domain=appName)
return self.translate(label, mapping=self.translationMapping)
def getPublishedObject(self):
'''Gets the currently published object, if its meta_class is among

View file

@ -1044,6 +1044,20 @@ class BaseMixin:
params = ''
return '%s%s' % (base, params)
def getLanguage(self):
'''Gets the language (code) of the current user.'''
# Try first the "LANGUAGE" key from the request
res = self.REQUEST.get('LANGUAGE', None)
if res: return res
# Try then the HTTP_ACCEPT_LANGUAGE key from the request, which stores
# language preferences as defined in the user's browser. Several
# languages can be listed, from most to less wanted.
res = self.REQUEST.get('HTTP_ACCEPT_LANGUAGE', None)
if not res: return 'en'
if ',' in res: res = res[:res.find(',')]
if '-' in res: res = res[:res.find('-')]
return res
def translate(self, label, mapping={}, domain=None, default=None,
language=None):
'''Translates a given p_label into p_domain with p_mapping.'''
@ -1062,14 +1076,21 @@ class BaseMixin:
else:
# We will get the translation from a Translation object.
# In what language must we get the translation?
if not language: language = self.REQUEST['LANGUAGE']
if not language: language = self.getLanguage()
tool = self.getTool()
translation = getattr(self.getTool(), language).appy()
try:
translation = getattr(tool, language).appy()
except AttributeError:
# We have no translation for this language. Fallback to 'en'.
translation = getattr(tool, 'en').appy()
res = getattr(translation, label, '')
if not res:
# Fallback to 'en'.
translation = getattr(tool, 'en').appy()
res = getattr(translation, label, '')
# Perform replacements if needed
for name, repl in mapping.iteritems():
res = res.replace('${%s}' % name, repl)
# At present, there is no fallback machanism.
return res
def getPageLayout(self, layoutType):

View file

@ -15,9 +15,9 @@
<td tal:define="titleIsClickable python: member.has_role('Manager') and rootClasses">
<a tal:condition="titleIsClickable"
tal:attributes="href python:'%s?type_name=%s' % (queryUrl, ','.join(rootClasses))"
tal:content="python: tool.translate(appName)"></a>
tal:content="structure python: tool.translate(appName)"></a>
<span tal:condition="not: titleIsClickable"
tal:replace="python: tool.translate(appName)"/>
tal:replace="structure python: tool.translate(appName)"/>
</td>
<td align="right">
<img style="cursor:pointer"
@ -43,7 +43,7 @@
<td>
<a tal:attributes="href python: '%s?type_name=%s' % (queryUrl, rootClass);
class python:test(not currentSearch and (currentType==rootClass), 'portletCurrent', '')"
tal:content="python: tool.translate(rootClass + '_plural')"></a>
tal:content="structure python: tool.translate(rootClass + '_plural')"></a>
</td>
<td align="right"
tal:define="addPermission python: '%s: Add %s' % (appName, rootClass);
@ -151,7 +151,7 @@
<table tal:condition="displayLink" cellpadding="0" cellspacing="0" width="100%">
<tr tal:define="pageName python: phase['pages'][0]">
<td><a tal:attributes="href python: contextObj.getUrl(page=pageName)"
tal:content="python: tool.translate(label)"></a>
tal:content="structure python: tool.translate(label)"></a>
</td>
<td align="right">
<img title="Edit" i18n:domain="plone" i18n:attributes="title" style="cursor:pointer"
@ -162,7 +162,7 @@
</tr>
</table>
<tal:comment replace="nothing">Several pages in the phase</tal:comment>
<span tal:condition="not: displayLink" tal:replace="python: tool.translate(label)"/>
<span tal:condition="not: displayLink" tal:replace="structure python: tool.translate(label)"/>
</div>
<div class="portletMenu">
<table cellpadding="0" cellspacing="0" width="100%"