Package elisa :: Package core :: Package utils :: Module i18n

Source Code for Module elisa.core.utils.i18n

  1  # -*- coding: utf-8 -*- 
  2  # Moovida - Home multimedia server 
  3  # Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Moovida with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Moovida is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Moovida" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  i18n related functions 
 19  """ 
 20   
 21  __maintainer__ = 'Philippe Normand <philippe@fluendo.com>' 
 22   
 23  from elisa.core import log 
 24  from elisa.extern import msgfmt 
 25  import os 
 26  import sys 
 27  import glob 
 28  import gettext 
 29  import locale 
 30  import pkg_resources 
 31  import platform 
 32   
33 -def compile_po_files(domain, directory, dest_directory, announce_func=None):
34 announce_func = announce_func or (lambda msg: sys.stdout.write("%s\n" % msg)) 35 36 announce_func("Compiling po files in %r directory for domain %s" % (directory, domain)) 37 38 package_path = os.path.dirname(dest_directory).replace(os.path.sep, '.') 39 basename = os.path.basename(dest_directory) 40 mo_files = {package_path: [os.path.join(basename, '*', 41 'LC_MESSAGES', '*.mo')]} 42 43 pattern = '%s/*.po' % directory 44 for filename in glob.glob(pattern): 45 announce_func("Trying to compile %r" % filename) 46 basename, ext = os.path.splitext(filename) 47 lang = os.path.basename(basename) 48 src = filename 49 destdir = os.path.join(dest_directory, lang, 'LC_MESSAGES') 50 dest = os.path.join(destdir, "%s.mo" % domain) 51 if not os.path.exists(dest): 52 if not os.path.exists(destdir): 53 try: 54 os.makedirs(destdir) 55 except OSError, e: 56 announce_func("Could not make directory %r: %r. " \ 57 "Skipped %s." % (destdir,e,dst)) 58 else: 59 announce_func("Compiling %s to %s" % (src, dest)) 60 msgfmt.make(src, dest) 61 62 else: 63 src_mtime = os.stat(src)[8] 64 dest_mtime = os.stat(dest)[8] 65 if src_mtime > dest_mtime: 66 announce_func("Compiling %s to %s" % (src, dest)) 67 msgfmt.make(src, dest) 68 else: 69 announce_func("Skipping already compiled %r" % dest) 70 71 return mo_files
72
73 -def get_current_locale():
74 if platform.system().lower() == 'windows': 75 # windows hack for locale setting 76 default_lang, default_enc = locale.getdefaultlocale() 77 lang = os.getenv('LANG') 78 if lang is None: 79 lang = default_lang 80 os.environ['LANG'] = lang 81 current_locale = lang 82 else: 83 current_locale = locale.getdefaultlocale()[0] 84 if current_locale is None: 85 current_locale = os.getenv('LANG') 86 87 return current_locale
88
89 -def install_translation(plugin_name, plural = False):
90 log_category = 'i18n' 91 domain = 'elisa-plugin-%s' % plugin_name 92 i18n_dir = pkg_resources.resource_filename('elisa.plugins.%s' % plugin_name, 93 'i18n') 94 95 current_locale = get_current_locale() 96 try: 97 fallback_locale = current_locale.split('_')[0] 98 t = gettext.translation(domain, i18n_dir, languages=[current_locale, fallback_locale]) 99 translator = t.ugettext 100 translator_plural = t.ungettext 101 except (AttributeError, IOError), error: 102 # if the locale couldn't be determined or there's no translation for it 103 translator = lambda string: string 104 translator_plural = lambda singular, plural, num: num == 1 and singular or plural 105 log.info(log_category,"Translation not found for locale %r in plugin %r", 106 current_locale, plugin_name) 107 else: 108 log.info(log_category, "Loaded translation for locale %r in plugin %r", 109 current_locale, plugin_name) 110 if plural: 111 return translator, translator_plural 112 else: 113 return translator
114