03dcnpgettext_pp.t 4.0 KB

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