Changeset 3502
- Timestamp:
- 09/16/07 07:41:27 (1 year ago)
- Files:
-
- branches/1.0/turbogears/command/i18n.py (modified) (2 diffs)
- branches/1.0/turbogears/tests/test_command_i18n.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/turbogears/command/i18n.py
r3468 r3502 26 26 from turbogears.util import get_model, load_project_config, get_package_name 27 27 from pkg_resources import resource_filename 28 import kid 28 29 29 30 class ProgramError(StandardError): … … 327 328 keys = [] 328 329 kid_expr_re = re.compile(r"_\(('(?P<texta>[^']*)'|\"(?P<textb>[^\"]*)\")\)") 330 331 def process_text(is_attribute, k, tag): 332 key = None 333 s = kid_expr_re.search(k) 334 if s: 335 key = s.groupdict()['texta'] or s.groupdict()['textb'] 336 # DBR start 337 # we don't have a kid expression in there 338 # so it is "just" a text entry - which we want to 339 # be translated!! 340 elif not is_attribute: 341 import kid.codewriter as cw 342 parts = cw.interpolate(k) 343 if isinstance(parts, list) and len(parts) > 1: 344 print "Warning: Mixed content in tag <%s>: %s" % (tag, k) 345 elif isinstance(parts, basestring): 346 key = k 347 # DBR end 348 if key and (key not in keys) and (tag not in tags_to_ignore): 349 messages.append((tag, fname, key)) 350 keys.append(key) 351 329 352 for fname in files: 330 353 print 'Working on', fname 331 354 tree = None 355 content = open(fname).read() 332 356 try: 333 tree = ElementTree(file=fname).getroot()357 tree = kid.document(fname) 334 358 except Exception, e: 335 359 print 'Skip %s: %s' % (fname, e) 336 360 continue 337 361 sentinel = None 338 for el in tree.getiterator(): 362 tag = None 363 for type, el in tree: 339 364 # if we have a lang-attribute, ignore this 340 365 # node AND all it's descendants. 341 if el.get('lang', None) is not None: 342 # if we have children, set the last 343 # one to be the sentinel. Otherwise, 344 # just continue 345 if len(el) > 0: 346 sentinel = el[-1] 347 continue 348 if sentinel is not None: 349 if sentinel == el: 366 #print type, el 367 if type == kid.parser.START: 368 if el.get('lang', None) is not None: 369 sentinel = el 370 continue 371 # set the tag from the current one. 372 tag = re.sub('({[^}]+})?(\w+)', '\\2', el.tag) 373 # process the attribute texts 374 for attrib_text in el.attrib.values(): 375 process_text(True, attrib_text, tag) 376 if type == kid.parser.END: 377 if el is sentinel: 350 378 sentinel = None 351 continue 352 if self.options.loose_kid_support: 353 tag = re.sub('({[^}]+})?(\w+)', '\\2', el.tag) 354 ents = [] 355 if el.text: ents = [(False, el.text.strip())] 356 if el.tail is not None: 357 ents.append((False, el.tail.strip())) 358 if el.attrib: 359 ents.extend([(True, v) for v in el.attrib.values()]) 360 for is_attribute, k in ents: 361 key = None 362 s = kid_expr_re.search(k) 363 if s: 364 key = s.groupdict()['texta'] or s.groupdict()['textb'] 365 # DBR start 366 # we don't have a kid expression in there 367 # so it is "just" a text entry - which we want to 368 # be translated!! 369 elif not is_attribute: 370 import kid.codewriter as cw 371 parts = cw.interpolate(k) 372 if isinstance(parts, list) and len(parts) > 1: 373 print "Warning: Mixed content in tag <%s>: %s" % (tag, k) 374 elif isinstance(parts, basestring): 375 key = k 376 # DBR end 377 if key and (key not in keys) and (tag not in tags_to_ignore): 378 messages.append((tag, fname, key)) 379 keys.append(key) 379 if type == kid.parser.TEXT and sentinel is None and el.strip(): 380 process_text(False, el, tag) 380 381 fd = open(potfile, 'at+') 381 382 for tag,fname,text in messages: branches/1.0/turbogears/tests/test_command_i18n.py
r3364 r3502 34 34 tool.run() 35 35 assert os.path.isdir(locale_dir), "locale directory not created" 36 37 TEMPLATE = """ 38 <html xmlns:py="http://purl.org/kid/ns#"> 39 <head> 40 <link py:strip="1" py:for="css in tg_css">${css.display()}</link> 41 <link py:strip="1" py:for="js in tg_js_head">${js.display()}</link> 42 </head> 43 <body> 44 <div>Some text to be i18n'ed</div> 45 <div>This is text that has a kid-expression ${_('which is to be i18n')}</div> 46 <div foo="${_('kid expression in attribute')}"/> 47 <div foo="normal attribute text"/> 48 <div lang="en">This is english, and it shouldn't be collected</div> 49 <div lang="de">Dies ist Deutsch, und es sollte nicht aufgesammelt werden</div> 50 <div>These are some entities that we shouldn't complain about: </div> 51 </body> 52 </html> 53 """ 54 def test_collect_template_strings(): 55 "Verify the locale directory got created as needed." 56 test_src_dir = os.path.join(work_dir, 'src') 57 os.mkdir(test_src_dir) 58 tf = open(os.path.join(test_src_dir, "test.kid"), "w") 59 tf.write(TEMPLATE) 60 tf.close() 61 sys.argv = ['i18n.py', '--src-dir', test_src_dir, 'collect'] 62 tool.config = False 63 tool.run() 64 pot_content = open(os.path.join(locale_dir, "testmessages.pot")).read() 65 assert "Some text to be i18n'ed" in pot_content 66 assert "kid expression in attribute" in pot_content 67 assert not "normal attribute text" in pot_content 68 assert not "This is english, and it shouldn't be collected" in pot_content 69 assert not "Dies ist Deutsch, und es sollte nicht aufgesammelt werden" in pot_content 70