Back to index

plone3  3.1.7
TranslationServiceTool.py
Go to the documentation of this file.
00001 """
00002 This tool requires a translation service which supports
00003 the translate method and the default parameter.
00004 """
00005 from zope.interface import implements
00006 
00007 from AccessControl import ClassSecurityInfo
00008 from Globals import InitializeClass
00009 from OFS.SimpleItem import SimpleItem
00010 from Products.CMFCore.utils import getToolByName
00011 from Products.CMFCore.utils import UniqueObject
00012 from Products.CMFPlone import PloneLocalesMessageFactory as PLMF
00013 from Products.CMFPlone import ToolNames
00014 from Products.CMFPlone.interfaces import ITranslationServiceTool
00015 from Products.CMFPlone.PloneBaseTool import PloneBaseTool
00016 
00017 from i18nl10n import ulocalized_time, utranslate, \
00018                      monthname_msgid, monthname_msgid_abbr, \
00019                      weekdayname_msgid, weekdayname_msgid_abbr, \
00020                      weekdayname_msgid_short, \
00021                      monthname_english, weekdayname_english
00022 
00023 
00024 class TranslationServiceTool(PloneBaseTool, UniqueObject, SimpleItem):
00025     """ Utility methods to access the translation machinery """
00026 
00027     id = 'translation_service'
00028     meta_type = ToolNames.TranslationServiceTool
00029     toolicon = 'skins/plone_images/site_icon.gif'
00030     security = ClassSecurityInfo()
00031     implements(ITranslationServiceTool)
00032 
00033     __implements__ = (PloneBaseTool.__implements__,
00034                       SimpleItem.__implements__, )
00035 
00036     security.declarePublic('utranslate')
00037     def utranslate(self, *args, **kw):
00038         # Translate method to access the translation service
00039         # from resticted code like skins.
00040         return utranslate(*args, **kw)
00041 
00042     security.declarePublic('encode')
00043     def encode(self, m, input_encoding=None, output_encoding=None, errors='strict'):
00044         # encode a give unicode type or string type to string type in encoding output_encoding
00045 
00046         # check if input is not type unicode
00047         if not isinstance(m, unicode):
00048             if input_encoding is None: input_encoding = 'utf-8'
00049             m = unicode(str(m), input_encoding, errors)
00050 
00051         if output_encoding is None:
00052             # get output encoding from portal
00053             plone_tool = getToolByName(self, 'plone_utils')
00054             output_encoding = plone_tool.getSiteEncoding()
00055 
00056         # return as type string
00057         return m.encode(output_encoding, errors)
00058 
00059     security.declarePublic('asunicodetype')
00060     def asunicodetype(self, m, input_encoding=None, errors='strict'):
00061         # create type unicode from type string
00062 
00063         if isinstance(m, unicode): return m
00064 
00065         if input_encoding is None:
00066             # get input encoding from portal
00067             plone_tool = getToolByName(self, 'plone_utils')
00068             input_encoding = plone_tool.getSiteEncoding()
00069 
00070         # return as type unicode
00071         return unicode(str(m), input_encoding, errors)
00072 
00073     security.declarePublic('ulocalized_time')
00074     def ulocalized_time(self, time, long_format=None, context=None,
00075                         domain='plonelocales', request=None):
00076         # get some context if none is passed
00077         if context is None:
00078             context = self
00079         return ulocalized_time(time, long_format, context, domain, request)
00080 
00081     security.declarePublic('day_msgid')
00082     def day_msgid(self, number, format=None):
00083         """ Returns the msgid which can be passed to the translation service for
00084         l10n of weekday names. Format is either None, 'a' or 's'.
00085 
00086         >>> ttool = TranslationServiceTool()
00087 
00088         >>> ttool.day_msgid(0)
00089         'weekday_sun'
00090 
00091         >>> ttool.day_msgid(6)
00092         'weekday_sat'
00093 
00094         >>> ttool.day_msgid(0, format='a')
00095         'weekday_sun_abbr'
00096 
00097         >>> ttool.day_msgid(3, format='s')
00098         'weekday_wed_short'
00099         """
00100         # 
00101         if format == 's':
00102             # short format
00103             method = weekdayname_msgid_short
00104         elif format == 'a':
00105             # abbreviation
00106             method = weekdayname_msgid_abbr
00107         else:
00108             # long format
00109             method = weekdayname_msgid
00110         return method(number)
00111 
00112     security.declarePublic('month_msgid')
00113     def month_msgid(self, number, format=None):
00114         """ Returns the msgid which can be passed to the translation service for
00115         l10n of month names. Format is either '' or 'a' (long or abbreviation).
00116 
00117         >>> ttool = TranslationServiceTool()
00118 
00119         >>> ttool.month_msgid(1)
00120         'month_jan'
00121 
00122         >>> ttool.month_msgid(12)
00123         'month_dec'
00124 
00125         >>> ttool.month_msgid(6, format='a')
00126         'month_jun_abbr'
00127         """
00128         return 'a' == format and monthname_msgid_abbr(number) or monthname_msgid(number)
00129 
00130     security.declarePublic('month_english')
00131     def month_english(self, number, format=None):
00132         """ Returns the english name of month by number. Format is either '' or
00133         'a' (long or abbreviation).
00134 
00135         >>> ttool = TranslationServiceTool()
00136 
00137         >>> ttool.month_english(1)
00138         'January'
00139 
00140         >>> ttool.month_english(1, 'a')
00141         'Jan'
00142         """
00143         return monthname_english(number, format=format)
00144 
00145     security.declarePublic('month')
00146     def month(self, number, format=None, default=None):
00147         """ Returns a Message with the month name, that can be translated by
00148         the TAL engine. Format is either None or 'a' (long or abbreviation).
00149         """
00150         if default is None:
00151             default = monthname_english(number, format=format)
00152         value = 'a' == format and monthname_msgid_abbr(number) or monthname_msgid(number)
00153         return PLMF(value, default=default)
00154 
00155     security.declarePublic('weekday_english')
00156     def weekday_english(self, number, format=None):
00157         """ Returns the english name of a week by number. Format is either None,
00158         'a' or 'p'.
00159 
00160         >>> ttool = TranslationServiceTool()
00161 
00162         >>> ttool.weekday_english(0)
00163         'Sunday'
00164 
00165         >>> ttool.weekday_english(6)
00166         'Saturday'
00167 
00168         >>> ttool.weekday_english(0, format='a')
00169         'Sun'
00170 
00171         >>> ttool.weekday_english(3, format='p')
00172         'Wed.'
00173         """
00174         return weekdayname_english(number, format=format)
00175 
00176 InitializeClass(TranslationServiceTool)