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

feat: add localization for internal widget #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rust-version = "1.71"
name = "cosmic"

[features]
default = ["clipboard"]
default = ["clipboard", "localization"]
# Accessibility support
a11y = ["iced/a11y", "iced_accessibility"]
# Builds support for animated images
Expand Down Expand Up @@ -70,6 +70,7 @@ winit_tokio = ["winit", "tokio"]
winit_wgpu = ["winit", "wgpu"]
# Enables XDG portal integrations
xdg-portal = ["ashpd"]
localization = ["i18n-embed", "rust-embed", "i18n-embed-fl"]

[dependencies]
apply = "0.3.0"
Expand All @@ -84,11 +85,14 @@ derive_setters = "0.1.5"
fraction = "0.15.3"
image = { version = "0.25.1", optional = true }
lazy_static = "1.4.0"
libc = { version = "0.2.155", optional = true }
libc = { version = "0.2.155", optional = true }
mime = { version = "0.3.17", optional = true }
palette = "0.7.3"
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"] }
slotmap = "1.0.6"
smol = { version = "2.0.0", optional = true }
Expand All @@ -99,6 +103,12 @@ tracing = "0.1"
unicode-segmentation = "1.6"
url = "2.4.0"
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]
freedesktop-icons = "0.2.5"
Expand Down
4 changes: 4 additions & 0 deletions i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fallback_language = "en"

[fluent]
assets_dir = "./i18n"
1 change: 1 addition & 0 deletions i18n/en/libcosmic.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
close = Close
1 change: 1 addition & 0 deletions i18n/fr/libcosmic.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
close = Fermer
3 changes: 3 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ pub(crate) fn iced_settings<App: Application>(
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result {
let settings = iced_settings::<App>(settings, flags);

#[cfg(feature = "localization")]
crate::localize::localize();

cosmic::Cosmic::<App>::run(settings)
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub use cctk;

pub mod theme;

#[cfg(feature = "localization")]
#[macro_use]
pub mod localize;

#[doc(inline)]
pub use theme::{style, Theme};

Expand Down
46 changes: 46 additions & 0 deletions src/localize.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 14 additions & 9 deletions src/widget/context_drawer/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ impl<'a, Message: Clone + 'static> ContextDrawer<'a, Message> {
.vertical_alignment(alignment::Vertical::Center),
)
.push(
button::text("Close")
.trailing_icon(icon::from_name("go-next-symbolic"))
.on_press(on_close)
.style(crate::theme::Button::Link)
.apply(container)
.width(Length::FillPortion(1))
.height(Length::Fill)
.align_x(alignment::Horizontal::Right)
.center_y(),
button::text(
#[cfg(feature = "localization")]
fl!("close"),
#[cfg(not(feature = "localization"))]
"Close",
)
.trailing_icon(icon::from_name("go-next-symbolic"))
.on_press(on_close)
.style(crate::theme::Button::Link)
.apply(container)
.width(Length::FillPortion(1))
.height(Length::Fill)
.align_x(alignment::Horizontal::Right)
.center_y(),
)
// XXX must be done after pushing elements or it may be overwritten by size hints from contents
.height(Length::Fixed(80.0))
Expand Down
Loading