03npgettext_pp.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 => 99;
  7. use Locale::Messages qw (bindtextdomain textdomain npgettext);
  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. textdomain 'not_here';
  35. # make sure it works with distinct key (where context doesn't matter)
  36. my $context3 = "Context here (3)";
  37. my @strings3 = ("Singular 3", "Plural 3");
  38. for (0 .. 9) {
  39. # Prototype checking fails here if you pass the list @strings.
  40. my $translation = npgettext ($context3, $strings3[0], $strings3[1], $_);
  41. my $expected = $_ == 1 ? 'Singular 3' : 'Plural 3';
  42. ok $translation, $expected;
  43. }
  44. # not try a msgid that matches existing one
  45. my $context = "Context here (2)";
  46. my @strings = qw (Singular Plural);
  47. for (0 .. 9) {
  48. # Prototype checking fails here if you pass the list @strings.
  49. my $translation = npgettext ($context, $strings[0], $strings[1], $_);
  50. my $expected = $_ == 1 ? 'Singular' : 'Plural';
  51. ok $translation, $expected;
  52. }
  53. my $textdomain = 'existing';
  54. Locale::Messages::nl_putenv ("LANGUAGE=C");
  55. Locale::Messages::nl_putenv ("LC_ALL=C");
  56. Locale::Messages::nl_putenv ("LANG=C");
  57. Locale::Messages::nl_putenv ("LC_MESSAGES=C");
  58. POSIX::setlocale (POSIX::LC_ALL() => '');
  59. my $bound_dir = bindtextdomain $textdomain => $locale_dir;
  60. ok defined $bound_dir;
  61. ok (File::Spec->catdir ($bound_dir), File::Spec->catdir ($bound_dir));
  62. my $bound_domain = textdomain $textdomain;
  63. ok defined $bound_domain;
  64. ok $bound_domain, $textdomain;
  65. for (0 .. 9) {
  66. my $translation = npgettext ($context, $strings[0], $strings[1], $_);
  67. my $expected = $_ == 1 ? 'Singular' : 'Plural';
  68. ok $translation, $expected;
  69. }
  70. Locale::Messages::nl_putenv ("LANGUAGE=de_AT");
  71. Locale::Messages::nl_putenv ("LC_ALL=de_AT");
  72. Locale::Messages::nl_putenv ("LANG=de_AT");
  73. Locale::Messages::nl_putenv ("LC_MESSAGES=de_AT");
  74. my $missing_locale = POSIX::setlocale (POSIX::LC_ALL() => '') ?
  75. '' : 'locale de_AT missing';
  76. for (0 .. 9) {
  77. my $translation = npgettext ($context, $strings[0], $strings[1], $_);
  78. my $expected = $_ == 1 ? 'Einzahl 2' : 'Mehrzahl 2';
  79. ok $translation, $expected;
  80. }
  81. $textdomain = 'additional';
  82. Locale::Messages::nl_putenv ("LANGUAGE=C");
  83. Locale::Messages::nl_putenv ("LC_ALL=C");
  84. Locale::Messages::nl_putenv ("LANG=C");
  85. Locale::Messages::nl_putenv ("LC_MESSAGES=C");
  86. POSIX::setlocale (POSIX::LC_ALL() => '');
  87. $bound_dir = bindtextdomain $textdomain => $locale_dir;
  88. ok defined $bound_dir;
  89. ok (File::Spec->catdir ($bound_dir), File::Spec->catdir ($bound_dir));
  90. $bound_domain = textdomain $textdomain;
  91. ok defined $bound_domain;
  92. ok $bound_domain, $textdomain;
  93. for (0 .. 9) {
  94. my $translation = npgettext ($context, $strings[0], $strings[1], $_);
  95. my $expected = $_ == 1 ? 'Singular' : 'Plural';
  96. ok $translation, $expected;
  97. }
  98. Locale::Messages::nl_putenv ("LANGUAGE=de_AT");
  99. Locale::Messages::nl_putenv ("LC_ALL=de_AT");
  100. Locale::Messages::nl_putenv ("LANG=de_AT");
  101. Locale::Messages::nl_putenv ("LC_MESSAGES=de_AT");
  102. POSIX::setlocale (POSIX::LC_ALL() => '');
  103. for (0 .. 40) {
  104. my $translation = npgettext ($context, $strings[0], $strings[1], $_);
  105. my $plural = ($_ == 1 ? 0 :
  106. $_ % 10 == 2 ? 1 :
  107. $_ % 10 == 3 || $_ %10 == 4 ? 2 : 3);
  108. ok $translation, "Numerus 2:$plural";
  109. }
  110. __END__
  111. Local Variables:
  112. mode: perl
  113. perl-indent-level: 4
  114. perl-continued-statement-offset: 4
  115. perl-continued-brace-offset: 0
  116. perl-brace-offset: -4
  117. perl-brace-imaginary-offset: 0
  118. perl-label-offset: -4
  119. cperl-indent-level: 4
  120. cperl-continued-statement-offset: 2
  121. tab-width: 4
  122. End: