4bd037c682fa4a842866c257738e305dc789f61e
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

1) #!/bin/bash
2) #
3) # Author: Runa Sandvik, <runa.sandvik@gmail.com>
4) # Google Summer of Code 2009
5) #
6) # This is Free Software (GPLv3)
7) # http://www.gnu.org/licenses/gpl-3.0.txt
8) #
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

9) # This script will convert all of the english wml files to po files, and
10) # keep them updated. The script will also convert subdirectories that
11) # exist in the english website module.
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

12) #
13) # For more information, see the HOWTO and README in
14) # translation/tools/gsoc09.
15) # 
16) 
17) ### start config ###
18) 
19) # Location of the wml files
20) wmldir="$PWD"
21) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

22) # Location of the po files.
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

23) # Assuming that the translation directory is relative to the website
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

24) podir="`dirname $wmldir`/translation/projects/website"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

25) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

26) # Set the copyright holder of the po files,
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

27) # for example "The Tor Project, Inc"
28) copyright="The Tor Project, Inc"
29) 
30) # A lot of the wml files have custom tags. These tags have been defined
31) # in website/include/versions.wmi. Tags that people usually forget to close,
32) # as well as tags that are not defined in versions.wmi have been added.
33) # See: https://svn.torproject.org/svn/website/trunk/include/versions.wmi
Steven Murdoch Add a link tag for svnproje...

Steven Murdoch authored 14 years ago

34) customtag=`echo $(cat "$wmldir/include/versions.wmi" | awk '{ printf "<%s> " , $2 }' | sed 's/<>//g') "<svnsandbox> <svnwebsite> <svnprojects> <input> <hr> <br> <img> <gitblob> <version-android-components>"`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

35) 
36) # We also need to use the nodefault option of po4a; space separated list
37) # of tags that the module should not try to set by default in any
38) # category. For now, we only need the input tag.
39) nodefault='<input>'
40) 
41) # The script can write the name of unprocessed files to a log.
42) # If you want to enable this option, set the logfile here.
Runa A. Sandvik updated translations for th...

Runa A. Sandvik authored 14 years ago

43) logfile="/home/runa/tor/wml2po.log"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

44) 
45) # This is the temp logfile. Leave this line even if you don't want to
46) # log. This will be deleted when the script is done.
47) tmplog="`dirname $wmldir`/tmp.log"
48) 
49) ### end config ###
50) 
51) # Create a lockfile to make sure that only one instance of the script
52) # can run at any time.
53) LOCKFILE=wml2po.lock
54) 
55) if lockfile -! -l 60 -r 3 "$LOCKFILE"; 
56) then
57) 	echo "unable to acquire lock" >2
58) 	exit 1
59) fi
60) 
61) trap "rm -f '$PWD/$LOCKFILE'" exit
62) 
63) # Check if translation/projects/website exist, i.e. has been checked out
64) if [ ! -d $podir ]
65) then
66) 	echo "Have you remembered to check out translation/projects/website?"
67) 	exit 1
68) fi
69) 
70) # If the logfile is set, write the date.
71) if [ $logfile ]
72) then
73) 	echo `date` > $logfile
74) fi
75) 
76) # Create the temp log
77) touch $tmplog
78) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

79) # We need to find out which language directories we have.
80) # We also need to excluse the website module directory itself, as well
81) # as .svn
82) langdir=`find "$podir" -maxdepth 1 -type d ! -path "$podir" ! -path "$podir/templates" ! -path "*\.*" | sed "s#$podir/##"`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

83) 
84) # We only need the english wml files, but we do not wish to translate
85) # the eff documents.
86) wml=`find $wmldir -regex '^'$wmldir'/.*en/.*\.wml' -type f | grep -v '^'$wmldir'/eff'`
87) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

88) # For every language directory, create and/or update the po files.
89) for lang in $langdir ; do
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

90) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

91) 	# For every english wml, see if the po needs to be created or
92) 	# updated
93) 	for file in $wml ; do
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

94) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

95) 		# Get the basename of the file we are dealing with
96) 		wmlfile=`basename $file`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

97) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

98) 		# Get the translation priority
99) 		priority=`cat $file | grep "# Translation-Priority" | awk '{print $3}'`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

100) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

101) 		# If the file doesn't have a translation-priority, we can assume
102) 		# that it doesn't need to be translated. Skip this file and
103) 		# continue on with the next.
104) 		if [ ! $priority ]
105) 		then
106) 			continue
107) 		fi
108) 
109) 		# Strip the file for its original extension and add .po
110) 		pofile="$priority.${wmlfile%%.*}.po"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

111) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

112) 		# Find out what directory the file is in.
113) 		# Also, remove the parth of the path that is $wmldir
114) 		indir=`dirname $file`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

115) 	
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

116) 		# We need to know what one dir up is
117) 		onedirup=`dirname $indir | sed "s#$wmldir/##"`
118) 
119) 		# We need to have the correct, full path to the po
120) 		# directory for the file we are working on.
121) 		# Also, did the subdirectory exist prior to running this
122) 		# script? If not, create it now and add it to the
123) 		# repository.
124) 		if [ $onedirup = $wmldir ]
125) 		then
126) 			popath="$podir/$lang"
127) 		else
128) 
129) 			# We need to know if a subdirectory, such as torbutton,
130) 			# exist in the translation module. If it does not exist,
131) 			# the script will create it in all the directories under
132) 			# translation/projects/website (excluding .svn)
133) 			subdir=`find "$podir/$lang" -maxdepth 1 -type d	! -path "$ppodir/$lang" ! -path "*\.*"`
134) 
135) 			for dir in $subdir ; do
136) 				if [ ! -d "$podir/$lang/$onedirup" ]
137) 				then
138) 					svn mkdir "$podir/$lang/$onedirup"
139) 				fi
140) 			done
Runa A. Sandvik if the subdirectory does no...

Runa A. Sandvik authored 15 years ago

141) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

142) 			# Set the path
143) 			popath="$podir/$lang/$onedirup"
144) 
145) 		fi
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

146) 		
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

147) 		# Check to see if the po existed prior to running this
148) 		# script. If it didn't, check if there any files with the same
149) 		# filename, but different priority. If neither of the files
150) 		# exist, create with po4a-gettextize.
151) 		if [ -e "$popath/$pofile" ]
152) 		then
153) 			poexist=1
154) 		elif [ `find $popath -type f -name "*.$filename" | wc -l` -gt "0" ]
155) 		then
156) 			poexist=2
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

157) 
158) 		# We need to rename the other file
159) 		for file in `find $popath -type f -name "*.$filename"` ; do
160) 			svn mv "$file" "$popath/$pofile"
161) 			echo "$popath/$pofile" > $tmplog
162) 		done
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

163) 	
164) 		else
165) 			poexist=0
166) 		fi
167) 
168) 		# If the po file does not exist, convert it with
169) 		# po4a-gettextize, set the right encoding and charset
170) 		# and the correct copyright.
171) 		if [ $poexist = 0 ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

172) 		then
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

173) 			# Convert it
174) 			po4a-gettextize -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

175) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

176) 			# Check to see if the file exists
177) 			if [ -e "$popath/$pofile" ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

178) 			then
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

179) 				# We don't want files without
180) 				# content, so check the file first.
181) 				content=`cat "$popath/$pofile" | grep '^#[.]' | wc -l`
182) 
183) 				# If the file does not have any
184) 				# content, delete it.
185) 				if [ $content = 0 ] 
186) 				then
187) 					rm -f "$popath/$pofile"
188) 					echo "$popath/$pofile" > $tmplog
189) 				else
190) 					# Set the right encoding and charset, as well
191) 					# as the correct copyright holder.
192) 					sed -i '0,/ENCODING/ s/ENCODING/8bit/' "$popath/$pofile"
193) 					sed -i '0,/CHARSET/ s/CHARSET/utf-8/' "$popath/$pofile"
194) 					sed -i "0,/Free Software Foundation, Inc/ s/Free Software Foundation, Inc/$copyright/" "$popath/$pofile"
195) 				
196) 					# And add it to the repository
197) 					svn add "$popath/$pofile"
198) 					echo "$popath/$pofile" > $tmplog
199) 				fi
200) 			fi
201) 
202) 			# Update the file with po4a-updatepo to make the
203) 			# word wrapping perfect
204) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
205) 
206) 			# Delete the backup
207) 			rm -f "$popath/$pofile~"
208) 		fi
209) 		
210) 		# If the po file does exist, calculate the hash first,
211) 		# then update the file, then calculate the hash again.
212) 		if [ $poexist = 1 ]
213) 		then
214) 			# Calculate the hash before we update the file
215) 			before=`grep -vE '^("POT-Creation-Date:|#)' "$popath/$pofile" | md5sum | cut -d " " -f1`
216) 
217) 			# Update the po file
218) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
219) 
220) 			# Calculate the new hash
221) 			after=`grep -vE '^("POT-Creation-Date:|#)' "$popath/$pofile" | md5sum | cut -d " " -f1`
222) 
223) 			# Delete the backup
224) 			rm -f "$popath/$pofile~"
225) 
226) 			# Now we need to compare the before and after
227) 			# hash. If they match (i.e. nothing has
228) 			# changed), revert the file.
229) 			if [ $before = $after ]
230) 			then
231) 				svn revert "$popath/$pofile"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

232) 				echo "$popath/$pofile" > $tmplog
233) 			else
234) 				echo "$popath/$pofile" > $tmplog
235) 			fi
236) 		fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

237) 
238) 		# If a file with the same name but different priority
239) 		# exist, then rename the file (we have done so already)
240) 		# and update it with po4a-updatepo to make sure
241) 		# everything else is ok.
242) 		if [ $poexist = 2 ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

243) 		then
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

244) 			# Update the file
245) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

246) 		fi
247) 	
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

248) 		# Write to the logfile
249) 		if [ -e $logfile ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

250) 		then
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

251) 			if [ `cat $tmplog | grep "$popath/$pofile" | wc -l` -eq "0" ]
252) 			then
253) 				echo "could not process: " "$file" >> $logfile
254) 			fi
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

255) 		fi
256) 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

257) 		# Delete the temp log
258) 		rm -f $tmplog
259) 	done