Back to index

plone3  3.1.7
convertToLocales.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """Converts a i18n layout to a locales layout
00003 
00004 Author: Christian Heimes
00005 License: ZPL 2.1
00006 """
00007 import os
00008 import os.path
00009 import re
00010 import glob
00011 
00012 RE_DOMAIN = re.compile(r"\"Domain: ?([a-zA-Z-_]*)\\n\"")
00013 RE_LANGUAGE = re.compile(r"\"Language-[cC]ode: ?([a-zA-Z-_]*)\\n\"")
00014 
00015 base = '.'
00016 i18n = os.path.join(base, 'i18n')
00017 locales = os.path.join(base, 'locales')
00018 po_files = glob.glob(os.path.join(i18n, '*.po'))
00019 pot_files = glob.glob(os.path.join(i18n, '*.pot'))
00020 
00021 def getLocalsPath(lang, domain):
00022     po = '%s.po' % domain
00023     path = os.path.join(locales, lang, 'LC_MESSAGES')
00024     return path, po
00025 
00026 def svnAdd(path):
00027     path = path.split(os.sep)
00028     for l in range(len(path)):
00029         l+=1
00030         p = os.path.join(*path[:l])
00031         if not os.path.isdir(os.path.join(p, '.svn')):
00032             os.system("svn add %s" % p)
00033 
00034 for po in po_files:
00035     fd = open(po, 'r')
00036     header = fd.read(5000) # 5,000 bytes should be fine 
00037     fd.close()
00038  
00039     domain = RE_DOMAIN.findall(header)
00040     lang = RE_LANGUAGE.findall(header)
00041     if domain and lang:
00042         domain = domain[0]
00043         lang = lang[0]
00044     else:
00045         print "Failed to get metadata for %s" % po
00046         continue
00047 
00048     po_path, new_po = getLocalsPath(lang, domain)
00049     if not os.path.isdir(po_path):
00050         os.makedirs(po_path)
00051 
00052     src = po
00053     dst = os.path.join(po_path, new_po)
00054     svnAdd(po_path)
00055     os.system("svn mv %s %s" % (src, dst))
00056     print "Copied %s - %s" % (po_path, new_po)
00057 
00058 for pot in pot_files:
00059     os.system("svn mv %s %s" % (pot, locales))
00060     print "Copied %s" % pot
00061