summaryrefslogtreecommitdiff
path: root/web/lang/genpopo
blob: 66bf9a12b30b1e1729745295469cfa4ff4610807 (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
#! /usr/bin/python -O
# -*- coding: iso-8859-1 -*-

# This script iterates through the script directories
# looking for php scripts that contain  __() functions.
# It creates/appends to the corresponding
# "xxx.po" 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
#

import re
import os
import sys

INC_HEADER = """\
<?php
# INSTRUCTIONS TO TRANSLATORS
#
# This file contains the i18n translations for a subset of the
# Arch Linux User Community Repository (AUR).  This is a PHP
# script, and as such, you MUST pay great attention to the syntax.
# If your text contains any double-quotes ("), you MUST escape
# them with a backslash (\).
#

global $_t;
"""

language = 'en'
lang = {}

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

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

scriptdirs = ['html', 'lib', 'template']
pofile = '%s.po' % language

current_dir = os.getcwd()

# Iterate through various places where the php files might be.
#
for dir in scriptdirs:
	dir = "../%s" % dir

	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')
				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()

			# Parse the file
			print "Parsing %s..." % file
			for line in lines:
				match = up.search(line)
				while match:
					term = match.group(1).replace('\\"','"')
					if print_dupes:
						if term in lang.keys():
							print 'Multiple use of "%s"' % term

					lang[term] = 1
					line = line[match.end(1):]
					match = up.search(line)

		os.chdir(current_dir)

# Generate the .po file if it doesn't already exist.
# If it does exist, only append new stuff to the end.
# If the 'force' option is passed, just overwrite.

if force:
	# Just overwrite any existing files
	print "Generating %s..." % pofile
	
	f = open(pofile,'w')
	f.write(INC_HEADER)

	for term in lang.keys():
		f.write("\n")
		f.write('$_t["%s"]\n  = "%s";\n' % (term, term))

	f.write("\n");
	f.close()
else:
	# Need to leave existing file intact. Only append on new terms.
	mapre = re.compile('^\$_t\["(.*)"\].*$')
	got_match = False

	print "Updating %s..." % pofile

	try:
		f = open(pofile, 'r')
		new_file = 0
	except:
		new_file = 1

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

		# Strip beginning/ending empty lines
		while contents[0] == '':
			del contents[0]
		while contents[-1] in ['', "\n", "?>", "?>\n", "\n?>"]:
			del contents[-1]
			
		f = open(pofile,'w')
		f.write("".join(contents))
		f.write("\n");
		f.close()
	else:
		f = open(pofile,'w')
		f.write(INC_HEADER)
		f.write('\n')
		f.close()

	# Read file contents so we can hash what already exists
	try:
		f = open(pofile, 'r')
		new_file = 0
	except:
		new_file = 1

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

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

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

	# Append any new terms to EOF
	f = open(pofile, 'w')
	if not new_file:
		f.write("".join(contents))
	else:
		f.write(INC_HEADER)

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