node: added initTranslations function (#6504)

This commit is contained in:
FloVanGH 2024-10-10 14:37:33 +00:00 committed by GitHub
parent 52afd18650
commit ccf5f04087
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 1 deletions

View file

@ -45,7 +45,7 @@ testing = ["dep:i-slint-backend-testing"]
napi = { version = "2.14.0", default-features = false, features = ["napi8"] }
napi-derive = "2.14.0"
i-slint-compiler = { workspace = true, features = ["default"] }
i-slint-core = { workspace = true, features = ["default"] }
i-slint-core = { workspace = true, features = ["default", "gettext-rs"] }
i-slint-backend-selector = { workspace = true }
slint-interpreter = { workspace = true, default-features = false, features = ["display-diagnostics", "internal", "compat-1-2"] }
spin_on = { workspace = true }

View file

@ -2,6 +2,8 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
mod interpreter;
use std::path::PathBuf;
pub use interpreter::*;
mod types;
@ -86,6 +88,12 @@ pub fn init_testing() {
i_slint_backend_testing::init_integration_test_with_mock_time();
}
#[napi]
pub fn init_translations(domain: String, dir_name: String) -> napi::Result<()> {
i_slint_core::translations::gettext_bindtextdomain(domain.as_str(), PathBuf::from(dir_name))
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
pub fn print_to_console(env: Env, function: &str, arguments: core::fmt::Arguments) {
let Ok(global) = env.get_global() else {
eprintln!("Unable to obtain global object");

View file

@ -922,6 +922,28 @@ export namespace private_api {
}
}
/**
* Initialize translations.
*
* Call this with the path where translations are located. This function internally calls the [bindtextdomain](https://man7.org/linux/man-pages/man3/bindtextdomain.3.html) function from gettext.
*
* Translations are expected to be found at <path>/<locale>/LC_MESSAGES/<domain>.mo, where path is the directory passed as an argument to this function, locale is a locale name (e.g., en, en_GB, fr), and domain is the package name.
*
* @param domain defines the domain name e.g. name of the package.
* @param path specifies the directory as `string` or as `URL` in which gettext should search for translations.
*
* For example, assuming this is in a package called example and the default locale is configured to be French, it will load translations at runtime from ``/path/to/example/translations/fr/LC_MESSAGES/example.mo`.
*
* ```js
* import * as slint from "slint-ui";
* slint.initTranslations("example", new URL("translations/", import.meta.url));
* ````
*/
export function initTranslations(domain: string, path: string | URL) {
const pathname = path instanceof URL ? path.pathname : path;
napi.initTranslations(domain, pathname);
}
/**
* @hidden
*/