tools/update_lua_locale_files.sh
author Wuzzy <Wuzzy2@mail.ru>
Mon, 12 Feb 2018 15:50:49 +0100
changeset 12980 263d006c56a9
parent 12715 f84849acda02
child 13090 3f3ad415d849
permissions -rwxr-xr-x
Battalion: Clean up and remove dead code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     1
#!/bin/sh -
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     2
# Script to update all Lua locale files.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     3
# It's Clunky and slow!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     4
# Note this script may sooner or later be phased out when we move to Gettext.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     5
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     6
# HOW TO USE:
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     7
# - Run this script in the tools/ directory.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     8
# - All .lua files in share/hedgewars/Data/Locale will be updated.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     9
# - Change LOCALEFILES below to limit the number of locale files to update
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    10
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    11
# Space-separated list of locale files to update, or *.lua for all.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    12
# (Note: always include stub.lua)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    13
LOCALEFILES="*.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    14
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    15
# List of all Lua files to scan:
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    16
# * Missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    17
# * Campaign missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    18
# * Lua libraries
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    19
# * Styles (aka multiplayer scripts)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    20
# * Mission maps
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    21
# IMPORTANT: Don't forget to update this list when new places for Lua
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    22
#            directories have been added!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    23
LUAFILES="../Missions/Challenge/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    24
 ../Missions/Scenario/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    25
 ../Missions/Training/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    26
 ../Missions/Campaign/*/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    27
 ../Scripts/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    28
 ../Scripts/Multiplayer/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    29
 ../Maps/*/map.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    30
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    31
cd ../share/hedgewars/Data/Locale;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    32
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    33
# Collect strings
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    34
echo "Step 1: Collect strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    35
echo -n "" > __temp_loc;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    36
for F in loc loc_noop;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    37
	do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    38
	grep -F "$F(\"" $LUAFILES | sed 's/")/")\n/g' | sed "s/.*$F(\"/loc(\"/;s/\").*/\")/" | grep loc | sort | uniq >> __temp_loc;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    39
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    40
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    41
# Update locale files
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    42
# This step is clunky and inefficient. Improve performance (if you are bored)!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    43
echo "Step 2: Update locale files (this may take a while)";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    44
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    45
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    46
	echo $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    47
	cat __temp_loc | while read f
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    48
		do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    49
		STR=$(echo "$f" | sed 's/loc("//;s/")\s*$//;s/"/\\"/g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    50
		MAPS=$(grep -F -l -- "loc(\"${STR}\")" $LUAFILES | sed 's/.*\/\([^\/]*\)\/map.lua/\1/;s/.*Campaign\/\([^\/]*\)\//\1:/;s/.*\///;s/.lua//;s/ /_/g' | xargs | sed 's/ /, /g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    51
		C=$(echo $MAPS | sed 's/,/\n/' | wc -l)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    52
		grep -Fq -- "[\"${STR}\"]" $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    53
		if (($?));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    54
		then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    55
			if ((C>0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    56
			then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    57
				echo "--      [\"${STR}\"] = \"\", -- $MAPS" >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    58
			else
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    59
				echo "--      [\"${STR}\"] = \"\"," >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    60
			fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    61
		fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    62
	done;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    63
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    64
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    65
# Sort
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    66
echo "Step 3: Sort strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    67
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    68
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    69
	echo $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    70
	rm -f __temp_head __temp_tail __temp_lua;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    71
	cat $i | grep -Ev "}|{" | grep -Ev "^[[:space:]]*$" | sort | uniq > __temp_lua;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    72
	echo "locale = {" > __temp_head;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    73
	echo "}" > __temp_tail;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    74
	cat __temp_head __temp_lua __temp_tail > $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    75
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    76
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    77
# Drop unused
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    78
echo "Step 4: Delete unused strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    79
cat stub.lua | grep '"] =' | while read f;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    80
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    81
	PHRASE=$(echo "$f" | sed 's/[^[]*\["//;s/"] =.*//;s/"/\\"/g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    82
	CNT=$(grep -Frc "loc(\"$PHRASE\")" __temp_loc);
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    83
	if (($CNT==0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    84
	then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    85
		echo "|$PHRASE|";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    86
		PHRASE=$(echo "$PHRASE" | sed 's/\\/\\\\/g;s/\[/\\[/g;s/\]/\\]/g;s/\//\\\//g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    87
		sed -i "/.*\[\"$PHRASE\"\].*/d" $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    88
	fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    89
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    90
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    91
# Delete temporary files
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    92
rm __temp_head __temp_tail __temp_lua __temp_loc;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    93
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    94
echo "Done."