Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translations: disable gettext by default for C++ and the viewer #2852

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ define_cargo_feature(renderer-winit-software "Enable support for the software re
define_cargo_feature(backend-qt "Enable Qt based rendering backend" ON)

define_cargo_feature(experimental "Enable experimental features (no compatibility guarantees)" OFF)
define_cargo_feature(gettext "Enable support of translations using gettext" OFF)

# Compat options
option(SLINT_FEATURE_BACKEND_GL_ALL "This feature is an alias for SLINT_FEATURE_BACKEND_WINIT and SLINT_FEATURE_RENDERER_WINIT_FEMTOVG." OFF)
Expand Down
3 changes: 2 additions & 1 deletion api/cpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ renderer-winit-skia = ["i-slint-backend-selector/renderer-winit-skia"]
renderer-winit-skia-opengl = ["i-slint-backend-selector/renderer-winit-skia-opengl"]
renderer-winit-skia-vulkan = ["i-slint-backend-selector/renderer-winit-skia-vulkan"]
renderer-winit-software = ["i-slint-backend-selector/renderer-winit-software"]
gettext = ["i-slint-core/gettext-rs"]

experimental = ["i-slint-renderer-skia", "raw-window-handle"]

Expand All @@ -44,7 +45,7 @@ default = ["backend-winit", "renderer-winit-femtovg", "backend-qt", "experimenta
i-slint-backend-selector = { version = "=1.0.3", path="../../internal/backends/selector" }
i-slint-backend-testing = { version = "=1.0.3", path="../../internal/backends/testing", optional = true }
i-slint-renderer-skia = { version = "=1.0.3", path="../../internal/renderers/skia", optional = true, features = ["x11", "wayland"] }
i-slint-core = { version = "=1.0.3", path="../../internal/core", features = ["ffi", "gettext-rs"] }
i-slint-core = { version = "=1.0.3", path="../../internal/core", features = ["ffi"] }
slint-interpreter = { version = "=1.0.3", path="../../internal/interpreter", default-features = false, features = ["ffi", "compat-1-0"], optional = true }
raw-window-handle = { version = "0.5", optional = true }
# Enable image-rs' default features to make all image formats to C++ users
Expand Down
6 changes: 5 additions & 1 deletion docs/language/src/concepts/translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export component MenuItem {

## Extract Translatable Strings


Use the `slint-tr-extractor` tool to generate a `.pot` file from `.slint` files.
You can run it like so:

Expand Down Expand Up @@ -151,7 +152,7 @@ With these sttings, Slint will look for `gallery.mo` in the `lang/fr/LC_MESSAGES

### Select and Load Translations with C++

TODO: Adjust this to whatever we determine as the final mechanism of accessing gettext.
You need to enable the feature by passing `-DSLINT_FEATURE_GETTEXT=1` when compiling Slint.

In C++ application using cmake, the `domain_name` is the CMake target name.

Expand Down Expand Up @@ -187,5 +188,8 @@ int main()

## Previewing Translations with `slint-viewer`

The `slint-viewer` need to be compiled with the `gettext` feature

When previewing `.slint` files with `slint-viewer`, use the `--translation-domain` and `--translation-dir`.
command line option to automatically load translations and display them based on the current locale.

9 changes: 8 additions & 1 deletion tools/viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ renderer-winit-skia = ["slint-interpreter/renderer-winit-skia"]
renderer-winit-skia-opengl = ["slint-interpreter/renderer-winit-skia-opengl"]
renderer-winit-skia-vulkan = ["slint-interpreter/renderer-winit-skia-vulkan"]

## Enable the translations using [gettext](https://www.gnu.org/software/gettext/gettext)
##
## the `@tr(...)` code from .slint files will be transformed into call to `dgettext`.
## You should pass --translation-domain and --translation-dir command line argument
## so that the viewer can find the translation
gettext = ["i-slint-core/gettext-rs"]

# Compat
backend-gl-all = ["backend-winit", "renderer-winit-femtovg"]
backend-gl-wayland = ["backend-winit-wayland", "renderer-winit-femtovg"]
Expand All @@ -35,7 +42,7 @@ backend-gl-x11 = ["backend-winit-x11", "renderer-winit-femtovg"]
default = ["backend-qt", "backend-winit", "renderer-winit-femtovg"]

[dependencies]
i-slint-core = { version = "=1.0.3", path="../../internal/core", features = ["gettext-rs"] }
i-slint-core = { version = "=1.0.3", path="../../internal/core" }
slint-interpreter = { version = "=1.0.3", path = "../../internal/interpreter", default-features = false, features = ["display-diagnostics", "compat-1-0"] }
i-slint-backend-selector = { version = "=1.0.3", path="../../internal/backends/selector" }

Expand Down
5 changes: 5 additions & 0 deletions tools/viewer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ struct Cli {
#[arg(long, value_names(&["callback", "handler"]), number_of_values = 2, action)]
on: Vec<String>,

#[cfg(feature = "gettext")]
/// Translation domain
#[arg(long = "translation-domain", action)]
translation_domain: Option<String>,

#[cfg(feature = "gettext")]
/// Translation directory where the translation files are searched for
#[arg(long = "translation-dir", action)]
translation_dir: Option<std::path::PathBuf>,
}
Expand All @@ -77,6 +80,7 @@ fn main() -> Result<()> {
std::env::set_var("SLINT_BACKEND", backend);
}

#[cfg(feature = "gettext")]
if let Some(dirname) = args.translation_dir.clone() {
i_slint_core::translations::gettext_bindtextdomain(
args.translation_domain.as_ref().map(String::as_str).unwrap_or_default(),
Expand Down Expand Up @@ -153,6 +157,7 @@ fn init_compiler(
fswatcher: Option<Arc<Mutex<notify::RecommendedWatcher>>>,
) -> slint_interpreter::ComponentCompiler {
let mut compiler = slint_interpreter::ComponentCompiler::default();
#[cfg(feature = "gettext")]
if let Some(domain) = args.translation_domain.clone() {
compiler.set_translation_domain(domain);
}
Expand Down