Package elisa :: Package core :: Package utils :: Module i18n
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.i18n

 1  # -*- coding: utf-8 -*- 
 2  # Elisa - Home multimedia server 
 3  # Copyright (C) 2006-2008 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 Elisa with Fluendo's plugins. 
11  # 
12  # The GPL part of Elisa is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Elisa" 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.extern import msgfmt 
24  from elisa.core import log 
25  import os 
26   
27  log_category = "po_compile" 
28   
29 -def compile_po_files(domain, directory, dest_directory):
30 log.info(log_category, "Compiling %s for %s to %s", directory, domain, 31 dest_directory) 32 33 package_path = os.path.dirname(dest_directory).replace(os.path.sep, '.') 34 basename = os.path.basename(dest_directory) 35 mo_files = {package_path: [os.path.join(basename, '*', 36 'LC_MESSAGES', '*.mo')]} 37 38 for filename in os.listdir(directory): 39 lang, ext = os.path.splitext(filename) 40 ext = ext[1:].lower() 41 if ext == 'po': 42 src = os.path.join(directory, filename) 43 destdir = os.path.join(dest_directory, lang, 'LC_MESSAGES') 44 dest = os.path.join(destdir, "%s.mo" % domain) 45 if not os.path.exists(dest): 46 if not os.path.exists(destdir): 47 try: 48 os.makedirs(destdir) 49 except OSError, e: 50 log.warning(log_category, 51 "Could not make directory %r: %r. " \ 52 "Skipped %s." % (destdir,e,dst)) 53 else: 54 log.info(log_category, "Compiling %s to %s", src, dest) 55 msgfmt.make(src, dest) 56 57 else: 58 src_mtime = os.stat(src)[8] 59 dest_mtime = os.stat(dest)[8] 60 if src_mtime > dest_mtime: 61 log.info(log_category, "Compiling %s to %s", src, dest) 62 msgfmt.make(src, dest) 63 else: 64 log.info(log_category, "Skipping already compiled %r", dest) 65 return mo_files 66
67 -def compile_translations_from_file(translations_file):
68 mo_files = {} 69 70 log.debug(log_category, "Compiling translations listed in %r", 71 translations_file) 72 73 for line in open(translations_file): 74 line = line.split() 75 if len(line) == 2: 76 domain = line[0] 77 directory = line[1] 78 output_directory = directory 79 elif len(line) == 3: 80 domain = line[0] 81 directory = line[1] 82 output_directory = line[2] 83 else: 84 continue 85 mo_files.update(compile_po_files(domain, directory, output_directory)) 86 87 return mo_files
88
89 -def compile_translations_from_files(files):
90 mo_files = {} 91 for filename in files: 92 mo_files.update(compile_translations_from_file(filename)) 93 return mo_files
94