| 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)] |
| 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 |
|
| 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 |
} |
| 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 |
|
| 194 |
6: |
| 195 |
let mut u_ext = String::new(); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
appear. |
| 219 |
|
| 220 |
|
| 221 |
if_locale_info_differs(LOCALE_STIMEFORMAT, |s| { |
| 222 |
if s.contains('h') { |
| 223 |
u_ext.push_str("-hc-h12"); 12 |
| 224 |
} else if s.contains('H') { |
| 225 |
u_ext.push_str("-hc-h23"); 23 |
| 226 |
} else if s.contains('K') { |
| 227 |
u_ext.push_str("-hc-h11"); 11 |
| 228 |
} else if s.contains('k') { |
| 229 |
u_ext.push_str("-hc-h24"); 24 |
| 230 |
} |
| 231 |
}); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if_locale_info_differs(LOCALE_IMEASURE, |s| { |
| 236 |
u_ext.push_str(match s { "0" => "-ms-metric", "1" => "-ms-ussystem", _ => "" }); |
| 237 |
}); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ied). |
| 244 |
|
| 245 |
if_locale_info_differs(LOCALE_SNATIVEDIGITS, |s| { |
| 246 |
u_ext.push_str(match s.as_ref() { |
| 247 |
|
| 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 |
|
| 285 |
_ => "", |
| 286 |
}) |
| 287 |
}); |
| 288 |
|
| 289 |
|
| 290 |
if !u_ext.is_empty() { |
| 291 |
s.push_str("-u"); |
| 292 |
s.push_str(&*u_ext); |
| 293 |
} |
| 294 |
} |
| 295 |
{ |
| 296 |
|
| 297 |
let mut x_ext = String::new(); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 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 |
|
| 312 |
|
| 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(); |
| 317 |
} |
| 318 |
}); |
| 319 |
|
| 320 |
|
| 321 |
|
| 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(); |
| 326 |
} |
| 327 |
}); |
| 328 |
|
| 329 |
|
| 330 |
|
| 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(); |
| 335 |
} |
| 336 |
}); |
| 337 |
|
| 338 |
|
| 339 |
|
| 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(); |
| 344 |
} |
| 345 |
}); |
| 346 |
|
| 347 |
|
| 348 |
|
| 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(); |
| 353 |
} |
| 354 |
}); |
| 355 |
|
| 356 |
|
| 357 |
if !x_ext.is_empty() { |
| 358 |
s.push_str("-x"); |
| 359 |
s.push_str(&*x_ext); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
with |
| 368 |
|
| 369 |
has |
| 370 |
x |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
”. |
| 376 |
te |
| 377 |
|
| 378 |
|
| 379 |
0.7. |
| 380 |
|
| 381 |
or |
| 382 |
|
| 383 |
|
| 384 |
en |
| 385 |
|
| 386 |
|
| 387 |
much |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
y, |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
l, |
| 406 |
|
| 407 |
he |
| 408 |
|
| 409 |
|
| 410 |
return LanguageRange::new(&*s).map(|x| x.into_static()); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 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 |
|
| 428 |
|
| 429 |
|
| 430 |
return Err(super::Error::NotWellFormed); |
| 431 |
} |
| 432 |
|
| 433 |
const MUI_LANGUAGE_NAME: winapi::ctypes::c_ulong = 0x8; |
| 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 |
|
| 453 |
|
| 454 |
|
| 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 |
} |