File: translate

package info (click to toggle)
translate 0.6-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 52 kB
  • ctags: 8
  • sloc: sh: 164; makefile: 40
file content (220 lines) | stat: -rwxr-xr-x 4,862 bytes parent folder | download | duplicates (4)
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
#
# translate -- script for translating words
# V0.5: Copyright(C) 1999 by Jochem Huhmann <joh@revier.com>
# Modified by Wolfgang Jhrling <wolfgang@pro-linux.de> 
#
# translate 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 2, or (at your option) any
# later version.
#
# translate 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 XEmacs; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

PATH=$PATH:/usr/bin
# Built-in defaults
VERSION=0.6
LOCDIR=~/.translate
GLOBDIR=/usr/share/trans
GLOBALCONF=/etc/translate.conf
LOCALCONF=$LOCDIR/translate.conf
LANGUAGE=de-en
WHOLEWORD=false
DONT_ASK=false
INVERS=false

# The help message

usage ()
  {
    echo "
`basename $0` Version $VERSION		20/6/1999		joh@revier.com

Looks up a word in a file with language-to-language translations
(field separator should be \" :: \") and maintains local dictionaries.

USAGE: 
`basename $0` [-niwvh] [-l languages] [words to translate]

OPTIONS:
 -n	non-interactive (don't prompt if no matches found)
 -i	inverse lookup (from second to first language)
 -w     search only complete words
 -l	languages to translate between
 -v	display version and exit
 -h	display this message

EXAMPLE: `basename $0` -l de-en -i simplest
"
  }

invert_lang ()
{
  if [ $INVERS = true ]
  then
    INVERS=false
  else
    INVERS=true
  fi
}

wholeword ()
{
 if [ $WHOLEWORD = true ]
 then
    WHOLEWORD = false
 else
    WHOLEWORD = true
 fi
}

display ()
{
  if [ "$2" = "-n" ]; then
    OPT="-n"
  fi
  if [ $UTF8 -eq 1 ]; then
    echo $OPT "$1"
  else
    echo $OPT "$1" | iconv -f UTF-8 -t $CHARSET
  fi
}

# Don't create $LOCDIR automatically to prevent homedir polution
#if [ ! -d $LOCDIR ] 
#  then
#    echo -n "creating private folder $LOCDIR... "
#    mkdir $LOCDIR && echo "OK."
#fi

# Read configuration file, if any
if [ -r $GLOBALCONF ]
then 
  . $GLOBALCONF
fi
if [ -r $LOCALCONF ]
then	
  . $LOCALCONF
fi

# Command line processing
set -- `getopt "vhniwl:" "$@"`
for i 
do
  case "$i"
    in
    -v) echo "`basename $0` Version $VERSION"; exit 0 ;;
    -w) WHOLEWORD=true ; shift;;
    -h) usage; exit 0 ;;
    -n) DONT_ASK=true; shift;;
    -i) invert_lang; shift;;
    -l) LANGUAGE=$2; shift; shift;;
    --) shift; break;;    
  esac
done                                   
if
  [ $# = 0 ] 
  then
    usage; exit 1
fi

# Finally we should know what language to use
GLOBDIC=$GLOBDIR/$LANGUAGE
LOCDIC=$LOCDIR/$LANGUAGE

# Is there already a global dictionary for these languages
# or should we rely on our own, private one?
if [ -r $GLOBDIC ]
then 
  if [ -r $LOCDIC ]
  then
    DIC="$GLOBDIC $LOCDIC"
  else
    DIC="$GLOBDIC"
  fi
else
  echo "`basename $0`: Global dictionary $GLOBDIC does not exist or is not readable!"
  if [ -r $LOCDIC ]
  then  
    echo "`basename $0`: Fallback to $LOCDIC"
    DIC=$LOCDIC
  else
    echo "`basename $0`: Fallback to $LOCDIC is not posible."
    echo "No dictionaries exist!"
    exit 0
  fi
fi

if [ ! "$CHARSET" ] ; then
    # ISO-8859-1 is just an assumption (a bad one) but I've no idea how to
    # find out the appropriate charmap.  This way, please can at least
    # overwrite it through their configuration files.
    CHARSET="ISO-8859-1"
fi

UTF8=0
if locale 2>&1 | grep -E -q "UTF-8?$"; then
    UTF8=1
fi
SEARCH=$*
[ $UTF8 -eq 0 ] && SEARCH=$(echo "$SEARCH" | iconv -f $CHARSET -t UTF-8)
# now get the real work done
if [ 1 ] ; then
    if 
	[ $WHOLEWORD = false ]
    then
	if 
	    [ $INVERS = false ]
	then
	    result=$(egrep -ihs  "$SEARCH".*' :: ' $DIC)
	else
	    result=$(egrep -ihs  ' :: '.*"$SEARCH" $DIC)
	fi
    else
	if 
	    [ $INVERS = false ]
	then
	    result=$(egrep -ihs  '\b'"$SEARCH"'\b'.*' :: ' $DIC)
	else
	    result=$(egrep -ihs  ' :: '.*'\b'"$SEARCH"'\b' $DIC)
	fi
    fi
fi

if [ "$result" ]; then
  display "$result"
  exit 0
else # Fallback to human intelligence (unless "-n" is present)
  if 
    [ $DONT_ASK = true ] 
  then 
    display "Sorry \"$SEARCH\" not found" 
  else
    echo "No matches. Teach me or press Return."
    display "$SEARCH :: " -n
    read NEW_ENTRY
    if [ -z "$NEW_ENTRY" ]
    then # User is lazy
      exit 0
    else # We've got a new entry
      if
         [ $INVERS = false ]
      # FIXME: handle UTF-8 (?)
      then
        echo "$SEARCH :: $NEW_ENTRY" >> $LOCDIC
      else 
        echo "$NEW_ENTRY :: $SEARCH" >> $LOCDIC
      fi
        exit 0
    fi
  fi
fi
exit 0