xref: /haiku/src/bin/locale/locale.cpp (revision 0044a8c39ab5721051b6279506d1a8c511e20453)
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 
15 #include <getopt.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 
20 extern const char *__progname;
21 static const char *kProgramName = __progname;
22 
23 
24 const char*
25 preferred_language()
26 {
27 	BMessage preferredLanguages;
28 	BLocaleRoster::Default()->GetPreferredLanguages(&preferredLanguages);
29 	const char* firstPreferredLanguage;
30 	if (preferredLanguages.FindString("language", &firstPreferredLanguage)
31 			!= B_OK) {
32 		// Default to English
33 		firstPreferredLanguage = "en";
34 	}
35 
36 	return firstPreferredLanguage;
37 }
38 
39 
40 void
41 print_formatting_conventions()
42 {
43 	BFormattingConventions conventions;
44 	BLocale::Default()->GetFormattingConventions(&conventions);
45 	printf("%s_%s.UTF-8\n", conventions.LanguageCode(), conventions.CountryCode());
46 }
47 
48 
49 void
50 usage(int status)
51 {
52 	printf("Usage: %s [-lcf]\n"
53 		"  -l, --language\tPrint the currently set preferred language\n"
54 		"  -c, --ctype\t\tPrint the LC_CTYPE value based on the preferred language\n"
55 		"  -f, --format\t\tPrint the formatting convention language\n"
56 		"  -h, --help\t\tDisplay this help and exit\n",
57 		kProgramName);
58 
59 	exit(status);
60 }
61 
62 
63 int
64 main(int argc, char **argv)
65 {
66 	static struct option const longopts[] = {
67 		{"language", no_argument, 0, 'l'},
68 		{"ctype", no_argument, 0, 'c'},
69 		{"format", no_argument, 0, 'f'},
70 		{"help", no_argument, 0, 'h'},
71 		{NULL}
72 	};
73 
74 	int c;
75 	while ((c = getopt_long(argc, argv, "lcfh", longopts, NULL)) != -1) {
76 		switch (c) {
77 			case 'l':
78 				printf("%s\n", preferred_language());
79 				break;
80 			case 'c':
81 				printf("%s.UTF-8\n", preferred_language());
82 				break;
83 			case 'f':
84 				print_formatting_conventions();
85 				break;
86 			case 'h':
87 				usage(0);
88 				break;
89 			case 0:
90 				break;
91 			default:
92 				usage(1);
93 				break;
94 		}
95 	}
96 
97 	return 0;
98 }
99 
100