xref: /haiku/src/bin/locale/locale.cpp (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
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(), conventions.CountryCode());
47 }
48 
49 
50 void
51 usage(int status)
52 {
53 	printf("Usage: %s [-lcf]\n"
54 		"  -l, --language\tPrint the currently set preferred language\n"
55 		"  -c, --ctype\t\tPrint the LC_CTYPE value based on the preferred language\n"
56 		"  -f, --format\t\tPrint the formatting convention language\n"
57 		"  -h, --help\t\tDisplay this help and exit\n",
58 		kProgramName);
59 
60 	exit(status);
61 }
62 
63 
64 int
65 main(int argc, char **argv)
66 {
67 	static struct option const longopts[] = {
68 		{"language", no_argument, 0, 'l'},
69 		{"ctype", no_argument, 0, 'c'},
70 		{"format", no_argument, 0, 'f'},
71 		{"help", no_argument, 0, 'h'},
72 		{NULL}
73 	};
74 
75 	int c;
76 	while ((c = getopt_long(argc, argv, "lcfh", longopts, NULL)) != -1) {
77 		switch (c) {
78 			case 'l':
79 				printf("%s\n", preferred_language().String());
80 				break;
81 			case 'c':
82 				printf("%s.UTF-8\n", preferred_language().String());
83 				break;
84 			case 'f':
85 				print_formatting_conventions();
86 				break;
87 			case 'h':
88 				usage(0);
89 				break;
90 			case 0:
91 				break;
92 			default:
93 				usage(1);
94 				break;
95 		}
96 	}
97 
98 	return 0;
99 }
100 
101