File: Debug.pm

package info (click to toggle)
libintl-perl 1.37-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,748 kB
  • sloc: perl: 156,874; makefile: 113; sh: 3
file content (213 lines) | stat: -rwxr-xr-x 6,440 bytes parent folder | download
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
#! /bin/false

# vim: set autoindent shiftwidth=4 tabstop=4:

# Copyright (C) 2002-2026 Guido Flohr <guido.flohr@cantanea.com>,
# all rights reserved.

# This program 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 3 of the License, or
# (at your option) any later version.

# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

package Locale::Messages::Debug;

use strict;

use Locale::Messages;
use Locale::gettext_pp;
use Locale::gettext_dumb;

use vars qw(@EXPORT_OK @ISA);

require Exporter;
@ISA = qw(Exporter);

@EXPORT_OK = qw(debug_gettext);

sub debug_gettext($) {
    my ($msgid) = @_;

    my @hints;

    my $package = $Locale::Messages::package;
    push @hints, "Package is '$package'.";

    my $domainname = Locale::Messages::textdomain();
    push @hints, "Textdomain is '$domainname', (use textdomain() to change this).";

    my $dir = Locale::Messages::bindtextdomain($domainname, '');
    unless (defined $dir && length $dir) {
        $dir = $Locale::gettext_pp::__gettext_pp_default_dir;
    }
    push @hints, "Textdomain is bound to '$dir', (use bindtextdomain() to change this).";

    my $lc_messages = Locale::Messages::LC_MESSAGES();
    if (1729 == $lc_messages) {
        push @hints, "System does not define 'LC_MESSAGES'";
    } else {
        push @hints, "System defines 'LC_MESSAGES'";
    }

    my $locale;
    if ('gettext_dumb' eq $package) {
        push @hints, "Package gettext_dumb selects locale by environment only:";
        $locale = Locale::gettext_dumb::__get_locale();
    } else {
        push @hints, "If setlocale() was called with empty string as second"
                     . " argument, the locale is selected from environment"
                     . " variables:";
    }

    if (exists $ENV{LANGUAGE} && length $ENV{LANGUAGE}) {
        push @hints, "  \$LANGUAGE is set to '$ENV{LANGUAGE}', wins.";
    } elsif (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) {
        push @hints, "  \$LANGUAGE is not set.";
        push @hints, "  \$LC_ALL is set to '$ENV{LC_ALL}', wins.";
    } elsif (exists $ENV{LANG} && length $ENV{LANG}) {
        push @hints, "  \$LANGUAGE and \$LC_ALL are not set.";
        push @hints, "  \$LANG is set to '$ENV{LANG}', wins.";
    } elsif (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES}) {
        push @hints, "  \$LANGUAGE, \$LC_ALL, and \$LANG are not set.";
        push @hints, "  \$LC_MESSAGES is set to '$ENV{LC_MESSAGES}', wins.";
    } else {
        push @hints, "  \$LANGUAGE, \$LC_ALL, \$LANG, and \$LC_MESSAGES are"
                     . " not set.";
        push @hints, "  falling back to 'C' aka 'POSIX'.";
    }

    my (undef, @locales) = 
        Locale::gettext_pp::__selected_locales($locale, 
                                               Locale::Messages::LC_MESSAGES(),
                                               "LC_MESSAGES");

    my @dirs = ($dir);
    my $default_dir = $Locale::gettext_pp::__gettext_pp_default_dir;
    push @dirs, $default_dir if $default_dir && $dir ne $default_dir;

    my @dirs = ($dir);
    my ($tries, $lookup) = Locale::gettext_pp::__extend_locales(@locales);

    my %seen;
	my %loaded;
    my @domains;
    push @hints, "Searching catalogs ...";
    foreach my $basedir (@dirs) {
    	foreach my $try (@$tries) {
			next if $loaded{$try};

    		my $fulldir = File::Spec->catfile($basedir, $try, "LC_MESSAGES");
    		next if $seen{$fulldir}++;

            my $filename = File::Spec->catfile($fulldir, "$domainname.mo");

    		my $domain = Locale::gettext_pp::__load_catalog($filename, $try);
            if ($domain) {
                push @hints, "  $filename: successfully loaded.";
            } else {
                push @hints, "  $filename: $!";
                next;
            }
    		
			$loaded{$try} = 1;

    		$domain->{locale_id} = $lookup->{$try};
    		push @domains, $domain;
    	}
    }

    if (!@domains) {
        push @hints, "No catalog found, giving up.";
    } else {
        foreach my $domain (@domains) {
            if (defined $domain->{messages}->{$msgid}) {
                my @trans = @{$domain->{messages}->{$msgid}};
                shift @trans;
                push @hints, "$domain->{filename}: found translation: >>>$trans[0]<<<";
                last;
            } else {
                push @hints, "$domain->{filename}: msgid not found";
            }
        }
    }

    return wantarray ? @hints : join "\n", @hints;
}

1;

__END__

=head1 NAME

Locale::Messages::Debug - Debug libint-perl Features

=head1 SYNOPSIS

 use Locale::Messages::Debug;

=head1 DESCRIPTION

Message translation with libintl-perl relies on a correct setup and to a
certain extent on features of the operating system.  There are many things 
that can go wrong.  This module aims to help you, when problems occur.  It
is not meant to be used in production code!

The module was introduced with libintl-perl version 1.30.

=head1 FUNCTIONS

The module exports by default nothing.  Every function has to be
imported explicitely.

=over 4

=item B<debug_gettext MSGID>

Tries to retrieve a translation for B<MSGID>.

The function returns a list of hints in string form or the same list as 
one new-line separated string in scalar context;

Example:

    my @hints = debug_gettext "Hello, world!";
    foreach my $hint (@hints) {
        print "hint\n";
    }

=head1 AUTHOR

Copyright (C) 2002-2026 L<Guido Flohr|http://www.guido-flohr.net/>
(L<mailto:guido.flohr@cantanea.com>), all rights reserved.  See the source
code for details!code for details!

=head1 SEE ALSO

Locale::Messages(3pm), Locale::TextDomain(3pm), Locale::gettext_pp(3pm),
Encode(3pm), perllocale(3pm), POSIX(3pm), perl(1), gettext(1), gettext(3)

=cut

__END__

Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
perl-label-offset: -4
cperl-indent-level: 4
cperl-continued-statement-offset: 2
tab-width: 4
End: