Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Find false use of variables in Drupal .po file

By: a guest on Sep 20th, 2010  |  syntax: Python  |  size: 0.84 KB  |  views: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. # encoding: utf-8
  3.  
  4. import re, sys, polib
  5.  
  6. if len(sys.argv)==1:
  7.     print 'Usage: filename'
  8.     sys.exit(0)
  9. else:
  10.     filename = sys.argv[1]
  11.  
  12. # Open file
  13. po = polib.pofile(filename)
  14.  
  15. for e in po:
  16.     msgid = getattr(e, 'msgid')
  17.     msgstr = getattr(e, 'msgstr')
  18.    
  19.     if msgstr != "" and re.search('(?P<varname>[@%!][a-z_-]{1,}).', msgid):
  20.         # Found a possible variable
  21.         match = re.findall('(?P<varname>[@%!][a-z_-]{1,})', msgid)
  22.         for i in match:
  23.             if re.search('%s[^a-z]|%s$'%(i,i),msgstr):
  24.                 #print "%s is correct" % (i)
  25.                 pass
  26.             else:
  27.                 print ""
  28.                 print 'Variable %s has been changed' % (i)
  29.                 print u"msgid  → %s" % (msgid)
  30.                 print u"msgstr → %s" % (msgstr)
  31.                 print ""