Back to index

plone3  3.1.7
relocate.py
Go to the documentation of this file.
00001 """
00002    Usage: relocate.py
00003 
00004    As we moved quite some messages between domains lately, this script
00005    automates this process, so it can be repeatedly and safely called on po
00006    files to preserve translations.
00007 
00008    Note that PYTHON and I18NDUDE must have been set as enviroment variables
00009    before calling this script
00010 """
00011 
00012 import os, sys
00013 from utils import getPoFiles, getLanguage
00014 
00015 from i18ndude import catalog
00016 
00017 __PYTHON = os.environ.get('PYTHON', 'python')
00018 __I18NDUDE = os.environ.get('I18NDUDE', 'i18ndude')
00019 
00020 def main():
00021     os.chdir('..')
00022     os.chdir('i18n')
00023 
00024     poFilesPlone = getPoFiles('plone')
00025     if not poFilesPlone:
00026         print 'No po-files were found for Plone.'
00027         sys.exit(2)
00028 
00029     poFilesATCT = getPoFiles('atcontenttypes')
00030     if not poFilesATCT:
00031         print 'No po-files were found for ATContentTypes.'
00032         sys.exit(3)
00033 
00034     # format: 'old-domain' : ['msgid1', 'msgid2', ...]
00035     relocated = {'plone' :
00036                   ['Changes saved.', 'Please correct the indicated errors.',
00037                    'Rotate 180', 'help_boolean_criteria_bool',
00038                    'help_criteria_field_name', 'help_custom_view',
00039                    'help_custom_view_fields', 'help_date_range_criteria_end',
00040                    'help_date_range_criteria_start', 'help_exclude_from_nav',
00041                    'help_limit_number', 'help_news_image', 'help_shortname',
00042                    'help_path_criteria_value', 'help_string_criteria_value',
00043                    'help_portal_type_criteria_value', 'help_url',
00044                    'label_body_text', 'label_boolean_criteria_bool',
00045                    'label_contact_email', 'label_contact_name',
00046                    'label_contact_phone', 'label_criteria_field_name',
00047                    'label_custom_view', 'label_custom_view_fields',
00048                    'label_date_range_criteria_end', 'label_event_announcement',
00049                    'label_date_range_criteria_start', 'label_event_attendees',
00050                    'label_event_end', 'label_event_location',
00051                    'label_event_start', 'label_event_type',
00052                    'label_exclude_from_nav', 'label_file', 'label_image',
00053                    'label_image_caption', 'label_inherit_criteria',
00054                    'label_item_count', 'label_limit_number', 'label_news_image',
00055                    'label_path_criteria_recurse', 'label_path_criteria_value',
00056                    'label_related_items', 'label_short_name',
00057                    'label_string_criteria_value', 'label_url',
00058                   ]
00059                 }
00060 
00061     # make sure we only try on languages for which both po files exist
00062     ploneLanguages = [getLanguage('plone', p) for p in poFilesPlone]
00063     atctLanguages = [getLanguage('atcontenttypes', p) for p in poFilesATCT]
00064 
00065     languages = [l for l in ploneLanguages if l in atctLanguages and l != 'en']
00066     changes = {'plone' : False, 'atcontenttypes' : False}
00067 
00068     for lang in languages:
00069         po_ctl = {}
00070         try:
00071             po_ctl['plone'] = catalog.MessageCatalog(filename='plone-%s.po' % lang)
00072         except IOError, e:
00073             print >> sys.stderr, 'I/O Error: %s' % e
00074         try:
00075             po_ctl['atcontenttypes'] = catalog.MessageCatalog(filename='atcontenttypes-%s.po' % lang)
00076         except IOError, e:
00077             print >> sys.stderr, 'I/O Error: %s' % e
00078 
00079         changes = {'plone' : False, 'atcontenttypes' : False}
00080 
00081         relocate_domain = {'plone' : 'atcontenttypes', 'atcontenttypes' : 'plone'}
00082 
00083         msgids = {}
00084         msgids['plone'] = po_ctl['plone'].keys()
00085         msgids['atcontenttypes'] = po_ctl['atcontenttypes'].keys()
00086 
00087         for old_domain in relocated:
00088             relocate_msgids = relocated.get(old_domain)
00089             for relocate_msgid in relocate_msgids:
00090                 if relocate_msgid in msgids[old_domain]:
00091                     msgstr = po_ctl[old_domain].get(relocate_msgid)
00092                     # We copy over messages for now
00093                     # del po_ctl[old_domain][relocate_msgid]
00094                     changes[old_domain] = True
00095 
00096                     new_domain = relocate_domain.get(old_domain)
00097                     if relocate_msgid in msgids[new_domain]:
00098                         old_msgstr = po_ctl[new_domain].get(relocate_msgid)
00099                         if old_msgstr.msgstr == '' and msgstr:
00100                             po_ctl[new_domain][relocate_msgid] = msgstr
00101                             changes[new_domain] = True
00102                             print 'copied msgstr for %s' % relocate_msgid
00103                         #else:
00104                         #    print '%s was already there' % relocate_msgid
00105                     else:
00106                         po_ctl[new_domain][relocate_msgid] = msgstr
00107                         changes[new_domain] = True
00108                         print 'copied %s to %s-%s.po' % (relocate_msgid, new_domain, lang)
00109                 #else:
00110                 #    print '%s was not found anymore' % relocate_msgid
00111 
00112         for domain in changes:
00113             if changes[domain]:
00114                 file = open('%s-%s.po' % (domain, lang), 'w')
00115                 writer = catalog.POWriter(file, po_ctl[domain])
00116                 writer.write(sort=True)
00117 
00118 if __name__ == '__main__':
00119     main()