358.14.1
by Matt Layman
Moved my copyright to Entertainer Developers. |
1 |
# Copyright (c) 2009 Entertainer Developers - See COPYING - GPLv2
|
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
2 |
'''Generate translations related files, pot/po/mo'''
|
3 |
||
317.3.3
by Matt Layman
Added update pos and mos. Removed the whole locale directly because it only needs to be generated when mos are created. locale needs to be removed because when the mo files are finally removed, lintian will complain about empty directories. |
4 |
import glob |
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
5 |
import os |
6 |
import re |
|
7 |
from subprocess import call |
|
317.3.3
by Matt Layman
Added update pos and mos. Removed the whole locale directly because it only needs to be generated when mos are created. locale needs to be removed because when the mo files are finally removed, lintian will complain about empty directories. |
8 |
import sys |
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
9 |
|
10 |
class TranslationsGenerator(object): |
|
11 |
'''Translation file generator'''
|
|
12 |
||
13 |
def __init__(self, lib_dir = '../entertainerlib', pot_dir = 'po', |
|
14 |
po_dir = 'po', mo_dir = '.', exclude_dir_pattern = 'tests'): |
|
15 |
||
16 |
# Directory of python library files to search for for translations
|
|
17 |
self.lib_dir = lib_dir |
|
18 |
||
19 |
# A directory pattern to exclude when searching
|
|
20 |
self.exclude = exclude_dir_pattern |
|
21 |
||
22 |
# Directory where pot file is stored
|
|
23 |
self.pot_dir = pot_dir |
|
24 |
||
25 |
# Directory where po files are stored
|
|
26 |
self.po_dir = po_dir |
|
27 |
||
28 |
# Directory to output mo files and related directories
|
|
29 |
self.mo_dir = mo_dir |
|
30 |
||
31 |
def update_pot(self, pot_file = 'entertainer.pot'): |
|
32 |
'''Update the pot file
|
|
33 |
||
34 |
If the pot file does not exist, create one.
|
|
35 |
'''
|
|
36 |
||
37 |
def generate_header(glade_file): |
|
38 |
'''Create a header file from the glade file with intltool-extract'''
|
|
39 |
||
40 |
call(['intltool-extract', '--type=gettext/glade', glade_file]) |
|
41 |
||
42 |
return '%s.h' % glade_file |
|
43 |
||
44 |
# List of file names to feed to xgettext
|
|
45 |
files_to_translate = [] |
|
46 |
# Store header file names to clean up after pot generation is complete
|
|
47 |
headers = [] |
|
48 |
||
49 |
# Cycle through all the files and collect the necessary data
|
|
341.1.4
by Paul Hummer
Fixed unused variable issues |
50 |
# pylint: disable-msg=W0612
|
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
51 |
for root, dirs, files in os.walk(self.lib_dir): |
52 |
if self.exclude in root: |
|
53 |
continue
|
|
341.1.7
by Paul Hummer
All variables have been updated to not step on builtins |
54 |
for filename in files: |
55 |
full_path = os.path.join(root, filename) |
|
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
56 |
|
341.1.4
by Paul Hummer
Fixed unused variable issues |
57 |
ext = os.path.splitext(full_path)[1] |
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
58 |
if ext == '.py': |
59 |
files_to_translate.append(full_path) |
|
60 |
elif ext == '.glade': |
|
61 |
header = generate_header(full_path) |
|
62 |
files_to_translate.append(header) |
|
63 |
headers.append(header) |
|
64 |
||
65 |
# Generate pot file
|
|
66 |
tmp = 'tmp.pot' |
|
67 |
command = ['xgettext', '--language=Python', '--keyword=_', |
|
68 |
'--keyword=N_', '--output=%s' % tmp] |
|
69 |
command.extend(files_to_translate) |
|
70 |
call(command) |
|
71 |
||
72 |
# Replace CHARSET with UTF-8 and write to final pot filename
|
|
73 |
pot_path = os.path.join(self.pot_dir, pot_file) |
|
74 |
out = open(pot_path, 'w') |
|
75 |
data = open(tmp).read() |
|
76 |
out.write(re.sub('charset=CHARSET', 'charset=UTF-8', data)) |
|
77 |
out.close() |
|
78 |
||
79 |
# Remove unnecessary files
|
|
80 |
for header in headers: |
|
81 |
os.remove(header) |
|
82 |
os.remove(tmp) |
|
83 |
||
317.3.3
by Matt Layman
Added update pos and mos. Removed the whole locale directly because it only needs to be generated when mos are created. locale needs to be removed because when the mo files are finally removed, lintian will complain about empty directories. |
84 |
def update_pos(self, pot_file = 'entertainer.pot'): |
85 |
'''Update all po files with the data in the pot reference file'''
|
|
86 |
||
87 |
pot_path = os.path.join(self.pot_dir, pot_file) |
|
88 |
||
89 |
pos = glob.glob(os.path.join(self.po_dir, '*.po')) |
|
90 |
for po in pos: |
|
91 |
call(['msgmerge', '-U', po, pot_path]) |
|
92 |
||
93 |
def update_mos(self, search_pattern = 'entertainer-(.*)\.po', |
|
94 |
mo_name = 'entertainer'): |
|
95 |
'''Generate mo files for all po files
|
|
96 |
||
97 |
Search pattern is the pattern that all the po files are in. The saved
|
|
98 |
value is the locale that is pulled out of the po filename.
|
|
99 |
'''
|
|
100 |
||
101 |
pos = glob.glob(os.path.join(self.po_dir, '*.po')) |
|
102 |
for po in pos: |
|
103 |
match = re.search(search_pattern, po) |
|
104 |
lang = match.group(1) |
|
105 |
lang_dir = '../locale/%s/LC_MESSAGES' % lang |
|
106 |
||
107 |
# Create the directory and all its intermediates if needed
|
|
108 |
if not os.path.exists(lang_dir): |
|
109 |
os.makedirs(lang_dir) |
|
110 |
||
111 |
# Create the mo file from the po file
|
|
112 |
mo_out = os.path.join(lang_dir, '%s.mo' % mo_name) |
|
113 |
call(['msgfmt', po, '-o', mo_out]) |
|
114 |
||
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
115 |
if __name__ == '__main__': |
116 |
translation_generator = TranslationsGenerator() |
|
317.3.3
by Matt Layman
Added update pos and mos. Removed the whole locale directly because it only needs to be generated when mos are created. locale needs to be removed because when the mo files are finally removed, lintian will complain about empty directories. |
117 |
|
118 |
if 'pot' in sys.argv: |
|
119 |
translation_generator.update_pot() |
|
120 |
if 'po' in sys.argv: |
|
121 |
translation_generator.update_pos() |
|
122 |
if 'mo' in sys.argv: |
|
123 |
translation_generator.update_mos() |
|
317.3.2
by Matt Layman
Replaced the pot shell script with a python script. |
124 |