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