feat: add localization for internal widget

This commit is contained in:
wiiznokes 2024-08-02 17:49:23 +02:00
parent f655710d55
commit 0942c67fa2
8 changed files with 86 additions and 12 deletions

View file

@ -8,7 +8,7 @@ rust-version = "1.71"
name = "cosmic" name = "cosmic"
[features] [features]
default = ["clipboard"] default = ["clipboard", "localization"]
# Accessibility support # Accessibility support
a11y = ["iced/a11y", "iced_accessibility"] a11y = ["iced/a11y", "iced_accessibility"]
# Builds support for animated images # Builds support for animated images
@ -70,6 +70,7 @@ winit_tokio = ["winit", "tokio"]
winit_wgpu = ["winit", "wgpu"] winit_wgpu = ["winit", "wgpu"]
# Enables XDG portal integrations # Enables XDG portal integrations
xdg-portal = ["ashpd"] xdg-portal = ["ashpd"]
localization = ["i18n-embed", "rust-embed", "i18n-embed-fl"]
[dependencies] [dependencies]
apply = "0.3.0" apply = "0.3.0"
@ -88,7 +89,10 @@ libc = { version = "0.2.155", optional = true }
mime = { version = "0.3.17", optional = true } mime = { version = "0.3.17", optional = true }
palette = "0.7.3" palette = "0.7.3"
rfd = { version = "0.14.0", optional = true } rfd = { version = "0.14.0", optional = true }
rustix = { version = "0.38.34", features = ["pipe", "process"], optional = true } rustix = { version = "0.38.34", features = [
"pipe",
"process",
], optional = true }
serde = { version = "1.0.180", features = ["derive"] } serde = { version = "1.0.180", features = ["derive"] }
slotmap = "1.0.6" slotmap = "1.0.6"
smol = { version = "2.0.0", optional = true } smol = { version = "2.0.0", optional = true }
@ -99,6 +103,12 @@ tracing = "0.1"
unicode-segmentation = "1.6" unicode-segmentation = "1.6"
url = "2.4.0" url = "2.4.0"
zbus = { version = "4.2.1", default-features = false, optional = true } zbus = { version = "4.2.1", default-features = false, optional = true }
i18n-embed = { version = "0.14", features = [
"fluent-system",
"desktop-requester",
], optional = true }
rust-embed = { version = "8.0", optional = true }
i18n-embed-fl = { version = "0.8", optional = true }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
freedesktop-icons = "0.2.5" freedesktop-icons = "0.2.5"

4
i18n.toml Normal file
View file

@ -0,0 +1,4 @@
fallback_language = "en"
[fluent]
assets_dir = "./i18n"

1
i18n/en/libcosmic.ftl Normal file
View file

@ -0,0 +1 @@
close = Close

1
i18n/fr/libcosmic.ftl Normal file
View file

@ -0,0 +1 @@
close = Fermer

View file

@ -161,6 +161,9 @@ pub(crate) fn iced_settings<App: Application>(
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result { pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result {
let settings = iced_settings::<App>(settings, flags); let settings = iced_settings::<App>(settings, flags);
#[cfg(feature = "localization")]
crate::localize::localize();
cosmic::Cosmic::<App>::run(settings) cosmic::Cosmic::<App>::run(settings)
} }

View file

@ -91,6 +91,10 @@ pub use cctk;
pub mod theme; pub mod theme;
#[cfg(feature = "localization")]
#[macro_use]
pub mod localize;
#[doc(inline)] #[doc(inline)]
pub use theme::{style, Theme}; pub use theme::{style, Theme};

46
src/localize.rs Normal file
View file

@ -0,0 +1,46 @@
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "./i18n/"]
struct Localizations;
lazy_static::lazy_static! {
pub static ref LANGUAGE_LOADER: FluentLanguageLoader = {
let loader: FluentLanguageLoader = fluent_language_loader!();
loader
.load_fallback_language(&Localizations)
.expect("Error while loading fallback language");
loader
};
}
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
}};
($message_id:literal, $($args:expr),*) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}
// Get the `Localizer` to be used for localizing this library.
fn localizer() -> Box<dyn Localizer> {
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
}
pub fn localize() {
let localizer = localizer();
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language {}", error);
}
}

View file

@ -54,7 +54,12 @@ impl<'a, Message: Clone + 'static> ContextDrawer<'a, Message> {
.vertical_alignment(alignment::Vertical::Center), .vertical_alignment(alignment::Vertical::Center),
) )
.push( .push(
button::text("Close") button::text(
#[cfg(feature = "localization")]
fl!("close"),
#[cfg(not(feature = "localization"))]
"Close",
)
.trailing_icon(icon::from_name("go-next-symbolic")) .trailing_icon(icon::from_name("go-next-symbolic"))
.on_press(on_close) .on_press(on_close)
.style(crate::theme::Button::Link) .style(crate::theme::Button::Link)