forked from spyder-ide/spyder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettext_helpers.py
112 lines (101 loc) · 3.97 KB
/
gettext_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 CEA
# Pierre Raybaut
# Licensed under the terms of the CECILL License
# (see guidata/__init__.py for details)
from __future__ import print_function
import sys
import os
import os.path as osp
import subprocess
if os.name == 'nt':
# Find pygettext.py source on a windows install
pygettext = ['python',
osp.join(sys.prefix, "Tools", "i18n", "pygettext.py")]
msgfmt = ['python', osp.join(sys.prefix, "Tools", "i18n", "msgfmt.py")]
else:
pygettext = ['pygettext']
msgfmt = ['msgfmt']
def get_files(modname):
if not osp.isdir(modname):
return [modname]
files = []
for dirname, _dirnames, filenames in os.walk(modname):
files += [ osp.join(dirname, f)
for f in filenames if f.endswith(".py") or f.endswith(".pyw") ]
for dirname, _dirnames, filenames in os.walk("tests"):
files += [ osp.join(dirname, f)
for f in filenames if f.endswith(".py") or f.endswith(".pyw") ]
return files
def get_lang( modname ):
localedir = osp.join( modname, "locale")
for _dirname, dirnames, _filenames in os.walk(localedir):
break # we just want the list of first level directories
return dirnames
def do_rescan(modname):
files = get_files(modname)
dirname = modname
do_rescan_files(files, modname, dirname)
def do_rescan_files(files, modname, dirname):
localedir = osp.join(dirname, "locale")
potfile = modname+".pot"
subprocess.call(pygettext+[
##"-D", # Extract docstrings
"-o", potfile, # Nom du fichier pot
"-p", localedir, # dest
]+files)
for lang in get_lang(dirname):
pofilepath = osp.join(localedir, lang, "LC_MESSAGES", modname+".po")
potfilepath = osp.join(localedir, potfile)
print("Updating...", pofilepath)
if not osp.exists( osp.join(localedir, lang, "LC_MESSAGES") ):
os.mkdir( osp.join(localedir, lang, "LC_MESSAGES") )
if not osp.exists( pofilepath ):
outf = open(pofilepath, "w")
outf.write("# -*- coding: utf-8 -*-\n")
data = open( potfilepath ).read()
data = data.replace("charset=CHARSET", "charset=utf-8")
data = data.replace("Content-Transfer-Encoding: ENCODING",
"Content-Transfer-Encoding: utf-8")
outf.write(data)
else:
print("merge...")
subprocess.call( ["msgmerge", "-o",
pofilepath, pofilepath, potfilepath] )
def do_compile(modname, dirname=None):
if dirname is None:
dirname = modname
localedir = osp.join(dirname, "locale")
for lang in get_lang(dirname):
pofilepath = osp.join(localedir, lang, "LC_MESSAGES", modname+".po")
subprocess.call( msgfmt+[pofilepath] )
def main( modname ):
if len(sys.argv)<2:
cmd = "help"
else:
cmd = sys.argv[1]
# lang = get_lang( modname )
if cmd=="help":
print("Available commands:")
print(" help : this message")
print(" help_gettext : pygettext --help")
print(" help_msgfmt : msgfmt --help")
print(" scan : rescan .py files and updates existing .po files")
print(" compile : recompile .po files")
print()
print("Pour fonctionner ce programme doit être lancé depuis")
print("la racine du module")
print("Traductions disponibles:")
for i in get_lang(modname):
print(i)
elif cmd=="help_gettext":
subprocess.call( pygettext+["--help"] )
elif cmd=="help_msgfmt":
subprocess.call( msgfmt+["--help"] )
elif cmd=="scan":
print("Updating pot files")
do_rescan( modname )
elif cmd=="compile":
print("Builtin .mo files")
do_compile( modname )