tools/update_lua_locale_files.sh
author Wuzzy <Wuzzy2@mail.ru>
Thu, 08 Mar 2018 17:07:14 +0100
changeset 13125 be5d9fd2c56a
parent 13095 3f3ad415d849
child 13516 b62b14aa88d4
permissions -rwxr-xr-x
ACF2: Make all Weaklings on worst CPU level
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12720
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
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    33
# Temporary files
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    34
TEMP_LOC=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    35
TEMP_HEAD=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    36
TEMP_TAIL=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    37
TEMP_LUA=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    38
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    39
# Collect strings
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    40
echo "Step 1: Collect strings";
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    41
echo -n "" > $TEMP_LOC;
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    42
for F in loc loc_noop;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    43
	do
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    44
	grep -F "$F(\"" $LUAFILES | sed 's/")/")\n/g' | sed "s/.*$F(\"/loc(\"/;s/\").*/\")/" | grep loc | sort | uniq >> $TEMP_LOC;
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    45
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    46
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    47
# Update locale files
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    48
# 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
    49
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
    50
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    51
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    52
	echo $i;
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    53
	cat $TEMP_LOC | while read f
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    54
		do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    55
		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
    56
		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
    57
		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
    58
		grep -Fq -- "[\"${STR}\"]" $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    59
		if (($?));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    60
		then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    61
			if ((C>0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    62
			then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    63
				echo "--      [\"${STR}\"] = \"\", -- $MAPS" >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    64
			else
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    65
				echo "--      [\"${STR}\"] = \"\"," >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    66
			fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    67
		fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    68
	done;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    69
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    70
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    71
# Sort
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    72
echo "Step 3: Sort strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    73
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    74
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    75
	echo $i;
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    76
	rm -f $TEMP_HEAD $TEMP_TAIL $TEMP_LUA;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    77
	cat $i | grep -Ev "}|{" | grep -Ev "^[[:space:]]*$" | sort | uniq > $TEMP_LUA;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    78
	echo "locale = {" > $TEMP_HEAD;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    79
	echo "}" > $TEMP_TAIL;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    80
	cat $TEMP_HEAD $TEMP_LUA $TEMP_TAIL > $i;
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    81
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    82
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    83
# Drop unused
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    84
echo "Step 4: Delete unused strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    85
cat stub.lua | grep '"] =' | while read f;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    86
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    87
	PHRASE=$(echo "$f" | sed 's/[^[]*\["//;s/"] =.*//;s/"/\\"/g');
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    88
	CNT=$(grep -Frc "loc(\"$PHRASE\")" $TEMP_LOC);
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    89
	if (($CNT==0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    90
	then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    91
		echo "|$PHRASE|";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    92
		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
    93
		sed -i "/.*\[\"$PHRASE\"\].*/d" $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    94
	fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    95
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    96
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    97
# Delete temporary files
13095
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12720
diff changeset
    98
rm $TEMP_HEAD $TEMP_TAIL $TEMP_LUA $TEMP_LOC;
12720
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    99
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   100
echo "Done."