| 1 | #! /bin/false | ||
| 2 | |||
| 3 | # vim: set autoindent shiftwidth=4 tabstop=4: | ||
| 4 | |||
| 5 | # Copyright (C) 2002-2026 Guido Flohr <guido.flohr@cantanea.com>, | ||
| 6 | # all rights reserved. | ||
| 7 | |||
| 8 | # This program is free software: you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License as published by | ||
| 10 | # the Free Software Foundation; either version 3 of the License, or | ||
| 11 | # (at your option) any later version. | ||
| 12 | |||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | |||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | |||
| 21 | package Locale::Messages::Debug; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | |||
| 25 | use Locale::Messages; | ||
| 26 | use Locale::gettext_pp; | ||
| 27 | use Locale::gettext_dumb; | ||
| 28 | |||
| 29 | use vars qw(@EXPORT_OK @ISA); | ||
| 30 | |||
| 31 | require Exporter; | ||
| 32 | @ISA = qw(Exporter); | ||
| 33 | |||
| 34 | @EXPORT_OK = qw(debug_gettext); | ||
| 35 | |||
| 36 | sub debug_gettext($) { | ||
| 37 | my ($msgid) = @_; | ||
| 38 | |||
| 39 | my @hints; | ||
| 40 | |||
| 41 | my $package = $Locale::Messages::package; | ||
| 42 | push @hints, "Package is '$package'."; | ||
| 43 | |||
| 44 | my $domainname = Locale::Messages::textdomain(); | ||
| 45 | push @hints, "Textdomain is '$domainname', (use textdomain() to change this)."; | ||
| 46 | |||
| 47 | my $dir = Locale::Messages::bindtextdomain($domainname, ''); | ||
| 48 | unless (defined $dir && length $dir) { | ||
| 49 | $dir = $Locale::gettext_pp::__gettext_pp_default_dir; | ||
| 50 | } | ||
| 51 | push @hints, "Textdomain is bound to '$dir', (use bindtextdomain() to change this)."; | ||
| 52 | |||
| 53 | my $lc_messages = Locale::Messages::LC_MESSAGES(); | ||
| 54 | if (1729 == $lc_messages) { | ||
| 55 | push @hints, "System does not define 'LC_MESSAGES'"; | ||
| 56 | } else { | ||
| 57 | push @hints, "System defines 'LC_MESSAGES'"; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $locale; | ||
| 61 | if ('gettext_dumb' eq $package) { | ||
| 62 | push @hints, "Package gettext_dumb selects locale by environment only:"; | ||
| 63 | $locale = Locale::gettext_dumb::__get_locale(); | ||
| 64 | } else { | ||
| 65 | push @hints, "If setlocale() was called with empty string as second" | ||
| 66 | . " argument, the locale is selected from environment" | ||
| 67 | . " variables:"; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (exists $ENV{LANGUAGE} && length $ENV{LANGUAGE}) { | ||
| 71 | push @hints, " \$LANGUAGE is set to '$ENV{LANGUAGE}', wins."; | ||
| 72 | } elsif (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) { | ||
| 73 | push @hints, " \$LANGUAGE is not set."; | ||
| 74 | push @hints, " \$LC_ALL is set to '$ENV{LC_ALL}', wins."; | ||
| 75 | } elsif (exists $ENV{LANG} && length $ENV{LANG}) { | ||
| 76 | push @hints, " \$LANGUAGE and \$LC_ALL are not set."; | ||
| 77 | push @hints, " \$LANG is set to '$ENV{LANG}', wins."; | ||
| 78 | } elsif (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES}) { | ||
| 79 | push @hints, " \$LANGUAGE, \$LC_ALL, and \$LANG are not set."; | ||
| 80 | push @hints, " \$LC_MESSAGES is set to '$ENV{LC_MESSAGES}', wins."; | ||
| 81 | } else { | ||
| 82 | push @hints, " \$LANGUAGE, \$LC_ALL, \$LANG, and \$LC_MESSAGES are" | ||
| 83 | . " not set."; | ||
| 84 | push @hints, " falling back to 'C' aka 'POSIX'."; | ||
| 85 | } | ||
| 86 | |||
| 87 | my (undef, @locales) = | ||
| 88 | Locale::gettext_pp::__selected_locales($locale, | ||
| 89 | Locale::Messages::LC_MESSAGES(), | ||
| 90 | "LC_MESSAGES"); | ||
| 91 | |||
| 92 | my @dirs = ($dir); | ||
| 93 | my $default_dir = $Locale::gettext_pp::__gettext_pp_default_dir; | ||
| 94 | push @dirs, $default_dir if $default_dir && $dir ne $default_dir; | ||
| 95 | |||
| 96 | my @dirs = ($dir); | ||
| 97 | my ($tries, $lookup) = Locale::gettext_pp::__extend_locales(@locales); | ||
| 98 | |||
| 99 | my %seen; | ||
| 100 | my %loaded; | ||
| 101 | my @domains; | ||
| 102 | push @hints, "Searching catalogs ..."; | ||
| 103 | foreach my $basedir (@dirs) { | ||
| 104 | foreach my $try (@$tries) { | ||
| 105 | next if $loaded{$try}; | ||
| 106 | |||
| 107 | my $fulldir = File::Spec->catfile($basedir, $try, "LC_MESSAGES"); | ||
| 108 | next if $seen{$fulldir}++; | ||
| 109 | |||
| 110 | my $filename = File::Spec->catfile($fulldir, "$domainname.mo"); | ||
| 111 | |||
| 112 | my $domain = Locale::gettext_pp::__load_catalog($filename, $try); | ||
| 113 | if ($domain) { | ||
| 114 | push @hints, " $filename: successfully loaded."; | ||
| 115 | } else { | ||
| 116 | push @hints, " $filename: $!"; | ||
| 117 | next; | ||
| 118 | } | ||
| 119 | |||
| 120 | $loaded{$try} = 1; | ||
| 121 | |||
| 122 | $domain->{locale_id} = $lookup->{$try}; | ||
| 123 | push @domains, $domain; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | if (!@domains) { | ||
| 128 | push @hints, "No catalog found, giving up."; | ||
| 129 | } else { | ||
| 130 | foreach my $domain (@domains) { | ||
| 131 | if (defined $domain->{messages}->{$msgid}) { | ||
| 132 | my @trans = @{$domain->{messages}->{$msgid}}; | ||
| 133 | shift @trans; | ||
| 134 | push @hints, "$domain->{filename}: found translation: >>>$trans[0]<<<"; | ||
| 135 | last; | ||
| 136 | } else { | ||
| 137 | push @hints, "$domain->{filename}: msgid not found"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | return wantarray ? @hints : join "\n", @hints; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | __END__ |