1/*
2 * Copyright 2011, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		Ryan Leavengood, leavengood@gmail.com
7 */
8
9
10#include <FormattingConventions.h>
11#include <Locale.h>
12#include <LocaleRoster.h>
13#include <Message.h>
14#include <String.h>
15
16#include <getopt.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20
21extern const char *__progname;
22static const char *kProgramName = __progname;
23
24
25BString
26preferred_language()
27{
28	BMessage preferredLanguages;
29	BLocaleRoster::Default()->GetPreferredLanguages(&preferredLanguages);
30	const char* firstPreferredLanguage;
31	if (preferredLanguages.FindString("language", &firstPreferredLanguage)
32			!= B_OK) {
33		// Default to English
34		firstPreferredLanguage = "en";
35	}
36
37	return firstPreferredLanguage;
38}
39
40
41void
42print_formatting_conventions()
43{
44	BFormattingConventions conventions;
45	BLocale::Default()->GetFormattingConventions(&conventions);
46	if (conventions.CountryCode() != NULL) {
47		printf("%s_%s.UTF-8\n", conventions.LanguageCode(),
48			conventions.CountryCode());
49	} else {
50		printf("%s.UTF-8\n", conventions.LanguageCode());
51	}
52}
53
54
55void
56print_time_conventions()
57{
58	BFormattingConventions conventions;
59	BLocale::Default()->GetFormattingConventions(&conventions);
60	if (conventions.CountryCode() != NULL) {
61		printf("%s_%s.UTF-8%s\n", conventions.LanguageCode(),
62			conventions.CountryCode(),
63			conventions.UseStringsFromPreferredLanguage()
64				? "@strings=messages" : "");
65	} else {
66		printf("%s.UTF-8%s\n", conventions.LanguageCode(),
67			conventions.UseStringsFromPreferredLanguage()
68				? "@strings=messages" : "");
69	}
70}
71
72
73void
74usage(int status)
75{
76	printf("Usage: %s [-lfmt]\n"
77		"  -l, --language\tPrint the currently set preferred language\n"
78		"  -f, --format\t\tPrint the formatting-related locale\n"
79		"  -m, --message\t\tPrint the message-related locale\n"
80		"  -t, --time\t\tPrint the time-related locale\n"
81		"  -h, --help\t\tDisplay this help and exit\n",
82		kProgramName);
83
84	exit(status);
85}
86
87
88int
89main(int argc, char **argv)
90{
91	static struct option const longopts[] = {
92		{"language", no_argument, 0, 'l'},
93		{"format", no_argument, 0, 'f'},
94		{"message", no_argument, 0, 'm'},
95		{"time", no_argument, 0, 't'},
96		{"help", no_argument, 0, 'h'},
97		{NULL}
98	};
99
100	int c;
101	while ((c = getopt_long(argc, argv, "lcfmth", longopts, NULL)) != -1) {
102		switch (c) {
103			case 'l':
104				printf("%s\n", preferred_language().String());
105				break;
106			case 'f':
107				print_formatting_conventions();
108				break;
109			case 'c':	// for compatibility, we used to use 'c' for ctype
110			case 'm':
111				printf("%s.UTF-8\n", preferred_language().String());
112				break;
113			case 't':
114				print_time_conventions();
115				break;
116			case 'h':
117				usage(0);
118				break;
119			case 0:
120				break;
121			default:
122				usage(1);
123				break;
124		}
125	}
126
127	return 0;
128}
129