feat: add localization for internal widget
This commit is contained in:
parent
f655710d55
commit
0942c67fa2
8 changed files with 86 additions and 12 deletions
16
Cargo.toml
16
Cargo.toml
|
|
@ -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
|
||||
|
|
@ -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"
|
||||
|
|
@ -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 }
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
4
i18n.toml
Normal file
4
i18n.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fallback_language = "en"
|
||||
|
||||
[fluent]
|
||||
assets_dir = "./i18n"
|
||||
1
i18n/en/libcosmic.ftl
Normal file
1
i18n/en/libcosmic.ftl
Normal file
|
|
@ -0,0 +1 @@
|
|||
close = Close
|
||||
1
i18n/fr/libcosmic.ftl
Normal file
1
i18n/fr/libcosmic.ftl
Normal file
|
|
@ -0,0 +1 @@
|
|||
close = Fermer
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
||||
|
|
|
|||
46
src/localize.rs
Normal file
46
src/localize.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue