Debian Codegraph @

script/maketext

raw
1 #! /usr/bin/perl -w
2 # Command-line interface to Locale::Maketext::Gettext (and Locale::Maketext)
3
4 # Copyright (c) 2003-2021 imacat. All rights reserved. This program is free
5 # software; you can redistribute it and/or modify it under the same terms
6 # as Perl itself.
7 # First written: 2003/5/3
8
9 use 5.008;
10 use strict;
11 use warnings;
12 use Getopt::Long qw(GetOptions);
13 use Locale::Maketext::Gettext::Functions;
14 our $VERSION = 0.06;
15 # Prototype declaration
16 sub main();
17 sub parse_args();
18
19 our ($THIS_FILE, $SHORT_HELP, $VER_STR, $SEARCH, $HELP);
20 $THIS_FILE = $0;
21 $THIS_FILE =~ s/^.*\///;
22 $SHORT_HELP = "Try `$THIS_FILE --help' for more information.";
23 $VER_STR = "$THIS_FILE v$VERSION by imacat <imacat\@mail.imacat.idv.tw>";
24 $SEARCH = join " ", @Locale::Maketext::Gettext::Functions::SYSTEM_LOCALEDIRS;
25 $HELP = << "EOT";
26 Usage: maketext [OPTION] [--domain=TEXTDOMAIN] MSGKEY [PARAM...]
27 or: maketext [OPTION] -s MSGKEY [PARAM...]
28
29 Maketext and display native language translation of a textual message.
30
31 -d, --domain=TEXTDOMAIN retrieve translated messages from TEXTDOMAIN
32 -h, --help display this help and exit
33 -V, --version display version information and exit
34 MSGKEY [PARAM...] retrieve translated message corresponding
35 to MSGKEY from TEXTDOMAIN
36
37 If the TEXTDOMAIN parameter is not given, the domain is determined from the
38 environment variable TEXTDOMAIN. If the message catalog is not found in the
39 regular directory, another location can be specified with the environment
40 variable TEXTDOMAINDIR.
41 When used with the -s option the program adds a new line to the end of the
42 output so that it behaves like the `echo' or the `gettext' command.
43 Standard search directories: $SEARCH
44
45 Report bugs to <imacat\@mail.imacat.idv.tw>.
46 EOT
47
48 our ($DOMAIN, $LOCALEDIR, $ECHO, $KEY, @PARAM);
49 $ECHO = 0;
50
51 # Main program
52 main();
53 exit 0;
54
55 # Main program
56 sub main() {
57 local ($_, %_);
58
59 # Parse the arguments
60 parse_args();
61
62 bindtextdomain($DOMAIN, $LOCALEDIR)
63 if defined $DOMAIN && defined $LOCALEDIR;
64 textdomain($DOMAIN) if defined $DOMAIN;
65 print maketext($KEY, @PARAM);
66 print "\n" if $ECHO;
67
68 return;
69 }
70
71 # Parse the arguments
72 sub parse_args() {
73 local ($_, %_);
74
75 # Get the arguments
76 $_ = eval {
77 local $SIG{__WARN__} = sub { die $_[0]; };
78 Getopt::Long::Configure("no_auto_abbrev");
79 GetOptions( "domain|d=s"=>\$DOMAIN,
80 "s"=>sub { $ECHO = 1; },
81 "help|h"=>sub { print $HELP; exit 0; },
82 "version|V"=>sub { print "$VER_STR\n"; exit 0; });
83 return 1;
84 };
85 die "$THIS_FILE: $@" if !defined $_;
86
87 # The MSGKEY
88 die "$THIS_FILE: missing arguments\n" if @ARGV == 0;
89 $KEY = shift @ARGV;
90 @PARAM = @ARGV;
91
92 # Set the locale directory
93 $LOCALEDIR = $ENV{"TEXTDOMAINDIR"} if exists $ENV{"TEXTDOMAINDIR"};
94 # Set the text domain
95 $DOMAIN = $ENV{"TEXTDOMAIN"}
96 if !defined $DOMAIN && exists $ENV{"TEXTDOMAIN"};
97
98 return;
99 }
100
101 __END__
102
103 =head1 NAME
104
105 maketext - translate and make messages
106
107 =head1 SYNOPSIS
108
109 maketext [OPTION] [--domain=TEXTDOMAIN] MSGKEY [PARAM...]
110 maketext [OPTION] -s MSGKEY [PARAM...]
111
112 =head1 DESCRIPTION
113
114 The C<maketext> script translates a natural language message into
115 the user's language, by looking up the translation in a message MO
116 file, and process the plural transformation with Maketext.
117
118 The C<maketext> script is a command-line interface to
119 L<Locale::Maketext::Gettext(3)|Locale::Maketext::Gettext/3> (and
120 L<Locale::Maketext(3)|Locale::Maketext/3>). It can be used in shell
121 scripts, etc, to translate, maketext and return the result. By this
122 way, it enables Maketext to be integrated into other programming
123 languages/systems, like bash/csh, python, PHP, C, etc. It works
124 like the command-line program gettext.
125
126 For example:
127
128 % maketext -s "[*,_1,virus was,viruses were] found in [*,_2,file,files]." 0 1
129 0 viruses were found in 1 file.
130 % maketext -s "[*,_1,virus was,viruses were] found in [*,_2,file,files]." 1 3
131 1 virus was found in 3 files.
132 %
133
134 =head1 OPTIONS
135
136 =over
137
138 =item -d,--domain=TEXTDOMAIN
139
140 Retrieve translated messages from TEXTDOMAIN.
141
142 =item -s
143
144 Adds a new line to the end of the output so that it behaves like the
145 `echo' or the `gettext' command.
146
147 =item -h,--help
148
149 Display the help messages.
150
151 =item -V,--version
152
153 Display version information and exit.
154
155 =item MSGKEY
156
157 The original text used to look up translated text.
158
159 =item PARAM...
160
161 Parameters to Maketext for the plural and other text functions.
162
163 =back
164
165 =head1 ENVIRONMENT
166
167 =over
168
169 =item TEXTDOMAIN
170
171 TEXTDOMAIN is used to determine the text domain when the -d
172 parameter is not given.
173
174 =item TEXTDOMAINDIR
175
176 TEXTDOMAINDIR is used to search the message catalog/MO file if it
177 does not reside in the system locale directories.
178
179 =back
180
181 =head1 NOTES
182
183 Maketext language function override, like C<quant> or C<numerate>, is
184 not available here. Suggestions are welcome.
185
186 The current system locale directory search order is:
187 /usr/share/locale, /usr/lib/locale, /usr/local/share/locale,
188 /usr/local/lib/locale. Suggestions are welcome.
189
190 =head1 BUGS
191
192 Report bugs to imacat <[email protected]>
193
194 =head1 SEE ALSO
195
196 L<Locale::Maketext(3)|Locale::Maketext/3>,
197 L<Locale::Maketext::TPJ13(3)|Locale::Maketext::TPJ13/3>,
198 L<Locale::Maketext::Gettext(3)|Locale::Maketext::Gettext/3>,
199 L<Locale::Maketext::Gettext::Functions(3)|Locale::Maketext::Gettext::Functions/3>,
200 L<bindtextdomain(3)|bindtextdomain/3>, L<textdomain(3)|textdomain/3>.
201 Also, please refer to the official GNU gettext manual at
202 L<https://www.gnu.org/software/gettext/manual/>.
203
204 =head1 AUTHOR
205
206 imacat <[email protected]>
207
208 =head1 COPYRIGHT
209
210 Copyright (c) 2003-2021 imacat. All rights reserved. This program is free
211 software; you can redistribute it and/or modify it under the same terms
212 as Perl itself.
213
214 =cut