feat(os): add `locale` API

Ref https://github.com/tauri-apps/tauri/pull/5960
pull/391/head
Lucas Nogueira 2 years ago
parent f3f92a6ff6
commit 06b4468e12
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

11
Cargo.lock generated

@ -4895,6 +4895,16 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sys-locale"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0b9eefabb91675082b41eb94c3ecd91af7656caee3fb4961a07c0ec8c7ca6f"
dependencies = [
"libc",
"windows-sys 0.45.0",
]
[[package]]
name = "system-deps"
version = "6.0.4"
@ -5271,6 +5281,7 @@ dependencies = [
"os_info",
"serde",
"serde_json",
"sys-locale",
"tauri",
"thiserror",
]

@ -12,3 +12,4 @@ tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
os_info = "3"
sys-locale = "0.3"

@ -123,5 +123,22 @@ async function tempdir(): Promise<string> {
return invoke("plugin:os|tempdir");
}
export { EOL, platform, version, type, arch, tempdir };
/**
* Returns a String with a `BCP-47` language tag inside. If the locale couldnt be obtained, `null` is returned instead.
* @example
* ```typescript
* import { locale } from '@tauri-apps/plugin-os';
* const locale = await locale();
* if (locale) {
* // use the locale string here
* }
* ```
*
* @since 1.3.0
*/
async function locale(): Promise<string | null> {
return invoke("plugin:os|locale");
}
export { EOL, platform, version, type, arch, tempdir, locale };
export type { Platform, OsType, Arch };

@ -28,3 +28,8 @@ pub fn arch() -> &'static str {
pub fn tempdir() -> PathBuf {
crate::tempdir()
}
#[tauri::command]
pub fn locale() -> Option<String> {
crate::locale()
}

@ -68,6 +68,11 @@ pub fn tempdir() -> PathBuf {
std::env::temp_dir()
}
/// Returns the locale with the `BCP-47` language tag. If the locale couldnt be obtained, `None` is returned instead.
pub fn locale() -> Option<String> {
sys_locale::get_locale()
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("os")
.invoke_handler(tauri::generate_handler![
@ -75,7 +80,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
commands::version,
commands::kind,
commands::arch,
commands::tempdir
commands::tempdir,
commands::locale
])
.build()
}

Loading…
Cancel
Save