1#! /bin/sh
2
3# Test of gettext facilities (including plural handling) in the Python
4# language.
5
6# Note: This test fails with Python 2.3, 2.4 when an UTF-8 locale is present.
7# It looks like a bug in Python's gettext.py. This here is a quick workaround:
8UTF8_LOCALE_UNSUPPORTED=yes
9
10tmpfiles=""
11trap 'rm -fr $tmpfiles' 1 2 3 15
12
13tmpfiles="$tmpfiles prog.py"
14cat <<\EOF > prog.py
15import sys
16import gettext
17
18n = int(sys.argv[1])
19
20gettext.textdomain('prog')
21gettext.bindtextdomain('prog', '.')
22
23print gettext.gettext("'Your command, please?', asked the waiter.")
24print gettext.ngettext("a piece of cake","%(count)d pieces of cake",n) \
25      % { 'count': n }
26print gettext.gettext("%(oldCurrency)s is replaced by %(newCurrency)s.") \
27      % { 'oldCurrency': "FF", 'newCurrency' : "EUR" }
28EOF
29
30tmpfiles="$tmpfiles prog.tmp prog.pot"
31: ${XGETTEXT=xgettext}
32${XGETTEXT} -o prog.tmp --omit-header --no-location prog.py
33test $? = 0 || { rm -fr $tmpfiles; exit 1; }
34tr -d '\r' < prog.tmp > prog.pot
35test $? = 0 || { rm -fr $tmpfiles; exit 1; }
36
37tmpfiles="$tmpfiles prog.ok"
38cat <<EOF > prog.ok
39msgid "'Your command, please?', asked the waiter."
40msgstr ""
41
42#, python-format
43msgid "a piece of cake"
44msgid_plural "%(count)d pieces of cake"
45msgstr[0] ""
46msgstr[1] ""
47
48#, python-format
49msgid "%(oldCurrency)s is replaced by %(newCurrency)s."
50msgstr ""
51EOF
52
53: ${DIFF=diff}
54${DIFF} prog.ok prog.pot || exit 1
55
56tmpfiles="$tmpfiles fr.po"
57cat <<\EOF > fr.po
58msgid ""
59msgstr ""
60"Content-Type: text/plain; charset=ISO-8859-1\n"
61"Plural-Forms: nplurals=2; plural=(n > 1);\n"
62
63msgid "'Your command, please?', asked the waiter."
64msgstr "�Votre commande, s'il vous plait�, dit le gar�on."
65
66# Les gateaux allemands sont les meilleurs du monde.
67#, python-format
68msgid "a piece of cake"
69msgid_plural "%(count)d pieces of cake"
70msgstr[0] "un morceau de gateau"
71msgstr[1] "%(count)d morceaux de gateau"
72
73# Reverse the arguments.
74#, python-format
75msgid "%(oldCurrency)s is replaced by %(newCurrency)s."
76msgstr "%(newCurrency)s remplace %(oldCurrency)s."
77EOF
78
79tmpfiles="$tmpfiles fr.po.tmp fr.po.new"
80: ${MSGMERGE=msgmerge}
81${MSGMERGE} -q -o fr.po.tmp fr.po prog.pot
82test $? = 0 || { rm -fr $tmpfiles; exit 1; }
83tr -d '\r' < fr.po.tmp > fr.po.new
84test $? = 0 || { rm -fr $tmpfiles; exit 1; }
85
86: ${DIFF=diff}
87${DIFF} fr.po fr.po.new || exit 1
88
89tmpfiles="$tmpfiles fr"
90test -d fr || mkdir fr
91test -d fr/LC_MESSAGES || mkdir fr/LC_MESSAGES
92
93: ${MSGFMT=msgfmt}
94${MSGFMT} -o fr/LC_MESSAGES/prog.mo fr.po
95
96# Test for presence of python version 2.3 or newer.
97(python -V) >/dev/null 2>/dev/null \
98  || { echo "Skipping test: python not found"; rm -fr $tmpfiles; exit 77; }
99case `python -c 'import sys; print sys.hexversion >= 0x20300F0'` in
100  1 | True) ;;
101  *) echo "Skipping test: python version too old"; rm -fr $tmpfiles; exit 77;;
102esac
103
104tmpfiles="$tmpfiles prog.ok prog.oku prog.out"
105: ${DIFF=diff}
106cat <<\EOF > prog.ok
107Votre commande, s'il vous plait�, dit le gar�on.
1082 morceaux de gateau
109EUR remplace FF.
110EOF
111cat <<\EOF > prog.oku
112��Votre commande, s'il vous plait��, dit le gar��on.
1132 morceaux de gateau
114EUR remplace FF.
115EOF
116
117: ${LOCALE_FR=fr_FR}
118: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
119if test $LOCALE_FR != none; then
120  LANGUAGE= LC_ALL=$LOCALE_FR python prog.py 2 > prog.out || exit 1
121  ${DIFF} prog.ok prog.out || exit 1
122fi
123if test -z "$UTF8_LOCALE_UNSUPPORTED"; then
124  if test $LOCALE_FR_UTF8 != none; then
125    LANGUAGE= LC_ALL=$LOCALE_FR_UTF8 python prog.py 2 > prog.out || exit 1
126    ${DIFF} prog.oku prog.out || exit 1
127  fi
128  if test $LOCALE_FR = none && test $LOCALE_FR_UTF8 = none; then
129    if test -f /usr/bin/localedef; then
130      echo "Skipping test: no french locale is installed"
131    else
132      echo "Skipping test: no french locale is supported"
133    fi
134    rm -fr $tmpfiles; exit 77
135  fi
136else
137  if test $LOCALE_FR = none; then
138    if test -f /usr/bin/localedef; then
139      echo "Skipping test: no traditional french locale is installed"
140    else
141      echo "Skipping test: no traditional french locale is supported"
142    fi
143    rm -fr $tmpfiles; exit 77
144  fi
145fi
146
147rm -fr $tmpfiles
148
149exit 0
150