mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-23 11:36:13 +02:00
feat(os): add locale API (#391)
This commit is contained in:
committed by
GitHub
parent
5676e77209
commit
7e3034904c
Generated
+11
@@ -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"
|
||||
|
||||
@@ -127,5 +127,22 @@ async function tempdir(): Promise<string> {
|
||||
return window.__TAURI_INVOKE__("plugin:os|tempdir");
|
||||
}
|
||||
|
||||
export { EOL, platform, version, type, arch, tempdir };
|
||||
/**
|
||||
* Returns a String with a `BCP-47` language tag inside. If the locale couldn’t 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 2.0.0
|
||||
*/
|
||||
async function locale(): Promise<string | null> {
|
||||
return window.__TAURI_INVOKE__("plugin:os|locale");
|
||||
}
|
||||
|
||||
export { EOL, platform, version, type, arch, tempdir, locale };
|
||||
export type { Platform, OsType, Arch };
|
||||
|
||||
@@ -1 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.tempdir=async function(){return window.__TAURI_INVOKE__("plugin:os|tempdir")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|kind")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}
|
||||
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.tempdir=async function(){return window.__TAURI_INVOKE__("plugin:os|tempdir")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|kind")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}
|
||||
|
||||
@@ -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 couldn’t 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")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
@@ -76,7 +81,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
commands::version,
|
||||
commands::kind,
|
||||
commands::arch,
|
||||
commands::tempdir
|
||||
commands::tempdir,
|
||||
commands::locale
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user