1#! /usr/local/bin/perl -w
2
3# vim: syntax=perl
4# vim: tabstop=4
5
6use strict;
7
8use Test;
9
10use constant NUM_TESTS => 95;
11
12use Locale::Messages qw (bindtextdomain dnpgettext);
13require POSIX;
14require File::Spec;
15
16BEGIN {
17	my $package;
18	if ($0 =~ /_pp\.t$/) {
19		$package = 'gettext_pp';
20	} else {
21		$package = 'gettext_xs';
22	}
23
24	my $selected = Locale::Messages->select_package ($package);
25	if ($selected ne $package && 'gettext_xs' eq $package) {
26		print "1..0 # Skip: Locale::$package not available here.\n";
27		exit 0;
28	}
29	plan tests => NUM_TESTS;
30}
31
32Locale::Messages::nl_putenv ("LANGUAGE=C");
33Locale::Messages::nl_putenv ("LC_ALL=C");
34Locale::Messages::nl_putenv ("LANG=C");
35Locale::Messages::nl_putenv ("LC_MESSAGES=C");
36Locale::Messages::nl_putenv ("OUTPUT_CHARSET=iso-8859-1");
37
38POSIX::setlocale (POSIX::LC_ALL() => '');
39
40my $locale_dir = $0;
41$locale_dir =~ s,[^\\/]+$,, or $locale_dir = '.';
42$locale_dir .= '/LocaleData';
43
44bindtextdomain not_here => $locale_dir;
45
46# make sure it works with distinct key (where context doesn't matter)
47my $context3 = "Context here (3)";
48my @strings3 = ("Singular 3", "Plural 3");
49for (0 .. 9) {
50	# Prototype checking fails here if you pass the list @strings.
51	my $translation = dnpgettext (not_here => $context3, $strings3[0], $strings3[1], $_);
52	my $expected = $_ == 1 ? 'Singular 3' : 'Plural 3';
53	ok $translation, $expected;
54}
55
56# not try a msgid that matches existing one
57my $context = "Context here (2)";
58my @strings = qw (Singular Plural);
59for (0 .. 9) {
60	# Prototype checking fails here if you pass the list @strings.
61	my $translation = dnpgettext (not_here => $context, $strings[0], $strings[1], $_);
62	my $expected = $_ == 1 ? 'Singular' : 'Plural';
63	ok $translation, $expected;
64}
65
66Locale::Messages::nl_putenv ("LANGUAGE=C");
67Locale::Messages::nl_putenv ("LC_ALL=C");
68Locale::Messages::nl_putenv ("LANG=C");
69Locale::Messages::nl_putenv ("LC_MESSAGES=C");
70
71POSIX::setlocale (POSIX::LC_ALL() => '');
72
73my $bound_dir = bindtextdomain existing => $locale_dir;
74
75ok defined $bound_dir;
76ok (File::Spec->catdir ($bound_dir), File::Spec->catdir ($locale_dir));
77
78for (0 .. 9) {
79	my $translation = dnpgettext (existing => $context, $strings[0], $strings[1], $_);
80	my $expected = $_ == 1 ? 'Singular' : 'Plural';
81	ok $translation, $expected;
82}
83
84Locale::Messages::nl_putenv ("LANGUAGE=de_AT");
85Locale::Messages::nl_putenv ("LC_ALL=de_AT");
86Locale::Messages::nl_putenv ("LANG=de_AT");
87Locale::Messages::nl_putenv ("LC_MESSAGES=de_AT");
88
89POSIX::setlocale (POSIX::LC_ALL() => '');
90
91for (0 .. 9) {
92	my $translation = dnpgettext (existing => $context, $strings[0], $strings[1], $_);
93	my $expected = $_ == 1 ? 'Einzahl 2' : 'Mehrzahl 2';
94	ok $expected, $translation;
95}
96
97Locale::Messages::nl_putenv ("LANGUAGE=C");
98Locale::Messages::nl_putenv ("LC_ALL=C");
99Locale::Messages::nl_putenv ("LANG=C");
100Locale::Messages::nl_putenv ("LC_MESSAGES=C");
101
102POSIX::setlocale (POSIX::LC_ALL() => '');
103
104$bound_dir = bindtextdomain additional => $locale_dir;
105
106ok defined $bound_dir;
107ok (File::Spec->catdir ($bound_dir), File::Spec->catdir ($locale_dir));
108
109for (0 .. 9) {
110	my $translation = dnpgettext (additional => $context, $strings[0], $strings[1], $_);
111	my $expected = $_ == 1 ? 'Singular' : 'Plural';
112	ok $translation, $expected;
113}
114
115Locale::Messages::nl_putenv ("LANGUAGE=de_AT");
116Locale::Messages::nl_putenv ("LC_ALL=de_AT");
117Locale::Messages::nl_putenv ("LANG=de_AT");
118Locale::Messages::nl_putenv ("LC_MESSAGES=de_AT");
119
120POSIX::setlocale (POSIX::LC_ALL() => '');
121
122for (0 .. 40) {
123	my $translation = dnpgettext (additional => $context, $strings[0], $strings[1], $_);
124	my $plural = ($_ == 1 ? 0 :
125				  $_ % 10 == 2 ? 1 :
126				  $_ % 10 == 3 || $_ %10 == 4 ? 2 : 3);
127	ok $translation, "Numerus 2:$plural";
128}
129
130__END__
131
132Local Variables:
133mode: perl
134perl-indent-level: 4
135perl-continued-statement-offset: 4
136perl-continued-brace-offset: 0
137perl-brace-offset: -4
138perl-brace-imaginary-offset: 0
139perl-label-offset: -4
140cperl-indent-level: 4
141cperl-continued-statement-offset: 2
142tab-width: 4
143End:
144
145