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 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 55 void 56 print_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 73 void 74 usage(int status) 75 { 76 printf("Usage: %s [-lftcm]\n" 77 " -l, --language\tPrint the currently set preferred language\n" 78 " -f, --format\t\tPrint the formatting-related locale\n" 79 " -t, --time\t\tPrint the time-related locale\n" 80 " -c, --message\t\tPrint the message-related locale\n" 81 " -m, --charmap\t\tList available character maps\n" 82 " -h, --help\t\tDisplay this help and exit\n", 83 kProgramName); 84 85 exit(status); 86 } 87 88 89 int 90 main(int argc, char **argv) 91 { 92 static struct option const longopts[] = { 93 {"language", no_argument, 0, 'l'}, 94 {"format", no_argument, 0, 'f'}, 95 {"time", no_argument, 0, 't'}, 96 {"message", no_argument, 0, 'c'}, 97 {"charmap", no_argument, 0, 'm'}, 98 {"help", no_argument, 0, 'h'}, 99 {NULL} 100 }; 101 102 int c; 103 while ((c = getopt_long(argc, argv, "lcfmth", longopts, NULL)) != -1) { 104 switch (c) { 105 case 'l': 106 printf("%s\n", preferred_language().String()); 107 break; 108 case 'f': 109 print_formatting_conventions(); 110 break; 111 case 'c': // for compatibility, we used to use 'c' for ctype 112 printf("%s.UTF-8\n", preferred_language().String()); 113 break; 114 case 't': 115 print_time_conventions(); 116 break; 117 118 // POSIX mandatory options 119 case 'm': 120 puts("UTF-8"); 121 break; 122 // TODO 'a', 'c', 'k' 123 124 case 'h': 125 usage(0); 126 break; 127 case 0: 128 break; 129 default: 130 usage(1); 131 break; 132 } 133 } 134 135 return 0; 136 } 137