Skip to content

Commit

Permalink
feat: i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Nov 17, 2024
1 parent 52ef3d7 commit 8eacf42
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 7 deletions.
333 changes: 330 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# (for which all strings must be present) when using the fluent
# system.
fallback_language = "en"

# (Optional) Use the fluent localization system.
[fluent]
# (Required) The path to the assets directory.
# The paths inside the assets directory should be structured like so:
# `assets_dir/{language}/{domain}.ftl`
assets_dir = "i18n"
8 changes: 7 additions & 1 deletion lala_bar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lala-bar"
version.workspace = true
version = "0.4.1"
edition.workspace = true
description = "a bar on layershell with iced-rs"
authors.workspace = true
Expand Down Expand Up @@ -46,3 +46,9 @@ async-trait.workspace = true
tracing-journald = "0.3.0"
iced_aw = "0.11.0"
iced_fonts = "0.1.1"
i18n-embed = { version = "0.13.4", features = [
"fluent-system",
"desktop-requester",
] }
i18n-embed-fl = "0.6.4"
rust-embed = "6.3.0"
10 changes: 10 additions & 0 deletions lala_bar/i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# (for which all strings must be present) when using the fluent
# system.
fallback_language = "en"

# (Optional) Use the fluent localization system.
[fluent]
# (Required) The path to the assets directory.
# The paths inside the assets directory should be structured like so:
# `assets_dir/{language}/{domain}.ftl`
assets_dir = "i18n"
3 changes: 3 additions & 0 deletions lala_bar/i18n/en/lala_bar.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
balance = balance
right = right
left = left
45 changes: 45 additions & 0 deletions lala_bar/src/localize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use rust_embed::RustEmbed;
use std::sync::LazyLock;

#[derive(RustEmbed)]
#[folder = "i18n/"]
struct Localizations;

pub static LANGUAGE_LOADER: LazyLock<FluentLanguageLoader> = LazyLock::new(|| {
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.
pub 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 for App List {}", error);
}
}
2 changes: 2 additions & 0 deletions lala_bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod music_bar;
mod notify;
mod slider;
mod zbus_mpirs;
mod localize;

use crate::music_bar::LalaMusicBar;
use crate::notify::NotifyCommand;
Expand All @@ -35,6 +36,7 @@ pub fn main() -> Result<(), iced_layershell::Error> {
//.unwrap(),
)
.init();
localize::localize();
LalaMusicBar::run(Settings {
layer_settings: LayerShellSettings {
size: Some((0, 35)),
Expand Down
8 changes: 5 additions & 3 deletions lala_bar/src/music_bar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::config::*;
use crate::dbusbackend;
use crate::fl;
use crate::get_metadata;
use crate::launcher::LaunchMessage;
use crate::notify::{NotifyCommand, NotifyUnitWidgetInfo};
Expand Down Expand Up @@ -109,9 +110,10 @@ impl LalaMusicBar {
fn update_balance(&mut self) {
self.left = aximer::get_left().unwrap_or(0);
self.right = aximer::get_right().unwrap_or(0);
self.left_text = format!("left {}%", self.left);
self.right_text = format!("right {}%", self.right);
self.balance_text = format!("balance {}%", self.balance_percent());

self.left_text = format!("{} {}%", fl!("left"), self.left);
self.right_text = format!("{} {}%", fl!("right"), self.right);
self.balance_text = format!("{} {}%", fl!("balance"), self.balance_percent());
}

fn set_balance(&mut self, balance: u8) {
Expand Down

0 comments on commit 8eacf42

Please sign in to comment.