Ticket #3955: compile-messages-use-settings-module.diff

File compile-messages-use-settings-module.diff, 3.0 KB (added by Ilya Semenov, 17 years ago)
  • django/bin/compile-messages.py

     
    44import os
    55import sys
    66
     7try:
     8    set
     9except NameError:
     10    from sets import Set as set     # For Python 2.3
     11
     12
    713def compile_messages(locale=None):
    8     basedir = None
    9 
    10     if os.path.isdir(os.path.join('conf', 'locale')):
    11         basedir = os.path.abspath(os.path.join('conf', 'locale'))
    12     elif os.path.isdir('locale'):
    13         basedir = os.path.abspath('locale')
    14     else:
    15         print "This script should be run from the Django SVN tree or your project or app tree."
     14    basedirs = [os.path.join('conf', 'locale'), 'locale']
     15    if os.environ.get('DJANGO_SETTINGS_MODULE'):
     16        from django.conf import settings
     17        basedirs += settings.LOCALE_PATHS
     18       
     19    # leave unique existent directories
     20    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))
     21   
     22    if not basedirs:
     23        print "This script should be run from the Django SVN tree or your project or app tree, or with the settings module specified."
    1624        sys.exit(1)
    1725
    18     if locale is not None:
    19         basedir = os.path.join(basedir, locale, 'LC_MESSAGES')
     26    for basedir in basedirs:
     27        if locale is not None:
     28            basedir = os.path.join(basedir, locale, 'LC_MESSAGES')
     29        compile_messages_in_dir(basedir)
    2030
     31def compile_messages_in_dir(basedir):
    2132    for dirpath, dirnames, filenames in os.walk(basedir):
    2233        for f in filenames:
    2334            if f.endswith('.po'):
     
    4051    parser = optparse.OptionParser()
    4152    parser.add_option('-l', '--locale', dest='locale',
    4253            help="The locale to process. Default is to process all.")
     54    parser.add_option('--settings',
     55        help='Python path to settings module, e.g. "myproject.settings". If provided, all LOCALE_PATHS will be processed. If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be checked as well.')
    4356    options, args = parser.parse_args()
    4457    if len(args):
    4558        parser.error("This program takes no arguments")
     59    if options.settings:
     60        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    4661    compile_messages(options.locale)
    4762
    4863if __name__ == "__main__":
  • docs/i18n.txt

     
    708708where either the ``conf/locale`` (in case of the source tree) or the ``locale/``
    709709(in case of app messages or project messages) directory are located. And you
    710710use the same ``compile-messages.py`` to produce the binary ``django.mo`` files that
    711 are used by ``gettext``.
     711are used by ``gettext``. You can also run ``compile-message.py
     712--settings=path.to.settings`` to make the compiler process all your ``LOCALE_PATHS``.
    712713
    713714Application message files are a bit complicated to discover -- they need the
    714715``LocaleMiddleware``. If you don't use the middleware, only the Django message
Back to Top