exgettext 4.12 KB
Newer Older
1
#! /bin/sh
2 3
# Wrapper around gettext for programs using the msgid convention.
# Copyright 1998, 2001, 2002 Free Software Foundation, Inc.
4 5 6 7

# Written by Paul Eggert <eggert@twinsun.com>.
# Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.

8
# This program is free software; you can redistribute it and/or modify
9 10 11 12
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

13
# This program is distributed in the hope that it will be useful,
14 15 16 17 18 19 20 21 22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GNU CC; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

23 24 25 26 27 28 29 30
# Always operate in the C locale.
LANG=C
LANGUAGE=C
LC_ALL=C
export LANG LANGUAGE LC_ALL

# Set AWK if environment has not already set it.
AWK=${AWK-awk}
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

# The arguments to this wrapper are: the program to execute, the
# name of the "package", and the path to the source directory.

if [ $# -ne 3 ]
then	echo "usage: $0 <xgettext> <package> <srcdir>"
	exit 1
fi

xgettext=$1
package=$2
srcdir=$3

nl='
'

set -e

# Create temporary directory for scratch files.
T=exg$$.d
mkdir $T
trap "rm -r $T" 0

pwd=`pwd`
kopt=$pwd/$T/keyword-options
emsg=$pwd/$T/emsgids.c
posr=$pwd/$T/po-sources

59 60
# Locate files to scan, and generate the list.  All .c, .h, and .def files
# in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
61 62 63 64 65 66 67 68 69 70 71 72 73 74
# (directories).  Also, all subdirectories of $srcdir that contain a
# config-lang.in.  Exclusions come from $srcdir/po/EXCLUDE.
#
# Then generate keyword options for xgettext, by scanning for declarations
# of functions whose parameter names end in "msgid".
#
# Finally, generate a source file containing all %e strings from
# driver specs, so those can be translated too.
#
# All in one huge awk script.

echo "scanning for keywords and %e strings..." >&2

( cd $srcdir
75 76 77 78 79 80
  lang_subdirs=`echo */config-lang.in | sed -e 's|config-lang\.in||g'`
  for dir in "" config/ config/*/ $lang_subdirs
  do  for glob in '*.c' '*.h' '*.def'
      do  eval echo $dir$glob
      done
  done | tr ' ' "$nl" | grep -v '\*' |
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v emsg=$emsg '
function keyword_option(line) {
    paren_index = index(line, "(")
    name = substr(line, 1, paren_index - 1)
    sub(/[^0-9A-Z_a-z]*$/, "", name)
    sub(/[	 ]+PARAMS/, "", name)
    sub(/[	 ]+VPARAMS/, "", name)
    sub(/.*[^0-9A-Z_a-z]/, "", name)

    args = substr(line, paren_index)
    sub(/msgid[,\)].*/, "", args)
    for (n = 1; sub(/^[^,]*,/, "", args); n++) {
	continue
    }

    if (n == 1) { keyword = name }
    else        { keyword = name ":" n }

    if (! keyword_seen[keyword]++) {
	print "--keyword=" keyword > kopt
    }
}

function spec_error_string (line) {
    while ((percent_index = index(line, "%e")) != 0) {
	escape = substr(line, percent_index - 1, 1)
	line = substr(line, percent_index + 2)
	if (escape == "%") return

	bracket_index = index(line, "}")
	if (bracket_index == 0) return

	msgid = substr(line, 1, bracket_index - 1)
	if (index(msgid, "%") != 0) return

	printf("#line %d \"%s\"\n", lineno, file) > emsg
	printf("_(\"%s\")\n", msgid) > emsg

	line = substr(line, bracket_index + 1)
    }
}

BEGIN {
  while ((getline < excl) > 0) {
    if ($0 ~ /^#/ || $0 ~ /^[ 	]*$/)
      continue
    excludes[$1] = 1
  }
}

{ if (!($0 in excludes)) {
    print > posr
    files[NR] = $0
  }
}

END {
    for (f in files) {
	file = files[f]
	lineno = 1
	while (getline < file) {
	    if (/^(#[ 	]*define[ 	]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
		keyword_option($0)
	    } else if (/%e/) {
		spec_error_string($0)
	    }
	    lineno++
	}
    }
    print emsg > posr
}'
)

# Run the xgettext command, with temporary added as a file to scan.
echo "running xgettext..." >&2
$xgettext --default-domain=$package --directory=$srcdir \
	  --add-comments `cat $kopt` --files-from=$posr \
158
	  --language=c -o po/$package.pot