xref: /haiku/src/bin/locale/locale.cpp (revision 7ee53ed3bd2222305c93a4959f8c587c373ed97c)
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 
21 extern const char *__progname;
22 static const char *kProgramName = __progname;
23 
24 
25 BString
26 preferred_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 
41 void
42 print_formatting_conventions()
43 {
44 	BFormattingConventions conventions;
45 	BLocale::Default()->GetFormattingConventions(&conventions);
46 	printf("%s_%s.UTF-8\n", conventions.LanguageCode(),
47 		conventions.CountryCode());
48 }
49 
50 
51 void
52 print_time_conventions()
53 {
54 	BFormattingConventions conventions;
55 	BLocale::Default()->GetFormattingConventions(&conventions);
56 	if (conventions.UseStringsFromPreferredLanguage()) {
57 		printf("%s_%s.UTF-8@strings=messages\n", conventions.LanguageCode(),
58 			conventions.CountryCode());
59 	} else {
60 		printf("%s_%s.UTF-8\n", conventions.LanguageCode(),
61 			conventions.CountryCode());
62 	}
63 }
64 
65 
66 void
67 usage(int status)
68 {
69 	printf("Usage: %s [-lfmt]\n"
70 		"  -l, --language\tPrint the currently set preferred language\n"
71 		"  -f, --format\t\tPrint the formatting-related locale\n"
72 		"  -m, --message\t\tPrint the message-related locale\n"
73 		"  -t, --time\t\tPrint the time-related locale\n"
74 		"  -h, --help\t\tDisplay this help and exit\n",
75 		kProgramName);
76 
77 	exit(status);
78 }
79 
80 
81 int
82 main(int argc, char **argv)
83 {
84 	static struct option const longopts[] = {
85 		{"language", no_argument, 0, 'l'},
86 		{"format", no_argument, 0, 'f'},
87 		{"message", no_argument, 0, 'm'},
88 		{"time", no_argument, 0, 't'},
89 		{"help", no_argument, 0, 'h'},
90 		{NULL}
91 	};
92 
93 	int c;
94 	while ((c = getopt_long(argc, argv, "lcfmth", longopts, NULL)) != -1) {
95 		switch (c) {
96 			case 'l':
97 				printf("%s\n", preferred_language().String());
98 				break;
99 			case 'f':
100 				print_formatting_conventions();
101 				break;
102 			case 'c':	// for compatibility, we used to use 'c' for ctype
103 			case 'm':
104 				printf("%s.UTF-8\n", preferred_language().String());
105 				break;
106 			case 't':
107 				print_time_conventions();
108 				break;
109 			case 'h':
110 				usage(0);
111 				break;
112 			case 0:
113 				break;
114 			default:
115 				usage(1);
116 				break;
117 		}
118 	}
119 
120 	return 0;
121 }
122