Skip to content

Commit

Permalink
feat(api): add locale function, closes #5939 (#5960)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored May 23, 2023
1 parent 45330e3 commit 359058c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/api-js-os-locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch'
---

Add `locale` function in the `os` module to get the system locale.
5 changes: 5 additions & 0 deletions .changes/api-rs-os-locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch"
---

Add `tauri::api::os::locale` function to get the system locale.
4 changes: 3 additions & 1 deletion core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ infer = { version = "0.9", optional = true }
png = { version = "0.17", optional = true }
ico = { version = "0.2.0", optional = true }
encoding_rs = "0.8.31"
sys-locale = { version = "0.2.3", optional = true }

[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
rfd = { version = "0.10", optional = true, features = [ "gtk3", "common-controls-v6" ] }
Expand Down Expand Up @@ -142,6 +143,7 @@ updater = [
]
http-api = [ "attohttpc" ]
http-multipart = [ "attohttpc/multipart-form", "reqwest/multipart" ]
os-api = [ "sys-locale" ]
shell-open-api = [ "open", "regex", "tauri-macros/shell-scope" ]
fs-extract-api = [ "zip" ]
reqwest-client = [ "reqwest", "bytes" ]
Expand Down Expand Up @@ -213,7 +215,7 @@ global-shortcut-all = [ "global-shortcut" ]
http-all = [ "http-request" ]
http-request = [ "http-api" ]
notification-all = [ "notification", "dialog-ask" ]
os-all = [ "os_info" ]
os-all = [ "os_info", "os-api" ]
path-all = [ ]
process-all = [ "process-relaunch", "process-exit" ]
process-exit = [ ]
Expand Down
6 changes: 3 additions & 3 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/tauri/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub mod file;
#[cfg_attr(doc_cfg, doc(cfg(feature = "http-api")))]
pub mod http;
pub mod ipc;
#[cfg(feature = "os-api")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "os-api")))]
pub mod os;
pub mod path;
pub mod process;
#[cfg(feature = "shell-open-api")]
Expand Down
10 changes: 10 additions & 0 deletions core/tauri/src/api/os.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! Types and functions related to operating system operations.
/// Returns `Some(String)` with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `None` is returned instead.
pub fn locale() -> Option<String> {
sys_locale::get_locale()
}
13 changes: 13 additions & 0 deletions core/tauri/src/endpoints/operating_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Cmd {
OsType,
Arch,
Tempdir,
Locale,
}

#[cfg(os_all)]
Expand All @@ -43,6 +44,10 @@ impl Cmd {
fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
Ok(std::env::temp_dir())
}

fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
Ok(crate::api::os::locale())
}
}

#[cfg(not(os_all))]
Expand All @@ -66,6 +71,10 @@ impl Cmd {
fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
}

fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
}
}

#[cfg(os_all)]
Expand Down Expand Up @@ -110,4 +119,8 @@ mod tests {
#[tauri_macros::module_command_test(os_all, "os > all", runtime)]
#[quickcheck_macros::quickcheck]
fn tempdir() {}

#[tauri_macros::module_command_test(os_all, "os > all", runtime)]
#[quickcheck_macros::quickcheck]
fn locale() {}
}
2 changes: 2 additions & 0 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//! - **reqwest-client**: Uses `reqwest` as HTTP client on the `http` APIs. Improves performance, but increases the bundle size.
//! - **native-tls-vendored**: Compile and statically link to a vendored copy of OpenSSL (applies to the default HTTP client).
//! - **reqwest-native-tls-vendored**: Compile and statically link to a vendored copy of OpenSSL (applies to the `reqwest` HTTP client).
//! - **os-api**: Enables the [`api::os`] module.
//! - **process-command-api**: Enables the [`api::process::Command`] APIs.
//! - **global-shortcut**: Enables the global shortcut APIs.
//! - **clipboard**: Enables the clipboard APIs.
Expand Down Expand Up @@ -897,6 +898,7 @@ mod tests {
"fs-extract-api",
"http-api",
"http-multipart",
"os-api",
"process-command-api",
"process-relaunch-dangerous-allow-symlink-macos",
"window-data-url",
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion tooling/api/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,27 @@ async function tempdir(): Promise<string> {
})
}

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/api/os';
* const locale = await locale();
* if (locale) {
* // use the locale string here
* }
* ```
*
* @since 1.3.0
*/
async function locale(): Promise<string | null> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'locale'
}
})
}

export { EOL, platform, version, type, arch, tempdir, locale }
export type { Platform, OsType, Arch }

0 comments on commit 359058c

Please sign in to comment.