xref: /freebsd/contrib/libxo/tests/gettext/gt_01.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*
2  * Copyright (c) 2015, Juniper Networks, Inc.
3  * All rights reserved.
4  * This SOFTWARE is licensed under the LICENSE provided in the
5  * ../Copyright file. By downloading, installing, copying, or otherwise
6  * using the SOFTWARE, you agree to be bound by the terms of that
7  * LICENSE.
8  * Phil Shafer, June 2015
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <ctype.h>
17 #include <time.h>
18 #include <sys/time.h>
19 #include <sys/param.h>
20 #include <locale.h>
21 #include <libintl.h>
22 
23 #include "xo.h"
24 
25 int
26 main (int argc, char **argv)
27 {
28     static char domainname[] = "gt_01";
29     static char path[MAXPATHLEN];
30     const char *tzone = "EST";
31     const char *lang = "pig_latin";
32 
33     argc = xo_parse_args(argc, argv);
34     if (argc < 0)
35 	return 1;
36 
37     for (argc = 1; argv[argc]; argc++) {
38 	if (strcmp(argv[argc], "tz") == 0)
39 	    tzone = argv[++argc];
40 	else if (strcmp(argv[argc], "lang") == 0)
41 	    lang = argv[++argc];
42 	else if (strcmp(argv[argc], "po") == 0)
43 	    strlcpy(path, argv[++argc], sizeof(path));
44     }
45 
46     setenv("LANG", lang, 1);
47     setenv("TZ", tzone, 1);
48 
49     if (path[0] == 0) {
50 	getcwd(path, sizeof(path));
51 	strlcat(path, "/po", sizeof(path));
52     }
53 
54     setlocale(LC_ALL, "");
55     bindtextdomain(domainname,  path);
56     bindtextdomain("ldns",  path);
57     bindtextdomain("strerror",  path);
58     textdomain(domainname);
59     tzset();
60 
61     xo_open_container("top");
62 
63     xo_emit("{G:}Your {qg:adjective} {g:noun} is {g:verb} {qg:owner} {g:target}\n",
64 	    "flaming", "sword", "burning", "my", "couch");
65 
66     xo_emit("{G:}The {qg:adjective} {g:noun} was {g:verb} {qg:owner} {g:target}\n",
67 	    "flaming", "sword", "burning", "my", "couch");
68 
69 
70     int i;
71     for (i = 0; i < 5; i++)
72 	xo_emit("{lw:bytes/%d}{Ngp:byte,bytes}\n", i);
73 
74     xo_emit("{G:}{L:total} {:total/%u}\n", 1234);
75 
76     xo_emit("{G:ldns}Received {:received/%zu} {Ngp:byte,bytes} "
77 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
78 	    (size_t) 1234, "foop", 4321, 32);
79 
80     xo_emit("{G:}Received {:received/%zu} {Ngp:byte,bytes} "
81 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
82 	    (size_t) 1234, "foop", 4321, 32);
83 
84     xo_emit("{G:/%s}Received {:received/%zu} {Ngp:byte,bytes} "
85 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
86 	    "ldns", (size_t) 1234, "foop", 4321, 32);
87 
88     struct timeval tv;
89     tv.tv_sec = 1435085229;
90     tv.tv_usec = 123456;
91 
92     struct tm tm;
93     (void) gmtime_r(&tv.tv_sec, &tm);
94 
95     char date[64];
96     strftime(date, sizeof(date), "%+", &tm);
97 
98     xo_emit("{G:}Only {:marzlevanes/%d} {Ngp:marzlevane,marzlevanes} "
99 	    "are functioning correctly\n", 3);
100 
101     xo_emit("{G:}Version {:version} {:date}\n", "1.2.3", date);
102 
103     errno = EACCES;
104     xo_emit_warn("{G:}Unable to {g:verb/objectulate} forward velociping");
105     xo_emit_warn("{G:}{g:style/automatic} synchronization of {g:type/cardinal} "
106 		 "{g:target/grammeters} failed");
107     xo_emit("{G:}{Lwcg:hydrocoptic marzlevanes}{:marzlevanes/%d}\n", 6);
108 
109     xo_emit("{G:}{Lwcg:Windings}{g:windings}\n", "lotus-o-delta");
110 
111     xo_close_container("top");
112     xo_finish();
113 
114     return 0;
115 }
116