tools/update_lua_locale_files.sh
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 13511 b62b14aa88d4
permissions -rwxr-xr-x
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.

#!/bin/sh -
# Script to update all Lua locale files.
# It's Clunky and slow!
# Note this script may sooner or later be phased out when we move to Gettext.

# HOW TO USE:
# - Run this script in the tools/ directory.
# - (Optional) Change LOCALEFILES below to limit the number of locale files to update
# Result: All .lua files in share/hedgewars/Data/Locale will be updated.

# SETTINGS:
# Space-separated list of locale files to update, or *.lua for all.
# (Note: always include stub.lua)
LOCALEFILES="*.lua"

# END OF SETTINGS

# List of all Lua files to scan:
# * Missions
# * Campaign missions
# * Lua libraries
# * Styles (aka multiplayer scripts)
# * Mission maps
# IMPORTANT: Don't forget to update this list when new places for Lua
#            directories have been added!
LUAFILES="../Missions/Challenge/*.lua\
 ../Missions/Scenario/*.lua\
 ../Missions/Training/*.lua\
 ../Missions/Campaign/*/*.lua\
 ../Scripts/*.lua\
 ../Scripts/Multiplayer/*.lua\
 ../Maps/*/map.lua"

cd ../share/hedgewars/Data/Locale;

# Temporary files
TEMP_LOC=$(mktemp);
TEMP_HEAD=$(mktemp);
TEMP_TAIL=$(mktemp);
TEMP_LUA=$(mktemp);

# Collect strings
echo "Step 1: Collect strings";
echo -n "" > $TEMP_LOC;
for F in loc loc_noop;
	do
	grep -F "$F(\"" $LUAFILES | sed 's/")/")\n/g' | sed "s/.*$F(\"/loc(\"/;s/\").*/\")/" | grep loc | sort | uniq >> $TEMP_LOC;
done

# Update locale files
# This step is clunky and inefficient. Improve performance (if you are bored)!
echo "Step 2: Update locale files (this may take a while)";
for i in $LOCALEFILES;
do
	echo $i;
	cat $TEMP_LOC | while read f
		do
		STR=$(echo "$f" | sed 's/loc("//;s/")\s*$//;s/"/\\"/g');
		MAPS=$(grep -F -l -- "loc(\"${STR}\")" $LUAFILES | sed 's/.*\/\([^\/]*\)\/map.lua/\1/;s/.*Campaign\/\([^\/]*\)\//\1:/;s/.*\///;s/.lua//;s/ /_/g' | xargs | sed 's/ /, /g');
		C=$(echo $MAPS | sed 's/,/\n/' | wc -l)
		grep -Fq -- "[\"${STR}\"]" $i;
		if (($?));
		then
			if ((C>0));
			then
				echo "--      [\"${STR}\"] = \"\", -- $MAPS" >> $i;
			else
				echo "--      [\"${STR}\"] = \"\"," >> $i;
			fi;
		fi;
	done;
done

# Sort
echo "Step 3: Sort strings";
for i in $LOCALEFILES;
do
	echo $i;
	rm -f $TEMP_HEAD $TEMP_TAIL $TEMP_LUA;
	cat $i | grep -Ev "}|{" | grep -Ev "^[[:space:]]*$" | sort | uniq > $TEMP_LUA;
	echo "locale = {" > $TEMP_HEAD;
	echo "}" > $TEMP_TAIL;
	cat $TEMP_HEAD $TEMP_LUA $TEMP_TAIL > $i;
done

# Drop unused
echo "Step 4: Delete unused strings";
cat stub.lua | grep '"] =' | while read f;
do
	PHRASE=$(echo "$f" | sed 's/[^[]*\["//;s/"] =.*//;s/"/\\"/g');
	CNT=$(grep -Frc "loc(\"$PHRASE\")" $TEMP_LOC);
	if (($CNT==0));
	then
		echo "|$PHRASE|";
		PHRASE=$(echo "$PHRASE" | sed 's/\\/\\\\/g;s/\[/\\[/g;s/\]/\\]/g;s/\//\\\//g');
		sed -i "/.*\[\"$PHRASE\"\].*/d" $LOCALEFILES;
	fi;
done

# Delete temporary files
rm $TEMP_HEAD $TEMP_TAIL $TEMP_LUA $TEMP_LOC;

echo "Done."