Debian Codegraph @

debian/missing-sources/locale_config/src/win32.rs

raw
1 extern crate winapi;
2
3 use super::{LanguageRange,Locale};
4
5 use std::fmt::Write;
6
7 fn if_locale_info_differs<F: FnOnce(&str)>(lc_type: winapi::ctypes::c_ulong, func: F) {
8 #[allow(non_snake_case)] // would be const if it wasn't for the fact const functions are still unstable
9 let LOCALE_NAME_USER_DEFAULT: *mut u16 = ::std::ptr::null_mut();
10 const LOCALE_NOUSEROVERRIDE: winapi::ctypes::c_ulong = 0x80000000;
11 let mut buf_user = [0u16; 86];
12 let mut buf_def = [0u16; 86];
13 let len_user = unsafe {
14 winapi::um::winnls::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, lc_type,
15 buf_user.as_mut_ptr(), buf_user.len() as winapi::ctypes::c_long)
16 };
17 let len_def = unsafe {
18 winapi::um::winnls::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, lc_type | LOCALE_NOUSEROVERRIDE,
19 buf_def.as_mut_ptr(), buf_user.len() as winapi::ctypes::c_long)
20 };
21 if buf_user[0..(len_user as usize - 1)] != buf_def[0..(len_def as usize - 1)] {
22 let s = &*String::from_utf16_lossy(&buf_user[0..(len_user as usize - 1)]);
23 func(&*s);
24 }
25 }
26
27 #[allow(dead_code)]
28 mod consts {
29 use super::winapi::ctypes::c_ulong;
30 // Locale information types from winnls.h
31 pub const LOCALE_ILANGUAGE: c_ulong = 0x0001;
32 pub const LOCALE_SLANGUAGE: c_ulong = 0x0002;
33 pub const LOCALE_SENGLANGUAGE: c_ulong = 0x1001;
34 pub const LOCALE_SENGLISHLANGUAGENAME: c_ulong = 0x1001;
35 pub const LOCALE_SABBREVLANGNAME: c_ulong = 0x0003;
36 pub const LOCALE_SNATIVELANGNAME: c_ulong = 0x0004;
37 pub const LOCALE_SNATIVELANGUAGENAME: c_ulong = 0x0004;
38 pub const LOCALE_ICOUNTRY: c_ulong = 0x0005;
39 pub const LOCALE_SCOUNTRY: c_ulong = 0x0006;
40 pub const LOCALE_SLOCALIZEDCOUNTRYNAME: c_ulong = 0x0006;
41 pub const LOCALE_SENGCOUNTRY: c_ulong = 0x1002;
42 pub const LOCALE_SENGLISHCOUNTRYNAME: c_ulong = 0x1002;
43 pub const LOCALE_SABBREVCTRYNAME: c_ulong = 0x0007;
44 pub const LOCALE_SNATIVECTRYNAME: c_ulong = 0x0008;
45 pub const LOCALE_SNATIVECOUNTRYNAME: c_ulong = 0x0008;
46 pub const LOCALE_IDEFAULTLANGUAGE: c_ulong = 0x0009;
47 pub const LOCALE_IDEFAULTCOUNTRY: c_ulong = 0x000A;
48 pub const LOCALE_IDEFAULTCODEPAGE: c_ulong = 0x000B;
49 pub const LOCALE_IDEFAULTANSICODEPAGE: c_ulong = 0x1004;
50 pub const LOCALE_IDEFAULTMACCODEPAGE: c_ulong = 0x1011;
51 pub const LOCALE_SLIST: c_ulong = 0x000C;
52 pub const LOCALE_IMEASURE: c_ulong = 0x000D;
53 pub const LOCALE_SDECIMAL: c_ulong = 0x000E;
54 pub const LOCALE_STHOUSAND: c_ulong = 0x000F;
55 pub const LOCALE_SGROUPING: c_ulong = 0x0010;
56 pub const LOCALE_IDIGITS: c_ulong = 0x0011;
57 pub const LOCALE_ILZERO: c_ulong = 0x0012;
58 pub const LOCALE_INEGNUMBER: c_ulong = 0x1010;
59 pub const LOCALE_SNATIVEDIGITS: c_ulong = 0x0013;
60 pub const LOCALE_SCURRENCY: c_ulong = 0x0014;
61 pub const LOCALE_SINTLSYMBOL: c_ulong = 0x0015;
62 pub const LOCALE_SMONDECIMALSEP: c_ulong = 0x0016;
63 pub const LOCALE_SMONTHOUSANDSEP: c_ulong = 0x0017;
64 pub const LOCALE_SMONGROUPING: c_ulong = 0x0018;
65 pub const LOCALE_ICURRDIGITS: c_ulong = 0x0019;
66 pub const LOCALE_IINTLCURRDIGITS: c_ulong = 0x001A;
67 pub const LOCALE_ICURRENCY: c_ulong = 0x001B;
68 pub const LOCALE_INEGCURR: c_ulong = 0x001C;
69 pub const LOCALE_SDATE: c_ulong = 0x001D;
70 pub const LOCALE_STIME: c_ulong = 0x001E;
71 pub const LOCALE_SSHORTDATE: c_ulong = 0x001F;
72 pub const LOCALE_SLONGDATE: c_ulong = 0x0020;
73 pub const LOCALE_STIMEFORMAT: c_ulong = 0x1003;
74 pub const LOCALE_IDATE: c_ulong = 0x0021;
75 pub const LOCALE_ILDATE: c_ulong = 0x0022;
76 pub const LOCALE_ITIME: c_ulong = 0x0023;
77 pub const LOCALE_ITIMEMARKPOSN: c_ulong = 0x1005;
78 pub const LOCALE_ICENTURY: c_ulong = 0x0024;
79 pub const LOCALE_ITLZERO: c_ulong = 0x0025;
80 pub const LOCALE_IDAYLZERO: c_ulong = 0x0026;
81 pub const LOCALE_IMONLZERO: c_ulong = 0x0027;
82 pub const LOCALE_S1159: c_ulong = 0x0028;
83 pub const LOCALE_S2359: c_ulong = 0x0029;
84 pub const LOCALE_ICALENDARTYPE: c_ulong = 0x1009;
85 pub const LOCALE_IOPTIONALCALENDAR: c_ulong = 0x100B;
86 pub const LOCALE_IFIRSTDAYOFWEEK: c_ulong = 0x100C;
87 pub const LOCALE_IFIRSTWEEKOFYEAR: c_ulong = 0x100D;
88 pub const LOCALE_SDAYNAME1: c_ulong = 0x002A;
89 pub const LOCALE_SDAYNAME2: c_ulong = 0x002B;
90 pub const LOCALE_SDAYNAME3: c_ulong = 0x002C;
91 pub const LOCALE_SDAYNAME4: c_ulong = 0x002D;
92 pub const LOCALE_SDAYNAME5: c_ulong = 0x002E;
93 pub const LOCALE_SDAYNAME6: c_ulong = 0x002F;
94 pub const LOCALE_SDAYNAME7: c_ulong = 0x0030;
95 pub const LOCALE_SABBREVDAYNAME1: c_ulong = 0x0031;
96 pub const LOCALE_SABBREVDAYNAME2: c_ulong = 0x0032;
97 pub const LOCALE_SABBREVDAYNAME3: c_ulong = 0x0033;
98 pub const LOCALE_SABBREVDAYNAME4: c_ulong = 0x0034;
99 pub const LOCALE_SABBREVDAYNAME5: c_ulong = 0x0035;
100 pub const LOCALE_SABBREVDAYNAME6: c_ulong = 0x0036;
101 pub const LOCALE_SABBREVDAYNAME7: c_ulong = 0x0037;
102 pub const LOCALE_SMONTHNAME1: c_ulong = 0x0038;
103 pub const LOCALE_SMONTHNAME2: c_ulong = 0x0039;
104 pub const LOCALE_SMONTHNAME3: c_ulong = 0x003A;
105 pub const LOCALE_SMONTHNAME4: c_ulong = 0x003B;
106 pub const LOCALE_SMONTHNAME5: c_ulong = 0x003C;
107 pub const LOCALE_SMONTHNAME6: c_ulong = 0x003D;
108 pub const LOCALE_SMONTHNAME7: c_ulong = 0x003E;
109 pub const LOCALE_SMONTHNAME8: c_ulong = 0x003F;
110 pub const LOCALE_SMONTHNAME9: c_ulong = 0x0040;
111 pub const LOCALE_SMONTHNAME10: c_ulong = 0x0041;
112 pub const LOCALE_SMONTHNAME11: c_ulong = 0x0042;
113 pub const LOCALE_SMONTHNAME12: c_ulong = 0x0043;
114 pub const LOCALE_SMONTHNAME13: c_ulong = 0x100E;
115 pub const LOCALE_SABBREVMONTHNAME1: c_ulong = 0x0044;
116 pub const LOCALE_SABBREVMONTHNAME2: c_ulong = 0x0045;
117 pub const LOCALE_SABBREVMONTHNAME3: c_ulong = 0x0046;
118 pub const LOCALE_SABBREVMONTHNAME4: c_ulong = 0x0047;
119 pub const LOCALE_SABBREVMONTHNAME5: c_ulong = 0x0048;
120 pub const LOCALE_SABBREVMONTHNAME6: c_ulong = 0x0049;
121 pub const LOCALE_SABBREVMONTHNAME7: c_ulong = 0x004A;
122 pub const LOCALE_SABBREVMONTHNAME8: c_ulong = 0x004B;
123 pub const LOCALE_SABBREVMONTHNAME9: c_ulong = 0x004C;
124 pub const LOCALE_SABBREVMONTHNAME10: c_ulong = 0x004D;
125 pub const LOCALE_SABBREVMONTHNAME11: c_ulong = 0x004E;
126 pub const LOCALE_SABBREVMONTHNAME12: c_ulong = 0x004F;
127 pub const LOCALE_SABBREVMONTHNAME13: c_ulong = 0x100F;
128 pub const LOCALE_SPOSITIVESIGN: c_ulong = 0x0050;
129 pub const LOCALE_SNEGATIVESIGN: c_ulong = 0x0051;
130 pub const LOCALE_IPOSSIGNPOSN: c_ulong = 0x0052;
131 pub const LOCALE_INEGSIGNPOSN: c_ulong = 0x0053;
132 pub const LOCALE_IPOSSYMPRECEDES: c_ulong = 0x0054;
133 pub const LOCALE_IPOSSEPBYSPACE: c_ulong = 0x0055;
134 pub const LOCALE_INEGSYMPRECEDES: c_ulong = 0x0056;
135 pub const LOCALE_INEGSEPBYSPACE: c_ulong = 0x0057;
136 pub const LOCALE_FONTSIGNATURE: c_ulong = 0x0058;
137 pub const LOCALE_SISO639LANGNAME: c_ulong = 0x0059;
138 pub const LOCALE_SISO3166CTRYNAME: c_ulong = 0x005A;
139 pub const LOCALE_IGEOID: c_ulong = 0x005B;
140 pub const LOCALE_SNAME: c_ulong = 0x005C;
141 pub const LOCALE_SDURATION: c_ulong = 0x005D;
142 pub const LOCALE_SKEYBOARDSTOINSTALL: c_ulong = 0x005E;
143 pub const LOCALE_SSHORTESTDAYNAME1: c_ulong = 0x0060;
144 pub const LOCALE_SSHORTESTDAYNAME2: c_ulong = 0x0061;
145 pub const LOCALE_SSHORTESTDAYNAME3: c_ulong = 0x0062;
146 pub const LOCALE_SSHORTESTDAYNAME4: c_ulong = 0x0063;
147 pub const LOCALE_SSHORTESTDAYNAME5: c_ulong = 0x0064;
148 pub const LOCALE_SSHORTESTDAYNAME6: c_ulong = 0x0065;
149 pub const LOCALE_SSHORTESTDAYNAME7: c_ulong = 0x0066;
150 pub const LOCALE_SISO639LANGNAME2: c_ulong = 0x0067;
151 pub const LOCALE_SISO3166CTRYNAME2: c_ulong = 0x0068;
152 pub const LOCALE_SNAN: c_ulong = 0x0069;
153 pub const LOCALE_SPOSINFINITY: c_ulong = 0x006A;
154 pub const LOCALE_SNEGINFINITY: c_ulong = 0x006B;
155 pub const LOCALE_SSCRIPTS: c_ulong = 0x006C;
156 pub const LOCALE_SPARENT: c_ulong = 0x006D;
157 pub const LOCALE_SCONSOLEFALLBACKNAME: c_ulong = 0x006E;
158 pub const LOCALE_SLANGDISPLAYNAME: c_ulong = 0x006F;
159 pub const LOCALE_SLOCALIZEDLANGUAGENAME: c_ulong = 0x006F;
160 pub const LOCALE_IREADINGLAYOUT: c_ulong = 0x0070;
161 pub const LOCALE_INEUTRAL: c_ulong = 0x0071;
162 pub const LOCALE_SENGLISHDISPLAYNAME: c_ulong = 0x0072;
163 pub const LOCALE_SNATIVEDISPLAYNAME: c_ulong = 0x0073;
164 pub const LOCALE_INEGATIVEPERCENT: c_ulong = 0x0074;
165 pub const LOCALE_IPOSITIVEPERCENT: c_ulong = 0x0075;
166 pub const LOCALE_SPERCENT: c_ulong = 0x0076;
167 pub const LOCALE_SPERMILLE: c_ulong = 0x0077;
168 pub const LOCALE_SMONTHDAY: c_ulong = 0x0078;
169 pub const LOCALE_SSHORTTIME: c_ulong = 0x0079;
170 pub const LOCALE_SOPENTYPELANGUAGETAG: c_ulong = 0x007A;
171 pub const LOCALE_SSORTLOCALE: c_ulong = 0x007B;
172
173 pub const LOCALE_IDEFAULTEBCDICCODEPAGE: c_ulong = 0x1012;
174 pub const LOCALE_IPAPERSIZE: c_ulong = 0x100A;
175 pub const LOCALE_SENGCURRNAME: c_ulong = 0x1007;
176 pub const LOCALE_SNATIVECURRNAME: c_ulong = 0x1008;
177 pub const LOCALE_SYEARMONTH: c_ulong = 0x1006;
178 pub const LOCALE_SSORTNAME: c_ulong = 0x1013;
179 pub const LOCALE_IDIGITSUBSTITUTION: c_ulong = 0x1014;
180 } // mod consts
181
182 use self::consts::*;
183
184 fn get_user_default_locale() -> super::Result<LanguageRange<'static>> {
185 let mut buf = [0u16; 85];
186 let len = unsafe {
187 winapi::um::winnls::GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as i32)
188 };
189 if len > 0 {
190 let mut s = String::from_utf16_lossy(&buf[..(len as usize - 1)]);
191
192 {
193 // First collect parameters that have representation in -u extension as defined in
194 // Unicode TR35§3.6:
195 let mut u_ext = String::new();
196
197 // cf
198 // Negative monetary format, Region and Language/Customize Format/Currency (LOCALE_INEGCURR):
199 // We only distinguish between parenthesized format (-u-cf-account) and using minus sign
200 // (-u-cf-standard).
201 // Note: the formats are numbered in rather random order, so the numbers corresponding to
202 // parenthesized formats are 0, 4, 14 and 15.
203 if_locale_info_differs(LOCALE_INEGCURR, |s| {
204 u_ext.push_str(match s { "0"|"4"|"14"|"15" => "-cf-account", _ => "-cf-standard" });
205 });
206
207 // fw
208 // Start-of-week, Region and Language/Formats and Region and Language/Customize
209 // Formats/Date (LOCALE_IFIRSTDAYOFWEEK):
210 if_locale_info_differs(LOCALE_IFIRSTDAYOFWEEK, |s| {
211 u_ext.push_str(match s { "0" => "-fw-mon", "1" => "-fw-tue", "2" => "-fw-wed", "3" => "-fw-thu", "4" => "-fw-fri", "5" => "-fw-sat", "6" => "-fw-sun", _ => "" });
212 });
213
214 // hc
215 // Time formats, Region and Language/Formats and Region and Language/Customize Formats/Time
216 // (LOCALE_SSHORTTIME + LOCALE_STIMEFORMAT):
217 // We only use the hour-cycle here
218 // NOTE: Only ‘h’ and ‘H’ actually appear.
219 // TODO: Verify the hour-cycles in Windows actually match the Unicode TR35
220 // TODO: Might also detect whether to use leading zeroes.
221 if_locale_info_differs(LOCALE_STIMEFORMAT, |s| {
222 if s.contains('h') {
223 u_ext.push_str("-hc-h12"); // 1–12
224 } else if s.contains('H') {
225 u_ext.push_str("-hc-h23"); // 0–23
226 } else if s.contains('K') {
227 u_ext.push_str("-hc-h11"); // 0–11
228 } else if s.contains('k') {
229 u_ext.push_str("-hc-h24"); // 1–24
230 }
231 });
232
233 // ms
234 // Measurement system, Region and Language/Customize Format/Numbers:
235 if_locale_info_differs(LOCALE_IMEASURE, |s| {
236 u_ext.push_str(match s { "0" => "-ms-metric", "1" => "-ms-ussystem", _ => "" });
237 });
238
239 // nu
240 // Standard digits, Region and Language/Customize Format/Numbers:
241 // TODO: We should probably take into account the LOCALE_IDIGITSUBSTITUTION and set the
242 // digits if either LOCALE_SNATIVEDIGITS or LOCALE_IDIGITSUBSTITUTION is not default AND
243 // LOCALE_IDIGITSUBSTITUTION is not 1 (0 = contextual, 1 = use latin, 2 = use specified).
244 // I am not sure we will implement the contextual logic too.
245 if_locale_info_differs(LOCALE_SNATIVEDIGITS, |s| {
246 u_ext.push_str(match s.as_ref() {
247 // basic plane numeric numberingSystems from CLDR
248 "٠١٢٣٤٥٦٧٨٩" => "-nu-arab",
249 "۰۱۲۳۴۵۶۷۸۹" => "-nu-arabext",
250 "᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙" => "-nu-bali",
251 "০১২৩৪৫৬৭৮৯" => "-nu-beng",
252 "꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙" => "-nu-cham",
253 "०१२३४५६७८९" => "-nu-deva",
254 "0123456789" => "-nu-fullwide",
255 "૦૧૨૩૪૫૬૭૮૯" => "-nu-gujr",
256 "੦੧੨੩੪੫੬੭੮੯" => "-nu-guru",
257 "〇一二三四五六七八九" => "-nu-hanidec",
258 "꧐꧑꧒꧓꧔꧕꧖꧗꧘꧙" => "-nu-java",
259 "꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉" => "-nu-kali",
260 "០១២៣៤៥៦៧៨៩" => "-nu-khmr",
261 "೦೧೨೩೪೫೬೭೮೯" => "-nu-knda",
262 "᪀᪁᪂᪃᪄᪅᪆᪇᪈᪉" => "-nu-lana",
263 "᪐᪑᪒᪓᪔᪕᪖᪗᪘᪙" => "-nu-lanatham",
264 "໐໑໒໓໔໕໖໗໘໙" => "-nu-laoo",
265 "0123456789" => "-nu-latn",
266 "᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉" => "-nu-lepc",
267 "᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏" => "-nu-limb",
268 "൦൧൨൩൪൫൬൭൮൯" => "-nu-mlym",
269 "᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙" => "-nu-mong",
270 "꯰꯱꯲꯳꯴꯵꯶꯷꯸꯹" => "-nu-mtei",
271 "၀၁၂၃၄၅၆၇၈၉" => "-nu-mymr",
272 "႐႑႒႓႔႕႖႗႘႙" => "-nu-mymrshan",
273 "߀߁߂߃߄߅߆߇߈߉" => "-nu-nkoo",
274 "᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙" => "-nu-olck",
275 "୦୧୨୩୪୫୬୭୮୯" => "-nu-orya",
276 "꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙" => "-nu-saur",
277 "᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹" => "-nu-sund",
278 "᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙" => "-nu-talu",
279 "௦௧௨௩௪௫௬௭௮௯" => "-nu-tamldec",
280 "౦౧౨౩౪౫౬౭౮౯" => "-nu-telu",
281 "๐๑๒๓๔๕๖๗๘๙" => "-nu-thai",
282 "༠༡༢༣༤༥༦༧༨༩" => "-nu-tibt",
283 "꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩" => "-nu-vaii",
284 // I don't think Windows can configure anything else, but just in case
285 _ => "",
286 })
287 });
288
289 // Append the extensions, if any
290 if !u_ext.is_empty() {
291 s.push_str("-u");
292 s.push_str(&*u_ext);
293 }
294 }
295 {
296 // Then collect other parameters that we want to represent using custom -x extension.
297 let mut x_ext = String::new();
298
299 // df
300 // Date formats, Region and Language/Formats and Region and Language/Customize
301 // Formats/Date (LOCALE_SSHORTDATE + LOCALE_SLONGDATE):
302 // The full format is too long and complex for the tag format, so we only interpret ISO
303 // format. TODO: might also detect swapped order and perhaps abbrev name vs. number for
304 // short format.
305 if_locale_info_differs(LOCALE_SSHORTDATE, |s| {
306 if s == "yyyy-MM-dd" {
307 x_ext.push_str("-df-iso")
308 }
309 });
310
311 // dm
312 // Monetary decimal separator, Region and Language/Customize Format/Currency:
313 if_locale_info_differs(LOCALE_SMONDECIMALSEP, |s| {
314 x_ext.push_str("-dm");
315 for c in s.chars() {
316 write!(&mut x_ext, "-{:04x}", c as u32).unwrap(); // shouldn't fail unless OOM
317 }
318 });
319
320 // ds
321 // Decimal separator, Region and Language/Customize Format/Numbers:
322 if_locale_info_differs(LOCALE_SDECIMAL, |s| {
323 x_ext.push_str("-ds");
324 for c in s.chars() {
325 write!(&mut x_ext, "-{:04x}", c as u32).unwrap(); // shouldn't fail unless OOM
326 }
327 });
328
329 // gm
330 // Monetary group separator, Region and Language/Customize Format/Currency:
331 if_locale_info_differs(LOCALE_SMONTHOUSANDSEP, |s| {
332 x_ext.push_str("-gm");
333 for c in s.chars() {
334 write!(&mut x_ext, "-{:04x}", c as u32).unwrap(); // shouldn't fail unless OOM
335 }
336 });
337
338 // gs
339 // Group separator, Region and Language/Customize Format/Numbers:
340 if_locale_info_differs(LOCALE_STHOUSAND, |s| {
341 x_ext.push_str("-gs");
342 for c in s.chars() {
343 write!(&mut x_ext, "-{:04x}", c as u32).unwrap(); // shouldn't fail unless OOM
344 }
345 });
346
347 // ls
348 // List separator, Region and Language/Customize Format/Numbers:
349 if_locale_info_differs(LOCALE_SLIST, |s| {
350 x_ext.push_str("-ls");
351 for c in s.chars() {
352 write!(&mut x_ext, "-{:04x}", c as u32).unwrap(); // shouldn't fail unless OOM
353 }
354 });
355
356 // Append the extensions, if any
357 if !x_ext.is_empty() {
358 s.push_str("-x");
359 s.push_str(&*x_ext);
360 }
361 }
362
363 // TODO: Below are the settings we don't handle, but might want to.
364 // ** Region and Language/Location:
365 // - TODO: Location - LOCALE_IGEOID does not seem to indicate it; not sure what does.
366 // ** Region and Language/Customize Format/Numbers:
367 // - TODO: Grouping — LOCALE_SGROUPING — not sure how much I really want to deal with
368 // this.
369 // - TODO: Negative sign — LOCALE_SNEGATIVESIGN — not sure this makes sense. CLDR has
370 // U+002D HYPHEN-MINUS almost everywhere with some U+2212 MINUS SIGN thrown in the mix
371 // rather randomly, one clearly nonsensical U+2013 EN DASH and handful of RTL/LTR marks
372 // that we should use according to script (or not at all; sure digits and +/- have sane
373 // RTL flags).
374 // - NOTE: Positive sign does not seem to be configurable and for en-GB even returns
375 // somewhat absurd value of “”.
376 // - TODO: Negative number format — Unicode TR35 only has this for currency (separate
377 // setting) and CLDR does not have any exceptions from the default of prefix-with-minus
378 // for decimal formats (only for currency formats).
379 // - TODO: Leading zeroes — LOCALE_ILZERO — whether to use .7 or 0.7.
380 // ** Region and Language/Customize Format/Currency:
381 // - TODO: Positive monetary format — LOCALE_ICURRENCY: Currency symbol can precede or
382 // succeed the value and can be separated by space or not, but we need to design
383 // representation for it in the -x extension space.
384 // - TODO: Negative monetary format — LOCALE_INEGCURR: We only handle distinction between
385 // parenthesized and regular with minus sign, so possibly design representation for the
386 // other options too.
387 // - TODO: Monetary grouping — LOCALE_SGROUPING — like grouping above, not sure how much
388 // I really want to deal with this.
389 // ** Region and Language/Customize Format/Time:
390 // - TODO: Possibly handle other kinds of formats than just distinguishing 12 and 23 hour
391 // formats we do above.
392 // - TODO: Custom AM and PM flags.
393 // ** Region and Language/Customize Format/Date:
394 // - TODO: Possibly handle other kinds of formats than just distinguishing the ISO-8601
395 // one we do above.
396 // - Note: Windows don't seem to support any other calendar than Gregorian; makes it
397 // easier for us.
398
399 // NOTE: Below are the settings we don't handle and I don't think we should.
400 // ** Region and Language/Customize Format/Numbers:
401 // - Number of digits after decimal — not used! For general numbers it indicates accuracy,
402 // so it must be application's choice and for monetary we always have explicit currency
403 // and use appropriate value for that.
404 // ** Region and Language/Customize Format/Currency:
405 // - Currency symbol — since there is no way to set the actual currency, just the symbol,
406 // we can't use it as our monetary formatting is based on explicit currency.
407 // - Monetary decimal digits — we always use the value for specified currency, not the
408 // local value.
409
410 return LanguageRange::new(&*s).map(|x| x.into_static());
411 }
412 // TODO: Fall back to GetUserDefaultLCID and/or GetLocaleInfoW
413 // GetLocaleInfo(Ex) with LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME might be of some
414 // utility too
415 return Err(super::Error::NotWellFormed);
416 }
417
418 fn get_system_default_locale() -> super::Result<LanguageRange<'static>> {
419 let mut buf = [0u16; 85];
420 let len = unsafe {
421 winapi::um::winnls::GetSystemDefaultLocaleName(buf.as_mut_ptr(), buf.len() as i32)
422 };
423 if len > 0 {
424 let s = String::from_utf16_lossy(&buf[..(len as usize - 1)]);
425 return LanguageRange::new(&*s).map(|x| x.into_static());
426 }
427 // TODO: Fall back to GetUserDefaultLCID and/or GetLocaleInfoW
428 // GetLocaleInfo(Ex) with LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME might be of some
429 // utility too
430 return Err(super::Error::NotWellFormed);
431 }
432
433 const MUI_LANGUAGE_NAME: winapi::ctypes::c_ulong = 0x8; // from winnls.h
434
435 fn get_user_preferred_languages() -> Vec<LanguageRange<'static>> {
436 let mut buf = [0u16; 5 * 85 + 1];
437 let mut n_langs = 0;
438 let mut len = buf.len() as winapi::ctypes::c_ulong;
439 let res = unsafe {
440 winapi::um::winnls::GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &mut n_langs, buf.as_mut_ptr(), &mut len)
441 };
442 if res != 0 && len > 1 {
443 let s = String::from_utf16_lossy(&buf[..(len as usize - 2)]);
444 return s.split('\0')
445 .filter_map(|x| LanguageRange::new(x).ok())
446 .map(|x| x.into_static())
447 .collect();
448 }
449 return Vec::new();
450 }
451
452 // User default language is the primary language. If user preferred UI languages returns anything, the
453 // first item is considered override for messages and the rest is fallbacks. And last fallback is
454 // system default language.
455 pub fn system_locale() -> Option<Locale> {
456 let mut loc =
457 if let Ok(lr) = get_user_default_locale() {
458 Some(Locale::from(lr))
459 } else {
460 None
461 };
462 let mut msg_locs = get_user_preferred_languages();
463 if !msg_locs.is_empty() {
464 if loc.is_none() {
465 loc = Some(Locale::from(msg_locs.remove(0)));
466 } else {
467 loc.as_mut().unwrap().add_category("messages", &msg_locs.remove(0));
468 }
469 debug_assert!(!loc.is_none());
470 for l in msg_locs {
471 loc.as_mut().unwrap().add(&l);
472 }
473 }
474 if let Ok(lr) = get_system_default_locale() {
475 if loc.is_none() {
476 loc = Some(Locale::from(lr));
477 } else {
478 loc.as_mut().unwrap().add(&lr);
479 }
480 }
481 loc
482 }