summaryrefslogtreecommitdiff
path: root/exgettext
blob: 97313b0877977839336338a79ea3a6b3657e7a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
#! /bin/sh
# Wrapper around gettext for programs using the msgid convention.

# Copyright (C) 1998, 2001, 2004, 2009-2011 Free Software Foundation, Inc.

# Written by Paul Eggert <eggert@twinsun.com>.

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

# This program is distributed in the hope that it will be useful,
# 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# 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}

# The argument to this wrapper is the xgettext command to be executed.
# Extract the xgettext program name from the rest of the command.
xgettext=${1?}
shift

# Save work if we're just wrapping a no-op.
case $xgettext in
:) exit;;
esac

# Find the files to be scanned, and the directory to scan them from.
directory=.
files=
for i
do
  case $i in
  --directory=*)
    directory=`expr " $i" : ' --directory=\(.*\)'`;;
  --files-from=*)
    files_from=`expr " $i" : ' --files-from=\(.*\)'`
    files=`$AWK '/^[^#]/ { print }' $files_from`;;
  esac
done

# Generate keyword options for xgettext,
# by scanning for declarations of functions
# whose parameter names end in "msgid".
generate_keyword_options='
  /^[A-Z_a-z].*\(.*msgid[,)]/ {

    paren_index = index($0, "(")

    name = substr($0, 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($0, 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
    }
  }
'
keyword_options=`(
  cd $directory &&
  $AWK "$generate_keyword_options" $files < /dev/null
)` || exit

# Run the xgettext command, with extra input containing the extra
# msgids that it wouldn't normally find.
generate_emsgids='
  /%e.*}/ {
    line = $0
    while ((percent_index = index(line, "%e")) != 0) {
      line = substr(line, percent_index + 2)
      bracket_index = index(line, "}")
      if (bracket_index == 0) {
	continue
      }
      msgid = substr(line, 1, bracket_index - 1)
      if (index(msgid, "%") != 0) {
        continue
      }
      printf "#line %d \"%s\"\n", FNR, FILENAME
      printf "_(\"%s\")\n", msgid
      line = substr(line, bracket_index + 1)
    }
  }
'
(cd $directory &&
 $AWK "$generate_emsgids" $files < /dev/null
) | "$xgettext" $keyword_options ${1+"$@"} -