Skip to main content

pacenotes_installer\i18n/
string_table.rs

1/// The string `id` in the calling thread's UI language, or `<id>` when the
2/// table lacks it, so a forgotten translation shows instead of a blank control.
3pub fn load_string(id: u16) -> String {
4    unsafe {
5        let mut resource: *mut u16 = core::ptr::null_mut();
6        let point_into_resource = 0;
7        let length = windows_sys::Win32::UI::WindowsAndMessaging::LoadStringW(
8            instance(),
9            id as u32,
10            &mut resource as *mut *mut u16 as windows_sys::core::PWSTR,
11            point_into_resource,
12        );
13        if length <= 0 || resource.is_null() {
14            return format!("<{id}>");
15        }
16        String::from_utf16_lossy(core::slice::from_raw_parts(resource, length as usize))
17    }
18}
19
20/// The string `id` with its first `%s` replaced by `argument`; a template
21/// without one comes back unchanged.
22pub fn load_string_arg(id: u16, argument: &str) -> String {
23    let template = load_string(id);
24    match template.find("%s") {
25        Some(at) => format!("{}{argument}{}", &template[..at], &template[at + 2..]),
26        None => template,
27    }
28}
29
30/// This executable's module handle, where the resources live.
31pub fn instance() -> windows_sys::Win32::Foundation::HMODULE {
32    unsafe { windows_sys::Win32::System::LibraryLoader::GetModuleHandleW(core::ptr::null()) }
33}