summaryrefslogtreecommitdiffstats
path: root/web/utils/translation_tool
blob: 6967139ac30fea68c0937d88afc4a50d1d308acb (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#! /usr/bin/python -O
# -*- coding: iso-8859-1 -*-

# this script iterates through the 'html' and 'lib' directories
# looking for php scripts that contain a include_once("xxx_po.inc")
# line and __() functions.  It creates/appends to the corresponding
# "xxx_po.inc" file in the 'lang' subdirectory and places the
# i18n strings into the file in the proper format.
#
# usage: genpopo [-v] [-f]
#        -v: verbose, print duplicate terms that could be moved to common_po
#        -f: force, overwrite existing translated files, otherwise append
#

translator_name=raw_input("What is your full name? ")
translator_email=raw_input("What is your email address? ")
trans_native=raw_input("What is the native name of the language? ")
trans_eng=raw_input("What is the English name of the language? ")
trans_abbrv=raw_input("What is the ISO 639-1 Alpha-2 abbreviation for the language? ")
if len(trans_abbrv) != 2:
	print "Must use 2 character abbreviation"
	raise SystemExit


INC_HEADER = """\
<?php
# %s (%s) translation
# Translator: %s <%s>

include_once("translator.inc");
global $_t;
""" % (trans_eng, trans_native, translator_name, translator_email)


import sys
print_dupes = '-v' in sys.argv
force = '-f' in sys.argv

import re, os
up = re.compile('_\(\s*"(([^"]|(?<=\\\\)["])+)"')

lang = { 'common_po.inc': {} }

current_dir = os.getcwd()

# Find the common_po.inc file.
#
common = {}
for dir in ['../lang', 'lang']:
	if os.path.exists(dir):
		os.chdir(dir)
		if os.path.exists('common_po.list'):
			f = open('common_po.list','r')
			lines = f.readlines()
			f.close()
			for line in lines:
				if line[0] != '#':
					common[line[:-1]] = 0
					lang['common_po.inc'][line[:-1]] = 1
			os.chdir(current_dir)
			break
		os.chdir(current_dir)
else:
	print "Can't find common_po.list file."
	raise SystemExit

#Find the lang directory
for dir in ['../lang', 'lang']:
	if os.path.exists(dir):
		lang_dir = dir
		break
else:
	print "Can't find the lang directory."
	raise SystemExit

trans_dir = trans_abbrv
if not os.path.exists(os.path.join(lang_dir,trans_dir)):
	os.mkdir(os.path.join(lang_dir,trans_dir))


# Iterate through various places where the php files might be.
#
for dir in ['../html', '../lib', 'html', 'lib']:

	if os.path.exists(dir):
		# Find all the PHP files in the current directory.
		#
		files = [x for x in os.listdir(dir)
					if (x[-4:] == '.inc' and x[-7:] != '_po.inc')
					or x[-6:] == '.class'
					or x[-4:] == '.php'
					or x[-6:] == '.phtml'
				]
		os.chdir(dir)

		for file in files:
			f = open(file,'r')
			lines = f.readlines()
			f.close()

			# Is this file one we need to parse for internationalized strings?
			#
			parse_file = 0
			for line in lines:
				match = re.search("include(_once|)\s*\(\s*[\"']([A-Za-z_]+_po.inc)[\"']\s*\);",line)
				if match and match.group(2) != "common_po.inc":
					po = match.group(2)
					if not lang.has_key(po):
						lang[po] = {}
					parse_file = 1
					break

			# If we need to parse the file, do so.
			#
			if parse_file:
				print "Parsing %s..." % file
				for line in lines:
					match = up.search(line)
					while match:
						term = match.group(1).replace('\\"','"')
						if common.has_key(term):
							common[term] += 1
						else:
							if print_dupes:
								for key in lang.keys():
									if key != po and lang[key].has_key(term):
										print "...Duplicate term: \"%s\" is also in %s." % (term,key)
							lang[po][term] = 1
						line = line[match.end(1):]
						match = up.search(line)

		os.chdir(current_dir)

# Now generate all the .inc files if they don't already exist.
# if they do exist, only append new stuff to the end.  If the 'force'
# option is passed, just overwrite the entire thing.
#

print """
INSTRUCTIONS:
****************************************************
You will now be prompted for all needed translations.
Please translate the requested lines, hitting [enter]
goes to the next one. You may stop at any time using
ctrl+c, and pick up where you left off by running
translation_tool again.

If there are escapes in the original English, you may
need to include them in your translation. The
following is a list of escapes and what they do:
%h - HTML code inserted at run-time
%s - Nontranslated string inserted at run-time (such as username)
\\" - A double quote (")

When you have finished your translation, make a tarball
of the lang/ directory and send it to Simo (simo@neotuli.net)
for inclusion in the AUR.
By submitting a translation, you are implying that you
are also willing to maintain it. When there are
new strings to be translated, you will be contacted.
****************************************************
"""

os.chdir(lang_dir)
if force:
	# just going to overwrite any existing files
	# NOT RECOMMENDED! OVERWRITES ALL OTHER LANGUAGE SUPPORT
	#
	for po in lang.keys():
		print "Generating %s..." % po

		f = open(po,'w')
		f.write(INC_HEADER)
		f.write('\ninclude_once(\"en/%s\");\n' % po)
		f.write('\n')
		f.close()

		f = open(trans_dir+"/"+po,'w')
		f.write(INC_HEADER)

		for term in lang[po].keys():
			f.write("\n")
			trans = raw_input(term+"  = ")
			f.write('$_t["%s"]["%s"] = "%s";\n' % (trans_abbrv, term, trans))
		f.write("\n");
		f.close()
else:
	# need to leave existing file intact, and only append on terms that are new
	#
	incre = re.compile('^include_once\("%s\/(.*)"\);' % trans_abbrv)
	mapre = re.compile('^\$_t\["%s"\]\["(.*)"\].*$' % trans_abbrv)
	for po in lang.keys():
		got_match = False
		print "Updating %s..." % po
		try:
			f = open(po, 'r')
			new_file = 0
		except:
			new_file = 1
		if not new_file:
			contents = f.readlines()
			f.close()
			# strip off beginning/ending empty lines
			#
			while contents[0] == '':
				del contents[0]
			while contents[-1] in ['', "\n", "?>", "?>\n", "\n?>"]:
				del contents[-1]
						
			for line in contents:
				match = incre.search(line)
				if match:
					got_match = True
			if not got_match:
				f = open(po,'w')
				f.write("".join(contents))
				f.write('\ninclude_once(\"%s/%s\");\n' % (trans_abbrv, po))
				f.write("\n");
				f.close()
		else:
			f = open(po,'w')
			f.write(INC_HEADER)
			f.write('\ninclude_once(\"%s/%s\");\n' % (trans_abbrv, po))
			f.write('\n')
			f.close()
		# first read in file contents so we can hash what already exists
		#
		try:
			f = open(trans_dir+"/"+po, 'r')
			new_file = 0
		except:
			new_file = 1

		existing_terms = []
		if not new_file:
			contents = f.readlines()
			f.close()

			# strip off beginning/ending empty lines
			#
			while contents[0] == '':
				del contents[0]
			while contents[-1] in ['', "\n", "?>", "?>\n", "\n?>"]:
				del contents[-1]

			# next, collect existing terms
			#
			for line in contents:
				match = mapre.search(line)
				if match:
					existing_terms.append(match.group(1))

		# now append any new terms to EOF
		#
		f = open(trans_dir+"/"+po, 'w')
		if not new_file:
			f.write("".join(contents))
		else:
			f.write(INC_HEADER)

		for term in lang[po].keys():
			if term not in existing_terms:
				f.write("\n");
				trans = raw_input(term+"  = ")
				f.write('$_t["%s"]["%s"] = "%s";\n' % (trans_abbrv, term, trans))
		f.write("\n");
		f.close()

# Print out warnings for unused and little-used common entries.
#
for key in common.keys():
	if common[key] == 1:
		print "Warning: common entry '%s' is only used once." % key
for key in common.keys():
	if common[key] == 0:
		print "Warning: unused common entry '%s'." % key

# vim: ts=2 sw=2 noet ft=python