| 1 |
use std::ffi::CStr; |
| 2 |
use std::os::raw::c_char; |
| 3 |
use std::os::raw::c_int; |
| 4 |
|
| 5 |
use super::Locale; |
| 6 |
|
| 7 |
// Bind some emscripten functions. Copied from webplatform crate. |
| 8 |
extern "C" { |
| 9 |
pub fn emscripten_asm_const_int(s: *const c_char, ...) -> c_int; |
| 10 |
} |
| 11 |
|
| 12 |
pub fn system_locale() -> Option<Locale> { |
| 13 |
const JS: &'static [u8] = b"\ |
| 14 |
try { \ |
| 15 |
return allocate(intArrayFromString(navigator.languages.join(',')), 'i8', ALLOC_STACK); \ |
| 16 |
} catch(e) {} \ |
| 17 |
try { \ |
| 18 |
return allocate(intArrayFromString(navigator.language), 'i8', ALLOC_STACK); \ |
| 19 |
} catch(e) {} \ |
| 20 |
try { \ |
| 21 |
return allocate(intArrayFromString(navigator.userLanguage), 'i8', ALLOC_STACK); \ |
| 22 |
} catch(e) {} \ |
| 23 |
return 0;\0"; |
| 24 |
unsafe { |
| 25 |
let cptr = emscripten_asm_const_int(&JS[0] as *const _ as *const c_char); |
| 26 |
return CStr::from_ptr(cptr as *const c_char).to_str().ok() |
| 27 |
.and_then(|s| Locale::new(s).ok()); |
| 28 |
} |
| 29 |
} |