Package turbogears :: Package i18n :: Module kidutils

Source Code for Module turbogears.i18n.kidutils

  1  """Functions supporting translation of strings in Kid templates 
  2  """ 
  3   
  4  from kid.parser import START, TEXT, END 
  5  from turbogears import config 
  6  # use plain_gettext because Kid's template strings are always evaluated 
  7  # immediately 
  8  from turbogears.i18n.tg_gettext import plain_gettext as gettext 
  9   
 10   
11 -def translate(item, attr=None):
12 """Translates the text of element plus the text of all child elements. 13 14 If 'attr' is present this is used to provide the locale name; if not then the 15 value provided by 'get_locale' is used. For example:: 16 17 <div lang=""> 18 this is a test 19 <a href="de">testing</a> 20 </div> 21 22 The string 'this is a test' is rendered by the locale provided by 23 'get_locale', the string 'testing' by the German locale. 24 25 Possible use in Kid template:: 26 27 <?python 28 from turbogears.i18n import translate 29 ?> 30 <translate xmlns:py="http://purl.org/kid/ns#" 31 py:match="'lang' in item.attrib" 32 py:replace="translate(item)" /> 33 <h1 lang="">Welcome!</h1> 34 35 @param item: element to be translated 36 @type item: ElementTree element 37 38 @param attr: attribute name used to store locale, if any 39 @type attr: str 40 41 """ 42 if attr is None: 43 attr = config.get("i18n.templateLocaleAttribute", "lang") 44 45 translate_all(item, item.get(attr), attr) 46 return item
47
48 -def __translate_text(text, lang):
49 prefix = '' 50 postfix = '' 51 if len(text) > 0 and text[0].isspace(): 52 prefix = text[0] 53 54 if len(text) > 1 and text[-1].isspace(): 55 postfix = text[-1] 56 57 return prefix + gettext(text.strip(), lang) + postfix
58
59 -def translate_all(tree, lang, attr, inroot=True):
60 """Recursive function to translate all text in child elements. 61 62 @param tree: parent ElementTree element 63 @param lang: language setting 64 @param attr: attribute name used to store locale 65 66 """ 67 if tree.text: 68 tree.text = __translate_text(tree.text, lang) 69 70 if tree.tail and not inroot: 71 # Don't translate tail of root. It is beyond the scope of the lang attr 72 tree.tail = __translate_text(tree.tail, lang) 73 74 for element in tree: 75 # check overriding lang attribute 76 if element.get(attr):lang = element.get(attr) 77 translate_all(element, lang, attr, False)
78
79 -def i18n_filter(stream, template, locale=None):
80 """Kid template filter translating all elements matching lang attribute. 81 82 The name of of the attribute is set in the configuration as 83 'i18n.templateLocaleAttribute' and defaults to 'lang'. 84 85 """ 86 lang_attr = config.get("i18n.templateLocaleAttribute", "lang") 87 locales = [locale] 88 89 for ev, item in stream: 90 91 if ev == START: 92 l = item.get(lang_attr) 93 if l: 94 locale = l 95 locales.append(l) 96 97 elif ev == TEXT: 98 prefix = '' 99 postfix = '' 100 if len(item) > 0 and item[0] == ' ': 101 prefix = ' ' 102 103 if len(item) > 1 and item[-1] == ' ': 104 postfix = ' ' 105 106 text = item.strip() 107 if text: 108 item = gettext(text, locale) 109 item = prefix + item + postfix 110 111 elif ev == END: 112 if item.get(lang_attr): 113 locales.pop() 114 locale = locales[-1] 115 116 yield (ev, item)
117