Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes
kupu.python.nationalizer.Nationalizer Class Reference
Collaboration diagram for kupu.python.nationalizer.Nationalizer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def translate
def parse_po_file
def apply_i18n
def apply_translate
def apply_attributes
def reduce_whitespace
def get_po_file_path
def serialize
def entitize

Public Attributes

 htmlfile
 locale

Static Public Attributes

list not_single

Detailed Description

Translates string in an HTML or XML file using i18n: directives

Definition at line 25 of file nationalizer.py.


Constructor & Destructor Documentation

def kupu.python.nationalizer.Nationalizer.__init__ (   self,
  htmlfile,
  locale 
)

Definition at line 46 of file nationalizer.py.

00046 
00047     def __init__(self, htmlfile, locale):
00048         self.htmlfile = htmlfile
00049         self.locale = locale


Member Function Documentation

def kupu.python.nationalizer.Nationalizer.apply_attributes (   self,
  node,
  msgcat 
)
handle Zope-style i18n:attributes

Definition at line 130 of file nationalizer.py.

00130 
00131     def apply_attributes(self, node, msgcat):
00132         """handle Zope-style i18n:attributes"""
00133         attrnames = node.getAttributeNS(I18NNS, 'attributes').split(' ')
00134         for attr in attrnames:
00135             value = node.getAttribute(attr)
00136             if value and msgcat.has_key(value):
00137                 node.setAttribute(attr, unicode(msgcat[value], 'UTF-8'))
00138         node.removeAttributeNS(I18NNS, 'attributes')

Here is the call graph for this function:

Here is the caller graph for this function:

def kupu.python.nationalizer.Nationalizer.apply_i18n (   self,
  dom,
  msgcat 
)
apply nationalization of the full dom

Definition at line 99 of file nationalizer.py.

00099 
00100     def apply_i18n(self, dom, msgcat):
00101         """apply nationalization of the full dom"""
00102         nodes = dom.documentElement.getElementsByTagName('*')
00103         for node in nodes:
00104             if node.hasAttributeNS(I18NNS, 'translate'):
00105                 self.apply_translate(node, msgcat)
00106             if node.hasAttributeNS(I18NNS, 'attributes'):
00107                 self.apply_attributes(node, msgcat)

Here is the call graph for this function:

Here is the caller graph for this function:

def kupu.python.nationalizer.Nationalizer.apply_translate (   self,
  node,
  msgcat 
)
handle Zope-style i18n:translate

Definition at line 108 of file nationalizer.py.

00108 
00109     def apply_translate(self, node, msgcat):
00110         """handle Zope-style i18n:translate"""
00111         buf = []
00112         msgid = msgstr = node.getAttributeNS(I18NNS, 'translate').strip()
00113         if not msgid:
00114             # no msgid in the attribute, use the node value
00115             for child in node.childNodes:
00116                 if child.nodeType == 3:
00117                     buf.append(child.nodeValue)
00118                 else:
00119                     raise TypeError, \
00120                         ('illegal element %s in i18n:translate element' % 
00121                             child.nodeName)
00122             msgid = msgstr = self.reduce_whitespace(u''.join(buf).strip())
00123         if msgcat.has_key(msgid):
00124             msgstr = msgcat[msgid]
00125         # now replace the contents of the node with the new contents
00126         while node.hasChildNodes():
00127             node.removeChild(node.firstChild)
00128         node.removeAttributeNS(I18NNS, 'translate')
00129         node.appendChild(node.ownerDocument.createTextNode(msgstr))

Here is the call graph for this function:

Here is the caller graph for this function:

def kupu.python.nationalizer.Nationalizer.entitize (   self,
  string 
)

Definition at line 183 of file nationalizer.py.

00183 
00184     def entitize(self, string):
00185         string = string.replace('&', '&')
00186         string = string.replace('<', '&lt;')
00187         string = string.replace('>', '&gt;')
00188         string = string.replace('"', '&quot;')
00189         return string
        

Here is the caller graph for this function:

Definition at line 146 of file nationalizer.py.

00146 
00147     def get_po_file_path(self, locale):
00148         for language in locale:
00149             startdir = '../i18n'
00150             language = language.split('-')
00151             pathstart = '%s/kupu-%s' % (startdir, language[0])
00152             paths = []
00153             if len(language) == 2:
00154                 paths.append('%s-%s.po' % (pathstart, language[1]))
00155             paths += [
00156                 '%s-default.po' % pathstart,
00157                 '%s.po' % pathstart,
00158                 ]
00159             for path in paths:
00160                 if os.path.isfile(path):
00161                     return path

Here is the caller graph for this function:

parse the .po file, create a mapping msgid->msgstr

Definition at line 74 of file nationalizer.py.

00074 
00075     def parse_po_file(self, pofp):
00076         """parse the .po file, create a mapping msgid->msgstr"""
00077         cat = {}
00078         state = None
00079         msgid = None
00080         msgstr = None
00081         for line in pofp.readlines():
00082             line = line.strip()
00083             if line.startswith('#') or not line:
00084                 continue
00085             if line.startswith('msgid'):
00086                 if msgid and msgstr:
00087                     cat[msgid] = msgstr
00088                 msgid = line[7:-1]
00089                 state = ID
00090             elif line.startswith('msgstr'):
00091                 msgstr = line[8:-1]
00092             else:
00093                 # ignore for now, might be a multiline msgstr, if we
00094                 # want to support those we should add some code here...
00095                 pass
00096         if msgid and msgstr:
00097             cat[msgid] = msgstr
00098         return cat

Here is the caller graph for this function:

Definition at line 139 of file nationalizer.py.

00139 
00140     def reduce_whitespace(self, string):
00141         for char in ['\n', '\t', '\r']:
00142             string  = string.replace(char, ' ')
00143         while string.find('  ') > -1:
00144             string = string.replace('  ', ' ')
00145         return string

Here is the caller graph for this function:

Definition at line 162 of file nationalizer.py.

00162 
00163     def serialize(self, el):
00164         buf = []
00165         if el.nodeType == 1:
00166             buf.append('<%s' % el.nodeName)
00167             if len(el.attributes):
00168                 for attr, value in el.attributes.items():
00169                     if value is not None:
00170                         buf.append(' %s="%s"' % (attr, self.entitize(value)))
00171             if el.hasChildNodes() or el.nodeName in self.not_single:
00172                 buf.append('>')
00173                 for child in el.childNodes:
00174                     buf += self.serialize(child)
00175                 buf.append('</%s>' % el.nodeName)
00176             else:
00177                 buf.append(' />')
00178         elif el.nodeType == 3:
00179             buf.append(el.nodeValue)
00180         else:
00181             pass #print 'ignoring node of type', el.nodeType
00182         return ''.join([ustr(b) for b in buf])

Here is the call graph for this function:

Here is the caller graph for this function:

load and translate everything

Definition at line 50 of file nationalizer.py.

00050 
00051     def translate(self):
00052         """load and translate everything"""
00053         popath = self.get_po_file_path(self.locale)
00054         if popath is not None:
00055             pofp = open(popath)
00056             try:
00057                 msgcat = self.parse_po_file(pofp)
00058             finally:
00059                 pofp.close()
00060         else:
00061             # if no pofile, parse anyway to get rid of those nasty i18n:
00062             # attributes (obviously not very fast, perhaps we need to either
00063             # cache a parsed version and send that back or just remove the
00064             # attributes here)
00065             msgcat = {}
00066         xmlfp = open(self.htmlfile)
00067         try:
00068             xml = xmlfp.read()
00069         finally:
00070             xmlfp.close()
00071         dom = parseString(xml)
00072         self.apply_i18n(dom, msgcat)
00073         return self.serialize(dom.documentElement)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 47 of file nationalizer.py.

Definition at line 48 of file nationalizer.py.

Initial value:
['a', 'abbr', 'acronym', 'address', 'applet', 
                    'b', 'bdo', 'big', 'blink', 'blockquote', 
                    'button', 'caption', 'center', 'cite', 
                    'comment', 'del', 'dfn', 'dir', 'div',
                    'dl', 'dt', 'em', 'embed', 'fieldset',
                    'font', 'form', 'frameset', 'h1', 'h2',
                    'h3', 'h4', 'h5', 'h6', 'i', 'iframe',
                    'ins', 'kbd', 'label', 'legend', 'li',
                    'listing', 'map', 'marquee', 'menu',
                    'multicol', 'nobr', 'noembed', 'noframes',
                    'noscript', 'object', 'ol', 'optgroup',
                    'option', 'p', 'pre', 'q', 's', 'script',
                    'select', 'small', 'span', 'strike', 
                    'strong', 'style', 'sub', 'sup', 'table',
                    'tbody', 'td', 'textarea', 'tfoot',
                    'th', 'thead', 'title', 'tr', 'tt', 'u',
                    'ul', 'xmp']

Definition at line 28 of file nationalizer.py.


The documentation for this class was generated from the following file: