Skip to content

Commit 359058c

Browse files
authored
feat(api): add locale function, closes #5939 (#5960)
1 parent 45330e3 commit 359058c

File tree

10 files changed

+68
-6
lines changed

10 files changed

+68
-6
lines changed

.changes/api-js-os-locale.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch'
3+
---
4+
5+
Add `locale` function in the `os` module to get the system locale.

.changes/api-rs-os-locale.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": "patch"
3+
---
4+
5+
Add `tauri::api::os::locale` function to get the system locale.

core/tauri/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ infer = { version = "0.9", optional = true }
8585
png = { version = "0.17", optional = true }
8686
ico = { version = "0.2.0", optional = true }
8787
encoding_rs = "0.8.31"
88+
sys-locale = { version = "0.2.3", optional = true }
8889

8990
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
9091
rfd = { version = "0.10", optional = true, features = [ "gtk3", "common-controls-v6" ] }
@@ -142,6 +143,7 @@ updater = [
142143
]
143144
http-api = [ "attohttpc" ]
144145
http-multipart = [ "attohttpc/multipart-form", "reqwest/multipart" ]
146+
os-api = [ "sys-locale" ]
145147
shell-open-api = [ "open", "regex", "tauri-macros/shell-scope" ]
146148
fs-extract-api = [ "zip" ]
147149
reqwest-client = [ "reqwest", "bytes" ]
@@ -213,7 +215,7 @@ global-shortcut-all = [ "global-shortcut" ]
213215
http-all = [ "http-request" ]
214216
http-request = [ "http-api" ]
215217
notification-all = [ "notification", "dialog-ask" ]
216-
os-all = [ "os_info" ]
218+
os-all = [ "os_info", "os-api" ]
217219
path-all = [ ]
218220
process-all = [ "process-relaunch", "process-exit" ]
219221
process-exit = [ ]

core/tauri/scripts/bundle.global.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub mod file;
1313
#[cfg_attr(doc_cfg, doc(cfg(feature = "http-api")))]
1414
pub mod http;
1515
pub mod ipc;
16+
#[cfg(feature = "os-api")]
17+
#[cfg_attr(doc_cfg, doc(cfg(feature = "os-api")))]
18+
pub mod os;
1619
pub mod path;
1720
pub mod process;
1821
#[cfg(feature = "shell-open-api")]

core/tauri/src/api/os.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
//! Types and functions related to operating system operations.
6+
7+
/// Returns `Some(String)` with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `None` is returned instead.
8+
pub fn locale() -> Option<String> {
9+
sys_locale::get_locale()
10+
}

core/tauri/src/endpoints/operating_system.rs

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Cmd {
2020
OsType,
2121
Arch,
2222
Tempdir,
23+
Locale,
2324
}
2425

2526
#[cfg(os_all)]
@@ -43,6 +44,10 @@ impl Cmd {
4344
fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
4445
Ok(std::env::temp_dir())
4546
}
47+
48+
fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
49+
Ok(crate::api::os::locale())
50+
}
4651
}
4752

4853
#[cfg(not(os_all))]
@@ -66,6 +71,10 @@ impl Cmd {
6671
fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
6772
Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
6873
}
74+
75+
fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
76+
Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
77+
}
6978
}
7079

7180
#[cfg(os_all)]
@@ -110,4 +119,8 @@ mod tests {
110119
#[tauri_macros::module_command_test(os_all, "os > all", runtime)]
111120
#[quickcheck_macros::quickcheck]
112121
fn tempdir() {}
122+
123+
#[tauri_macros::module_command_test(os_all, "os > all", runtime)]
124+
#[quickcheck_macros::quickcheck]
125+
fn locale() {}
113126
}

core/tauri/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//! - **reqwest-client**: Uses `reqwest` as HTTP client on the `http` APIs. Improves performance, but increases the bundle size.
2626
//! - **native-tls-vendored**: Compile and statically link to a vendored copy of OpenSSL (applies to the default HTTP client).
2727
//! - **reqwest-native-tls-vendored**: Compile and statically link to a vendored copy of OpenSSL (applies to the `reqwest` HTTP client).
28+
//! - **os-api**: Enables the [`api::os`] module.
2829
//! - **process-command-api**: Enables the [`api::process::Command`] APIs.
2930
//! - **global-shortcut**: Enables the global shortcut APIs.
3031
//! - **clipboard**: Enables the clipboard APIs.
@@ -897,6 +898,7 @@ mod tests {
897898
"fs-extract-api",
898899
"http-api",
899900
"http-multipart",
901+
"os-api",
900902
"process-command-api",
901903
"process-relaunch-dangerous-allow-symlink-macos",
902904
"window-data-url",

tooling/api/docs/js-api.json

+1-1
Large diffs are not rendered by default.

tooling/api/src/os.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,27 @@ async function tempdir(): Promise<string> {
160160
})
161161
}
162162

163-
export { EOL, platform, version, type, arch, tempdir }
163+
/**
164+
* Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead.
165+
* @example
166+
* ```typescript
167+
* import { locale } from '@tauri-apps/api/os';
168+
* const locale = await locale();
169+
* if (locale) {
170+
* // use the locale string here
171+
* }
172+
* ```
173+
*
174+
* @since 1.3.0
175+
*/
176+
async function locale(): Promise<string | null> {
177+
return invokeTauriCommand<string>({
178+
__tauriModule: 'Os',
179+
message: {
180+
cmd: 'locale'
181+
}
182+
})
183+
}
184+
185+
export { EOL, platform, version, type, arch, tempdir, locale }
164186
export type { Platform, OsType, Arch }

0 commit comments

Comments
 (0)