2 * Copyright (c) 2010, Corinna Vinschen
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 extern char *__progname;
36 void usage (FILE *, int) __attribute__ ((noreturn));
39 usage (FILE * stream, int status)
43 "Print default locale or list of all supported locales\n"
47 " -a, --all List all available supported locales\n"
48 " -s, --system Print system default locale\n"
49 " (default is current user default locale)\n"
50 " -u, --utf Attach \".UTF-8\" to the result\n"
51 " -h, --help this text\n",
56 struct option longopts[] = {
57 {"all", no_argument, NULL, 'a'},
58 {"system", no_argument, NULL, 's'},
59 {"utf", no_argument, NULL, 'u'},
60 {"help", no_argument, NULL, 'h'},
61 {0, no_argument, NULL, 0}
63 const char *opts = "ahsu";
66 getlocale (LCID lcid, char *name)
72 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, 10))
74 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, 10);
75 sprintf (name, "%s%s%s", iso639, lcid > 0x3ff ? "_" : "",
76 lcid > 0x3ff ? iso3166 : "");
80 int main (int argc, char **argv)
83 LCID lcid = LOCALE_USER_DEFAULT;
88 setlocale (LC_ALL, "");
89 while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
96 lcid = LOCALE_SYSTEM_DEFAULT;
110 unsigned lang, sublang;
112 for (lang = 1; lang <= 0xff; ++lang)
116 wchar_t country[256];
121 for (sublang = 1; sublang <= 0x3f; ++sublang)
123 lcid = (sublang << 10) | lang;
124 if (getlocale (lcid, name))
127 wchar_t country[256];
132 /* Go figure. Even the English name of a language or
133 locale might contain native characters. */
134 GetLocaleInfoW (lcid, LOCALE_SENGLANGUAGE, lang, 256);
135 GetLocaleInfoW (lcid, LOCALE_SENGCOUNTRY, country, 256);
137 for (i = 0; i < lcnt; ++ i)
138 if (!wcscmp (loc_list[i].lang, lang)
139 && !wcscmp (loc_list[i].country, country))
145 wcscpy (loc_list[lcnt].lang, lang);
146 wcscpy (loc_list[lcnt].country, country);
148 /* Now check certain conditions to figure out if that
149 locale requires a modifier. */
150 c = stpcpy (loc, name);
151 if (wcsstr (lang, L"(Latin)")
152 && (!strncmp (loc, "sr_", 3)
153 || !strcmp (loc, "be_BY")))
154 stpcpy (c, "@latin");
155 else if (wcsstr (lang, L"(Cyrillic)")
156 && !strcmp (loc, "uz_UZ"))
157 stpcpy (c, "@cyrillic");
158 /* Avoid more dups */
159 for (i = 0; i < lcnt; ++ i)
160 if (!strcmp (loc_list[i].loc, loc))
168 strcpy (loc_list[lcnt++].loc, loc);
170 printf ("%-16s %ls (%ls)\n", loc, lang, country);
171 /* Check for locales which sport a modifier for
172 changing the codeset and other stuff. */
173 if (!strcmp (loc, "tt_RU"))
174 stpcpy (c, "@iqtelif");
175 else if (GetLocaleInfoW (lcid, LOCALE_SINTLSYMBOL, wbuf, 9)
176 && !wcsncmp (wbuf, L"EUR", 3))
178 else if (!strncmp (loc, "ja_", 3)
179 || !strncmp (loc, "ko_", 3)
180 || !strncmp (loc, "zh_", 3))
181 stpcpy (c, "@cjknarrow");
184 printf ("%-16s %ls (%ls)\n", loc, lang, country);
190 if (getlocale (lcid, name))
191 printf ("%s%s\n", name, utf);