diff --git a/crates/open_jtalk/Cargo.toml b/crates/open_jtalk/Cargo.toml index b8e75b6..68dd17d 100644 --- a/crates/open_jtalk/Cargo.toml +++ b/crates/open_jtalk/Cargo.toml @@ -1,12 +1,16 @@ [package] name = "open_jtalk" -version = "0.1.24" +version = "0.1.25" edition = "2021" [dependencies] open_jtalk-sys = { path = "../open_jtalk-sys", version = "0.15.111" } thiserror = "1.0.31" +[target.'cfg(target_os = "windows")'.dependencies] +local-encoding = "0.2.0" +windows = { version = "0.39.0", features = ["Win32_Globalization"] } + [dev-dependencies] rstest = "0.12.0" pretty_assertions = "1.2.1" diff --git a/crates/open_jtalk/src/lib.rs b/crates/open_jtalk/src/lib.rs index a0b6251..47c6578 100644 --- a/crates/open_jtalk/src/lib.rs +++ b/crates/open_jtalk/src/lib.rs @@ -4,6 +4,9 @@ mod njd; mod resource; mod text2mecab; +#[cfg(target_os = "windows")] +mod win_api_helper; + pub use jpcommon::*; pub use mecab::*; pub use njd::*; diff --git a/crates/open_jtalk/src/mecab/mod.rs b/crates/open_jtalk/src/mecab/mod.rs index 8427dc6..11ead7f 100644 --- a/crates/open_jtalk/src/mecab/mod.rs +++ b/crates/open_jtalk/src/mecab/mod.rs @@ -37,7 +37,17 @@ impl Mecab { } pub fn load(&mut self, dic_dir: impl AsRef) -> bool { - let dic_dir = CString::new(dic_dir.as_ref().to_str().unwrap()).unwrap(); + let path_str = dic_dir.as_ref().to_str().unwrap(); + #[cfg(target_os = "windows")] + let dic_dir = { + if let Ok(s) = win_api_helper::str_to_local_multi_byte_string(path_str) { + s + } else { + return false; + } + }; + #[cfg(not(target_os = "windows"))] + let dic_dir = CString::new(path_str).unwrap(); unsafe { bool_number_to_bool(open_jtalk_sys::Mecab_load( self.as_raw_ptr(), diff --git a/crates/open_jtalk/src/win_api_helper.rs b/crates/open_jtalk/src/win_api_helper.rs new file mode 100644 index 0000000..830504c --- /dev/null +++ b/crates/open_jtalk/src/win_api_helper.rs @@ -0,0 +1,8 @@ +use std::ffi::CString; + +pub fn str_to_local_multi_byte_string(s: &str) -> Result { + let cp = unsafe { windows::Win32::Globalization::GetACP() }; + Ok(unsafe { + CString::from_vec_unchecked(local_encoding::windows::string_to_multibyte(cp, s, None)?) + }) +}