1 /* ngettext - retrieve plural form strings from message catalog and print them.
2 Copyright (C) 1995-1997, 2000-2007, 2012, 2015 Free Software
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "relocatable.h"
34 #include "propername.h"
37 #define HAVE_SETLOCALE 1
38 /* Make sure we use the included libintl, not the system's one. */
40 #include "libgnuintl.h"
42 #define _(str) gettext (str)
45 static const struct option long_options[] =
47 { "domain", required_argument, NULL, 'd' },
48 { "env", required_argument, NULL, '=' },
49 { "help", no_argument, NULL, 'h' },
50 { "version", no_argument, NULL, 'V' },
54 /* Forward declaration of local functions. */
55 static void usage (int __status)
56 #if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2)
57 __attribute__ ((noreturn))
62 main (int argc, char *argv[])
66 const char *msgid_plural;
70 /* Default values for command line options. */
72 bool do_version = false;
73 bool environ_changed = false;
74 const char *domain = getenv ("TEXTDOMAIN");
75 const char *domaindir = getenv ("TEXTDOMAINDIR");
77 /* Set program name for message texts. */
78 set_program_name (argv[0]);
81 /* Set locale via LC_ALL. */
82 setlocale (LC_ALL, "");
85 /* Set the text message domain. */
86 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
89 /* Ensure that write errors on stdout are detected. */
90 atexit (close_stdout);
92 /* Parse command line options. */
93 while ((optchar = getopt_long (argc, argv, "+d:hV", long_options, NULL))
97 case '\0': /* Long option. */
110 /* Undocumented option --env sets an environment variable. */
111 char *separator = strchr (optarg, '=');
112 if (separator != NULL)
115 xsetenv (optarg, separator + 1, 1);
116 environ_changed = true;
122 usage (EXIT_FAILURE);
125 #ifdef HAVE_SETLOCALE
127 /* Set locale again via LC_ALL. */
128 setlocale (LC_ALL, "");
131 /* Version information is requested. */
134 printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
135 /* xgettext: no-wrap */
136 printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
137 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
138 This is free software: you are free to change and redistribute it.\n\
139 There is NO WARRANTY, to the extent permitted by law.\n\
141 "1995-1997, 2000-2006");
142 printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper"));
146 /* Help is requested. */
148 usage (EXIT_SUCCESS);
150 /* More optional command line options. */
151 if (argc - optind <= 2)
152 error (EXIT_FAILURE, 0, _("missing arguments"));
154 /* Now the mandatory command line options. */
155 msgid = argv[optind++];
156 msgid_plural = argv[optind++];
158 /* If no domain name is given we print the original string.
159 We mark this assigning NULL to domain. */
160 if (domain == NULL || domain[0] == '\0')
163 /* Bind domain to appropriate directory. */
164 if (domaindir != NULL && domaindir[0] != '\0')
165 bindtextdomain (domain, domaindir);
167 /* To speed up the plural-2 test, we accept more than one COUNT in one
169 while (optind < argc)
171 count = argv[optind++];
175 unsigned long tmp_val;
178 tmp_val = strtoul (count, &endp, 10);
179 if (errno == 0 && count[0] != '\0' && endp[0] == '\0')
182 /* When COUNT is not valid, use plural. */
186 /* If no domain name is given we don't translate, and we use English
187 plural form handling. */
189 fputs (n == 1 ? msgid : msgid_plural, stdout);
191 /* Write out the result. */
192 fputs (dngettext (domain, msgid, msgid_plural, n), stdout);
199 /* Display usage information and exit. */
203 if (status != EXIT_SUCCESS)
204 fprintf (stderr, _("Try '%s --help' for more information.\n"),
208 /* xgettext: no-wrap */
210 Usage: %s [OPTION] MSGID MSGID-PLURAL COUNT...\n\
211 -d, --domain=TEXTDOMAIN retrieve translated message from TEXTDOMAIN\n\
212 -h, --help display this help and exit\n\
213 -V, --version display version information and exit\n\
214 MSGID MSGID-PLURAL translate MSGID (singular) / MSGID-PLURAL (plural)\n\
215 COUNT choose singular/plural form based on this value\n"),
217 /* xgettext: no-wrap */
220 If the TEXTDOMAIN parameter is not given, the domain is determined from the\n\
221 environment variable TEXTDOMAIN. If the message catalog is not found in the\n\
222 regular directory, another location can be specified with the environment\n\
223 variable TEXTDOMAINDIR.\n\
224 Standard search directory: %s\n"), LOCALEDIR);
225 /* TRANSLATORS: The placeholder indicates the bug-reporting address
226 for this package. Please add _another line_ saying
227 "Report translation bugs to <...>\n" with the address for translation
228 bugs (typically your translation team's web or email address). */
229 fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"), stdout);