Made internationalisation optional; to compile the game without I18N,
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 11 Sep 2007 23:58:30 +0000 (23:58 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 11 Sep 2007 23:58:30 +0000 (23:58 +0000)
use "make DISABLE_NLS=1".  I moved most of the gettext-related Makefile
stuff to po/Makefile and added a couple of the more useful ("standard"?)
features found in autotools-based packages:  support for the LINGUAS
environment variable and the "update-po" target.

git-svn-id: https://s.snth.net/svn/neverball/trunk@1119 78b8d119-cf0a-0410-b17c-f493084dd1d7

Makefile
po/Makefile [new file with mode: 0644]
scripts/extractpo.sh
share/lang.c
share/lang.h

index de09b9b..fc86dd7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,13 @@ PNG_CPPFLAGS := $(shell libpng-config --cflags)
 
 ALL_CFLAGS   := $(CFLAGS)
 ALL_CPPFLAGS := $(SDL_CPPFLAGS) $(PNG_CPPFLAGS) -Ishare \
-    -DVERSION=\"$(VERSION)\" $(CPPFLAGS)
+    -DVERSION=\"$(VERSION)\"
+
+ifdef DISABLE_NLS
+    ALL_CPPFLAGS += -DDISABLE_NLS=1
+endif
+
+ALL_CPPFLAGS += $(CPPFLAGS)
 
 #------------------------------------------------------------------------------
 
@@ -24,9 +30,15 @@ SDL_LIBS := $(shell sdl-config --libs)
 PNG_LIBS := $(shell libpng-config --libs)
 
 ifdef MINGW
+ifndef DISABLE_NLS
+    INTL_LIBS := -lintl
+endif
+endif
+
+ifdef MINGW
     OGL_LIBS  := -lopengl32 -lm
-    BASE_LIBS := -lSDL -lSDL_image -lintl
-    ALL_LIBS  := $(SDL_LIBS) -lSDL_image -lintl \
+    BASE_LIBS := -lSDL -lSDL_image $(INTL_LIBS)
+    ALL_LIBS  := $(SDL_LIBS) -lSDL_image $(INTL_LIBS) \
        $(PNG_LIBS) -lSDL_ttf -lSDL_mixer $(OGL_LIBS)
 else
     OGL_LIBS  := -lGL -lm
@@ -49,16 +61,6 @@ PUTT_TARG := neverputt$(EXT)
 
 #------------------------------------------------------------------------------
 
-LOCALEDIR   := locale
-LOCALEDOM   := neverball
-POTFILE     := po/neverball.pot
-MESSAGEPART := /LC_MESSAGES/$(LOCALEDOM).mo
-POS         := $(shell echo po/*.po)
-LINGUAS     := $(POS:po/%.po=%)
-MESSAGES    := $(LINGUAS:%=$(LOCALEDIR)/%$(MESSAGEPART))
-
-#-------------------------------------------------------------------------------
-
 MAPC_OBJS := \
        share/vec3.o        \
        share/base_image.o  \
@@ -149,10 +151,6 @@ SOLS := $(MAPS:%.map=%.sol)
 %.sol : %.map $(MAPC_TARG)
        $(WINE) ./$(MAPC_TARG) $< data
 
-$(LOCALEDIR)/%$(MESSAGEPART) : po/%.po
-       mkdir -p `dirname $@`
-       msgfmt -c -v -o $@ $<
-
 #------------------------------------------------------------------------------
 
 all : $(BALL_TARG) $(PUTT_TARG) $(MAPC_TARG) sols locales
@@ -168,7 +166,10 @@ $(MAPC_TARG) : $(MAPC_OBJS)
 
 sols : $(SOLS)
 
-locales : $(MESSAGES)
+locales :
+ifndef DISABLE_NLS
+       $(MAKE) -C po
+endif
 
 clean-src :
        $(RM) $(BALL_TARG) $(BALL_OBJS) $(BALL_DEPS)
@@ -177,29 +178,14 @@ clean-src :
 
 clean : clean-src
        $(RM) $(SOLS)
-       rm -rf $(LOCALEDIR)
+       $(MAKE) -C po clean
 
 test : all
        ./neverball
 
 #------------------------------------------------------------------------------
 
-po/%.po : $(POTFILE)
-       msgmerge -U $@ $<
-       touch $@
-       
-po-update-extract :
-       sh scripts/extractpo.sh $(POTFILE) $(LOCALEDOM)
-
-po-update-merge : $(POS)
-
-po-update : po-update-extract po-update-merge
-
-#------------------------------------------------------------------------------
-
-.PHONY : all sols locales clean-src clean test \
-       po-update-extract po-update-merge po-update \
-
+.PHONY : all sols locales clean-src clean test
 
 -include $(BALL_DEPS) $(PUTT_DEPS) $(MAPC_DEPS)
 
diff --git a/po/Makefile b/po/Makefile
new file mode 100644 (file)
index 0000000..b13872d
--- /dev/null
@@ -0,0 +1,79 @@
+#-------------------------------------------------------------------------------
+
+# Basic LINGUAS environment variable support
+
+ALL_LINGUAS := $(basename $(wildcard *.po))
+
+ifneq ($(LINGUAS),)
+    LINGUAS := $(filter $(ALL_LINGUAS), $(LINGUAS))
+else
+    LINGUAS := $(ALL_LINGUAS)
+endif
+
+#-------------------------------------------------------------------------------
+
+ROOT := ..
+
+DOMAIN    := neverball
+LOCALEDIR := $(ROOT)/locale
+POFILES   := $(LINGUAS:%=%.po)
+MOFILES   := $(LINGUAS:%=$(LOCALEDIR)/%/LC_MESSAGES/$(DOMAIN).mo)
+SOURCES   := \
+    $(wildcard $(ROOT)/ball/*.[ch])  \
+    $(wildcard $(ROOT)/share/*.[ch]) \
+    $(wildcard $(ROOT)/putt/*.[ch])
+
+#------------------------------------------------------------------------------
+
+COPYRIGHT := Robert Kooima
+BUGADDR   := robert.kooima@gmail.com
+
+XGETTEXT := xgettext
+XGETTEXT_FLAGS := \
+    --add-comments=TRANSLATORS --from-code=UTF-8 \
+    --keyword=_ --keyword=N_ --keyword=sgettext  \
+    --default-domain="$(DOMAIN)"                 \
+    --copyright-holder="$(COPYRIGHT)"            \
+    --msgid-bugs-address="$(BUGADDR)"
+
+#------------------------------------------------------------------------------
+
+all: $(MOFILES)
+
+clean:
+       rm -rf $(LOCALEDIR)
+
+#------------------------------------------------------------------------------
+
+# Here it gets somewhat convoluted.  Basic idea is:  we want the
+# update-po target which is widely supported in autotools-based
+# internationalised packages.  update-po unconditionally updates the POT
+# file, and thus forces PO and MO files to be updated as well.  The
+# logic is loosely based on Makefile.in.in distributed with GNU gettext.
+
+update-po: $(DOMAIN).pot-update $(POFILES) $(MOFILES)
+
+$(LOCALEDIR)/%/LC_MESSAGES/$(DOMAIN).mo: %.po
+       mkdir -p $$(dirname $@)
+       msgfmt -c --statistics -o $@ $<
+
+%.po: $(DOMAIN).pot
+       msgmerge -U $@ $<
+       touch $@
+
+$(DOMAIN).pot:
+       $(MAKE) $(DOMAIN).pot-update
+
+$(DOMAIN).pot-update:
+       $(XGETTEXT) $(XGETTEXT_FLAGS) -o $(DOMAIN).pot \
+           --directory="$(ROOT)" $(SOURCES:$(ROOT)/%=%)
+       
+       # Force encoding to UTF-8
+       sed "1,/^$$/ s/CHARSET/UTF-8/" < $(DOMAIN).pot > $(DOMAIN).pot.utf8
+       mv -f $(DOMAIN).pot.utf8 $(DOMAIN).pot
+       
+       sh $(ROOT)/scripts/extractpo.sh $(ROOT) >> $(DOMAIN).pot
+       msguniq -o $(DOMAIN).pot $(DOMAIN).pot
+
+#------------------------------------------------------------------------------
+
index bb36549..72f828a 100644 (file)
@@ -1,8 +1,9 @@
 #!/bin/sh
-# This program creates the neverball.pot file from source code and level files.
-
+# This script extracts translatable messages from maps and sets.
 # Copyright (C) 2006 Jean Privat
-# Part of the Neverball Project http://icculus.org/neverball/
+#
+# Part of the Neverball project
+# http://icculus.org/neverball/
 #
 # NEVERBALL is  free software; you can redistribute  it and/or modify
 # it under the  terms of the GNU General  Public License as published
 # MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
 # General Public License for more details.
 
-set -B
-
-POTFILE="$1"
-DOMAIN="$2"
-COPYRIGHT="Robert Kooima"
-BUGADDR="robert.kooima@gmail.com"
-
-DATA=data
-SETS="$DATA"/sets.txt
-COURSES="$DATA"/courses.txt
+test -n "$1" || exit 1
 
-export LC_ALL=C
+LC_ALL=C
+export LC_ALL
 
-# First, extract from source
-echo "# Sources"
-> "$POTFILE"
-xgettext --add-comments=TRANSLATORS --from-code=UTF-8 --keyword=_ --keyword=N_ --keyword=sgettext -d "$DOMAIN" --copyright-holder="$COPYRIGHT" --msgid-bugs-address="$BUGADDR" -F -o "$POTFILE" {ball,putt,share}/*.[ch]
+cd "$1"
 
-# Force encoding to UTF-8
-sed "1,/^$/ s/charset=CHARSET/charset=UTF-8/" < "$POTFILE" > "$POTFILE".utf8
-mv "$POTFILE".utf8 "$POTFILE"
+DATA="data"
+SETS="$DATA/sets.txt"
+COURSES="$DATA/courses.txt"
 
-# Second, extract from neverball sets and neverputt courses
-echo "# Sets and courses"
 for i in $(cat "$SETS"); do
-       i="$DATA"/"$i"
-
-       # Only translate the two first lines
-       head -2 $i | while read -r d; do
-               echo
-               echo "#: $i"
-               # Convert \ to \\
-               echo "msgid \"$(echo "$d" | sed 's/\\/\\\\/g')\""
-               echo "msgstr \"\""
-       done >> $POTFILE
+    i="$DATA/$i"
+
+    # Only translate the two first lines
+    head -n 2 $i | while read -r d; do
+        echo
+        echo "#: $i"
+        echo "msgid \"$(echo "$d" | sed 's/\\/\\\\/g')\""
+        echo "msgstr \"\""
+    done
 done
 
 # the "echo | cat x -" forces the end of the last line
 echo | cat "$COURSES" - | while read -r d; do
-       # Heuristic: description is non empty line without .txt inside
-       if test -n "$d" && echo "$d" | grep -v ".txt" &> /dev/null; then
-               echo
-               echo "#: $COURSES"
-               # Convert \ to \\
-               echo "msgid \"$(echo "$d" | sed 's/\\/\\\\/g')\""
-               echo "msgstr \"\""
-       fi
-done >> $POTFILE
-
-# Third, extracts from levels
-echo -n "# Levels: "
-find "$DATA" -name "*.map" | sort | tee .map_list | wc -l
-for i in $(cat .map_list); do
-       # Check encoding?
-       # file --mime $i
-       echo -n '.'
-       # Only translatable string is "message"
-       grep -E "^\"message\"" "$i" | while read -r a b; do
-               echo
-               echo "#: $i"
-               echo "msgid $(echo "$b" | sed 's/\\/\\\\/g')"
-               echo "msgstr \"\""
-       done >> $POTFILE
+    # Heuristic: description is non empty line without .txt inside
+    if test -n "$d" && echo "$d" | grep -v ".txt" &> /dev/null; then
+        echo
+        echo "#: $COURSES"
+        echo "msgid \"$(echo "$d" | sed 's/\\/\\\\/g')\""
+        echo "msgstr \"\""
+    fi
 done
-echo " ok"
-rm .map_list
 
-# Remove duplicates, to keep msgmerge from complaining
-echo "# Removing duplicates."
-msguniq -o "$POTFILE" -t UTF-8 "$POTFILE"
+for i in $(find $DATA -name "*.map" | sort); do
+    # Only translatable string is "message"
+    grep -E "^\"message\"" "$i" | while read -r a b; do
+        echo
+        echo "#: $i"
+        echo "msgid $(echo "$b" | sed 's/\\/\\\\/g')"
+        echo "msgstr \"\""
+    done
+done
 
index 03aaf7c..94824fc 100644 (file)
@@ -23,6 +23,7 @@
 
 void lang_init(const char *domain, const char *default_dir)
 {
+#ifndef DISABLE_NLS
     char *dir = getenv("NEVERBALL_LOCALE");
 
     setlocale(LC_ALL, "");
@@ -30,11 +31,16 @@ void lang_init(const char *domain, const char *default_dir)
     bindtextdomain(domain, dir ? dir : default_dir);
     bind_textdomain_codeset(domain, "UTF-8");
     textdomain(domain);
+#endif
 }
 
 const char *sgettext(const char *msgid)
 {
+#ifndef DISABLE_NLS
     const char *msgval = gettext(msgid);
+#else
+    const char *msgval = msgid;
+#endif
 
     if (msgval == msgid)
     {
index 58e06ab..9d625ef 100644 (file)
@@ -15,7 +15,7 @@
 #ifndef LANG_H
 #define LANG_H
 
-#ifndef POSIX
+#ifndef DISABLE_NLS
 #include <libintl.h>
 #define _(String)   gettext(String)
 #else