1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
"""
This tool requires a translation service which supports
the utranslate method and the default parameter.
By time of writing this code, that is only valid for PTS.
"""
from Globals import InitializeClass
from OFS.SimpleItem import SimpleItem
from Products.CMFCore.utils import UniqueObject
from Products.CMFPlone import ToolNames
from AccessControl import ClassSecurityInfo
from Products.CMFPlone.PloneBaseTool import PloneBaseTool
from Products.CMFCore.utils import getToolByName
from i18nl10n import utranslate, ulocalized_time, \
monthname_msgid, monthname_msgid_abbr, \
weekdayname_msgid, weekdayname_msgid_abbr, \
weekdayname_msgid_short, \
monthname_english, weekdayname_english
class TranslationServiceTool(PloneBaseTool, UniqueObject, SimpleItem):
""" Utility methods to access the translation machinery """
id = 'translation_service'
meta_type = ToolNames.TranslationServiceTool
toolicon = 'skins/plone_images/site_icon.gif'
security = ClassSecurityInfo()
__implements__ = (PloneBaseTool.__implements__,
SimpleItem.__implements__, )
security.declarePublic('utranslate')
def utranslate(self, *args, **kw):
# Translate method to access the translation service
# from resticted code like skins.
return utranslate(*args, **kw)
security.declarePublic('encode')
def encode(self, m, input_encoding=None, output_encoding=None, errors='strict'):
# encode a give unicode type or string type to string type in encoding output_encoding
# check if input is not type unicode
if not isinstance(m, unicode):
if input_encoding is None: input_encoding = 'utf-8'
m = unicode(str(m), input_encoding, errors)
if output_encoding is None:
# get output encoding from portal
plone_tool = getToolByName(self, 'plone_utils')
output_encoding = plone_tool.getSiteEncoding()
# return as type string
return m.encode(output_encoding, errors)
security.declarePublic('asunicodetype')
def asunicodetype(self, m, input_encoding=None, errors='strict'):
# create type unicode from type string
if isinstance(m, unicode): return m
if input_encoding is None:
# get input encoding from portal
plone_tool = getToolByName(self, 'plone_utils')
input_encoding = plone_tool.getSiteEncoding()
# return as type unicode
return unicode(str(m), input_encoding, errors)
security.declarePublic('ulocalized_time')
def ulocalized_time(self, time, long_format = None, context = None, domain='plone'):
# get some context if none is passed
if context is None: context = self
return ulocalized_time(time, long_format, context, domain)
security.declarePublic('day_msgid')
def day_msgid(self, number, format=''):
""" Returns the msgid which can be passed to the translation service for
l10n of weekday names. Format is either '', 'a' or 's'.
>>> ttool = self.portal.translation_service
>>> ttool.day_msgid(0)
'weekday_sun'
>>> ttool.day_msgid(6)
'weekday_sat'
>>> ttool.day_msgid(0, format='a')
'weekday_sun_abbr'
>>> ttool.day_msgid(3, format='s')
'weekday_wed_short'
"""
#
if format == 's':
# short format
method = weekdayname_msgid_short
elif format == 'a':
# abbreviation
method = weekdayname_msgid_abbr
else:
# long format
method = weekdayname_msgid
return method(number)
security.declarePublic('month_msgid')
def month_msgid(self, number, format=''):
""" Returns the msgid which can be passed to the translation service for
l10n of month names. Format is either '' or 'a' (long or abbreviation).
>>> ttool = self.portal.translation_service
>>> ttool.month_msgid(1)
'month_jan'
>>> ttool.month_msgid(12)
'month_dec'
>>> ttool.month_msgid(6, format='a')
'month_jun_abbr'
"""
return 'a' == format and monthname_msgid_abbr(number) or monthname_msgid(number)
security.declarePublic('month_english')
def month_english(self, number, format=''):
""" Returns the english name of month by number. Format is either '' or
'a' (long or abbreviation).
>>> ttool = self.portal.translation_service
>>> ttool.month_english(1)
'January'
>>> ttool.month_english(1, 'a')
'Jan'
"""
return monthname_english(number, format=format)
security.declarePublic('weekday_english')
def weekday_english(self, number, format=''):
""" Returns the english name of a week by number. Format is either '',
'a' or 'p'.
>>> ttool = self.portal.translation_service
>>> ttool.weekday_english(0)
'Sunday'
>>> ttool.weekday_english(6)
'Saturday'
>>> ttool.weekday_english(0, format='a')
'Sun'
>>> ttool.weekday_english(3, format='p')
'Wed.'
"""
return weekdayname_english(number, format=format)
InitializeClass(TranslationServiceTool)
|