Back to index

plone3  3.1.7
load.py
Go to the documentation of this file.
00001 import fnmatch
00002 import logging
00003 import os
00004 from os.path import isdir
00005 from os.path import join
00006 from stat import ST_MTIME
00007 
00008 from Products.PlacelessTranslationService.utils import log
00009 from Products.PlacelessTranslationService.msgfmt import Msgfmt
00010 from Products.PlacelessTranslationService.msgfmt import PoSyntaxError
00011 
00012 # Restrict languages
00013 PTS_LANGUAGES = None
00014 if bool(os.getenv('PTS_LANGUAGES')):
00015     langs = os.getenv('PTS_LANGUAGES')
00016     langs = langs.strip().replace(',', '').split()
00017     PTS_LANGUAGES = tuple(langs)
00018 
00019 
00020 def _checkLanguage(lang):
00021     if PTS_LANGUAGES is None:
00022         return True
00023     else:
00024         return bool(lang in PTS_LANGUAGES)
00025 
00026 
00027 def _updateMoFile(name, msgpath, lang, domain, mofile):
00028     """
00029     Creates or updates a mo file in the locales folder. Returns True if a
00030     new file was created.
00031     """
00032     pofile = join(msgpath, name)
00033     create = False
00034     update = False
00035 
00036     try:
00037         po_mtime = os.stat(pofile)[ST_MTIME]
00038     except (IOError, OSError):
00039         po_mtime = 0
00040 
00041     if os.path.exists(mofile):
00042         # Update mo file?
00043         try:
00044             mo_mtime = os.stat(mofile)[ST_MTIME]
00045         except (IOError, OSError):
00046             mo_mtime = 0
00047 
00048         if po_mtime > mo_mtime:
00049             # Update mo file
00050             update = True
00051         else:
00052             # Mo file is current
00053             return
00054     else:
00055         # Create mo file
00056         create = True
00057 
00058     if create or update:
00059         try:
00060             mo = Msgfmt(pofile, domain).getAsFile()
00061             fd = open(mofile, 'wb')
00062             fd.write(mo.read())
00063             fd.close()
00064 
00065         except (IOError, OSError, PoSyntaxError):
00066             log('Error while compiling %s' % pofile, logging.WARNING)
00067             return
00068 
00069         if create:
00070             return True
00071 
00072     return None
00073 
00074 def _compile_locales_dir(basepath):
00075     """
00076     Compiles all po files in a locales directory (Zope3 format) to mo files.
00077     Format:
00078         Products/MyProduct/locales/${lang}/LC_MESSAGES/${domain}.po
00079     Where ${lang} and ${domain} are the language and the domain of the po
00080     file (e.g. locales/de/LC_MESSAGES/plone.po)
00081     """
00082     basepath = str(os.path.normpath(basepath))
00083     if not isdir(basepath):
00084         return
00085 
00086     for lang in os.listdir(basepath):
00087         if not _checkLanguage(lang):
00088             continue
00089         langpath = join(basepath, lang)
00090         if not isdir(langpath):
00091             # it's not a directory
00092             continue
00093         msgpath = join(langpath, 'LC_MESSAGES')
00094         if not isdir(msgpath):
00095             # it doesn't contain a LC_MESSAGES directory
00096             continue
00097         names = fnmatch.filter(os.listdir(msgpath), '*.po')
00098         for name in names:
00099             domain = name[:-3]
00100             mofile = join(msgpath, name[:-2] + 'mo')
00101             result = _updateMoFile(name, msgpath, lang, domain, mofile)