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
25void
26print_available_languages()
27{
28	BMessage languages;
29	BLocaleRoster::Default()->GetAvailableLanguages(&languages);
30	BString language;
31	for (int i = 0; languages.FindString("language", i, &language) == B_OK;
32			i++) {
33		printf("%s.UTF-8\n", language.String());
34	}
35	printf("POSIX\n");
36}
37
38
39BString
40preferred_language()
41{
42	BMessage preferredLanguages;
43	BLocaleRoster::Default()->GetPreferredLanguages(&preferredLanguages);
44	const char* firstPreferredLanguage;
45	if (preferredLanguages.FindString("language", &firstPreferredLanguage)
46			!= B_OK) {
47		// Default to English
48		firstPreferredLanguage = "en";
49	}
50
51	return firstPreferredLanguage;
52}
53
54
55void
56print_formatting_conventions()
57{
58	BFormattingConventions conventions;
59	BLocale::Default()->GetFormattingConventions(&conventions);
60	if (conventions.CountryCode() != NULL) {
61		printf("%s_%s.UTF-8\n", conventions.LanguageCode(),
62			conventions.CountryCode());
63	} else {
64		printf("%s.UTF-8\n", conventions.LanguageCode());
65	}
66}
67
68
69void
70print_time_conventions()
71{
72	BFormattingConventions conventions;
73	BLocale::Default()->GetFormattingConventions(&conventions);
74	if (conventions.CountryCode() != NULL) {
75		printf("%s_%s.UTF-8%s\n", conventions.LanguageCode(),
76			conventions.CountryCode(),
77			conventions.UseStringsFromPreferredLanguage()
78				? "@strings=messages" : "");
79	} else {
80		printf("%s.UTF-8%s\n", conventions.LanguageCode(),
81			conventions.UseStringsFromPreferredLanguage()
82				? "@strings=messages" : "");
83	}
84}
85
86
87void
88usage(int status)
89{
90	printf("Usage: %s [-alftcm]\n"
91		"  -a, --all\t\tPrint all available languages\n"
92		"  -l, --language\tPrint the currently set preferred language\n"
93		"  -f, --format\t\tPrint the formatting-related locale\n"
94		"  -t, --time\t\tPrint the time-related locale\n"
95		"  -c, --message\t\tPrint the message-related locale\n"
96		"  -m, --charmap\t\tList available character maps\n"
97		"  -h, --help\t\tDisplay this help and exit\n",
98		kProgramName);
99
100	exit(status);
101}
102
103
104int
105main(int argc, char **argv)
106{
107	static struct option const longopts[] = {
108		{"all", no_argument, 0, 'a'},
109		{"language", no_argument, 0, 'l'},
110		{"format", no_argument, 0, 'f'},
111		{"time", no_argument, 0, 't'},
112		{"message", no_argument, 0, 'c'},
113		{"charmap", no_argument, 0, 'm'},
114		{"help", no_argument, 0, 'h'},
115		{NULL}
116	};
117
118	int c;
119	while ((c = getopt_long(argc, argv, "lcfmath", longopts, NULL)) != -1) {
120		switch (c) {
121			case 'l':
122				printf("%s\n", preferred_language().String());
123				break;
124			case 'f':
125				print_formatting_conventions();
126				break;
127			case 'c':	// for compatibility, we used to use 'c' for ctype
128				printf("%s.UTF-8\n", preferred_language().String());
129				break;
130			case 't':
131				print_time_conventions();
132				break;
133
134			// POSIX mandatory options
135			case 'm':
136				puts("UTF-8");
137				break;
138			case 'a':
139				print_available_languages();
140				break;
141			// TODO 'c', 'k'
142
143			case 'h':
144				usage(0);
145				break;
146			case 0:
147				break;
148			default:
149				usage(1);
150				break;
151		}
152	}
153
154	return 0;
155}
156