tools/update_lua_locale_files.sh
author Wuzzy <Wuzzy2@mail.ru>
Sun, 07 Apr 2019 23:08:33 +0200
changeset 14758 bc0e86b0347b
parent 13511 b62b14aa88d4
permissions -rwxr-xr-x
Don't show SMine timer if drowning
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.
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
     8
# - (Optional) Change LOCALEFILES below to limit the number of locale files to update
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
     9
# Result: All .lua files in share/hedgewars/Data/Locale will be updated.
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    10
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    11
# SETTINGS:
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    12
# 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
    13
# (Note: always include stub.lua)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    14
LOCALEFILES="*.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    15
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    16
# END OF SETTINGS
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    17
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    18
# List of all Lua files to scan:
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    19
# * Missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    20
# * Campaign missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    21
# * Lua libraries
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    22
# * Styles (aka multiplayer scripts)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    23
# * Mission maps
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    24
# 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
    25
#            directories have been added!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    26
LUAFILES="../Missions/Challenge/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    27
 ../Missions/Scenario/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    28
 ../Missions/Training/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    29
 ../Missions/Campaign/*/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    30
 ../Scripts/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    31
 ../Scripts/Multiplayer/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    32
 ../Maps/*/map.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    33
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    34
cd ../share/hedgewars/Data/Locale;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    35
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    36
# Temporary files
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    37
TEMP_LOC=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    38
TEMP_HEAD=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    39
TEMP_TAIL=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    40
TEMP_LUA=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    41
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    42
# Collect strings
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    43
echo "Step 1: Collect strings";
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    44
echo -n "" > $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    45
for F in loc loc_noop;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    46
	do
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    47
	grep -F "$F(\"" $LUAFILES | sed 's/")/")\n/g' | sed "s/.*$F(\"/loc(\"/;s/\").*/\")/" | grep loc | sort | uniq >> $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    48
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    49
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    50
# Update locale files
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    51
# 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
    52
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
    53
for i in $LOCALEFILES;
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
	echo $i;
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    56
	cat $TEMP_LOC | while read f
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    57
		do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    58
		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
    59
		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
    60
		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
    61
		grep -Fq -- "[\"${STR}\"]" $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    62
		if (($?));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    63
		then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    64
			if ((C>0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    65
			then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    66
				echo "--      [\"${STR}\"] = \"\", -- $MAPS" >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    67
			else
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    68
				echo "--      [\"${STR}\"] = \"\"," >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    69
			fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    70
		fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    71
	done;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    72
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    73
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    74
# Sort
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    75
echo "Step 3: Sort strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    76
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    77
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    78
	echo $i;
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    79
	rm -f $TEMP_HEAD $TEMP_TAIL $TEMP_LUA;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    80
	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: 12715
diff changeset
    81
	echo "locale = {" > $TEMP_HEAD;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    82
	echo "}" > $TEMP_TAIL;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    83
	cat $TEMP_HEAD $TEMP_LUA $TEMP_TAIL > $i;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    84
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    85
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    86
# Drop unused
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    87
echo "Step 4: Delete unused strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    88
cat stub.lua | grep '"] =' | while read f;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    89
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    90
	PHRASE=$(echo "$f" | sed 's/[^[]*\["//;s/"] =.*//;s/"/\\"/g');
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    91
	CNT=$(grep -Frc "loc(\"$PHRASE\")" $TEMP_LOC);
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    92
	if (($CNT==0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    93
	then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    94
		echo "|$PHRASE|";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    95
		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
    96
		sed -i "/.*\[\"$PHRASE\"\].*/d" $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    97
	fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    98
done
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
# Delete temporary files
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
   101
rm $TEMP_HEAD $TEMP_TAIL $TEMP_LUA $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   102
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   103
echo "Done."