xref: /freebsd/contrib/libxo/tests/gettext/gt_01.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 #include "xo_encoder.h"
25 
26 int
27 main (int argc, char **argv)
28 {
29     static char domainname[] = "gt_01";
30     static char path[MAXPATHLEN];
31     const char *tzone = "EST";
32     const char *lang = "pig_latin";
33 
34     argc = xo_parse_args(argc, argv);
35     if (argc < 0)
36 	return 1;
37 
38     for (argc = 1; argv[argc]; argc++) {
39 	if (xo_streq(argv[argc], "tz"))
40 	    tzone = argv[++argc];
41 	else if (xo_streq(argv[argc], "lang"))
42 	    lang = argv[++argc];
43 	else if (xo_streq(argv[argc], "po"))
44 	    strlcpy(path, argv[++argc], sizeof(path));
45     }
46 
47     setenv("LANG", lang, 1);
48     setenv("TZ", tzone, 1);
49 
50     if (path[0] == 0) {
51 	getcwd(path, sizeof(path));
52 	strlcat(path, "/po", sizeof(path));
53     }
54 
55     setlocale(LC_ALL, "");
56     bindtextdomain(domainname,  path);
57     bindtextdomain("ldns",  path);
58     bindtextdomain("strerror",  path);
59     textdomain(domainname);
60     tzset();
61 
62     xo_open_container("top");
63 
64     xo_emit("{G:}Your {qg:adjective} {g:noun} is {g:verb} {qg:owner} {g:target}\n",
65 	    "flaming", "sword", "burning", "my", "couch");
66 
67     xo_emit("{G:}The {qg:adjective} {g:noun} was {g:verb} {qg:owner} {g:target}\n",
68 	    "flaming", "sword", "burning", "my", "couch");
69 
70 
71     int i;
72     for (i = 0; i < 5; i++)
73 	xo_emit("{lw:bytes/%d}{Ngp:byte,bytes}\n", i);
74 
75     xo_emit("{G:}{L:total} {:total/%u}\n", 1234);
76 
77     xo_emit("{G:ldns}Received {:received/%zu} {Ngp:byte,bytes} "
78 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
79 	    (size_t) 1234, "foop", 4321, 32);
80 
81     xo_emit("{G:}Received {:received/%zu} {Ngp:byte,bytes} "
82 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
83 	    (size_t) 1234, "foop", 4321, 32);
84 
85     xo_emit("{G:/%s}Received {:received/%zu} {Ngp:byte,bytes} "
86 	    "from {:from/%s}#{:port/%d} in {:time/%d} ms\n",
87 	    "ldns", (size_t) 1234, "foop", 4321, 32);
88 
89     struct timeval tv;
90     tv.tv_sec = 1435085229;
91     tv.tv_usec = 123456;
92 
93     struct tm tm;
94     (void) gmtime_r(&tv.tv_sec, &tm);
95 
96     char date[64];
97     strftime(date, sizeof(date), "%+", &tm);
98 
99     xo_emit("{G:}Only {:marzlevanes/%d} {Ngp:marzlevane,marzlevanes} "
100 	    "are functioning correctly\n", 3);
101 
102     xo_emit("{G:}Version {:version} {:date}\n", "1.2.3", date);
103 
104     errno = EACCES;
105     xo_emit_warn("{G:}Unable to {g:verb/objectulate} forward velociping");
106     xo_emit_warn("{G:}{g:style/automatic} synchronization of {g:type/cardinal} "
107 		 "{g:target/grammeters} failed");
108     xo_emit("{G:}{Lwcg:hydrocoptic marzlevanes}{:marzlevanes/%d}\n", 6);
109 
110     xo_emit("{G:}{Lwcg:Windings}{g:windings}\n", "lotus-o-delta");
111 
112     xo_close_container("top");
113     xo_finish();
114 
115     return 0;
116 }
117