summaryrefslogtreecommitdiff
path: root/pixel-client
diff options
context:
space:
mode:
Diffstat (limited to 'pixel-client')
-rw-r--r--pixel-client/src/main.rs5
-rw-r--r--pixel-client/src/menu/credits.rs6
-rw-r--r--pixel-client/src/menu/ingame.rs7
-rw-r--r--pixel-client/src/menu/main.rs9
-rw-r--r--pixel-client/src/menu/settings.rs9
-rw-r--r--pixel-client/src/strings.rs42
-rw-r--r--pixel-client/src/tilemap.rs9
7 files changed, 67 insertions, 20 deletions
diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs
index c43680ae..1dd34e72 100644
--- a/pixel-client/src/main.rs
+++ b/pixel-client/src/main.rs
@@ -15,13 +15,14 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-#![feature(map_many_mut, path_add_extension)]
+#![feature(map_many_mut, path_add_extension, iterator_try_collect)]
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use config::Config;
use game::Game;
use hurrycurry_client_lib::network::sync::Network;
use hurrycurry_protocol::glam::Vec2;
+use strings::set_language;
use menu::{ingame::IngameMenu, main::MainMenu};
use profiler::ProfilerOverlay;
use render::Renderer;
@@ -31,6 +32,7 @@ use std::time::{Duration, Instant};
pub mod config;
pub mod game;
pub mod helper;
+pub mod strings;
pub mod menu;
pub mod profiler;
pub mod render;
@@ -64,6 +66,7 @@ fn main() -> Result<()> {
let args = Args::parse();
let mut config = Config::load()?;
+ set_language("de");
rustls::crypto::ring::default_provider()
.install_default()
diff --git a/pixel-client/src/menu/credits.rs b/pixel-client/src/menu/credits.rs
index b0a74478..75803ee3 100644
--- a/pixel-client/src/menu/credits.rs
+++ b/pixel-client/src/menu/credits.rs
@@ -15,7 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::{render::sprite::SpriteDraw, ui::Ui};
+use crate::{render::sprite::SpriteDraw, strings::tr, ui::Ui};
use hurrycurry_protocol::glam::Vec2;
#[derive(Default)]
@@ -38,12 +38,12 @@ impl CreditsMenu {
ui.advance(Vec2::splat(30.));
ui.text("Pixel Curry!");
- ui.small_text("developed by");
+ ui.small_text(tr("c.credits.developed_by"));
ui.text("metamuffin, BigBrotherNii");
ui.advance(ui.get_remaining() - Vec2::Y * 30.);
- back = ui.button(80., "Back");
+ back = ui.button(80., tr("c.menu.back"));
});
});
diff --git a/pixel-client/src/menu/ingame.rs b/pixel-client/src/menu/ingame.rs
index ed1ad1bb..991faad6 100644
--- a/pixel-client/src/menu/ingame.rs
+++ b/pixel-client/src/menu/ingame.rs
@@ -2,6 +2,7 @@ use super::main::MainMenu;
use crate::{
config::Config,
game::Game,
+ strings::tr,
render::{sprite::SpriteDraw, AtlasLayout, Renderer},
ui::UiState,
State,
@@ -60,10 +61,10 @@ impl IngameMenu {
ui.vertical(|ui| {
ui.advance(Vec2::splat(20.));
let w = 80.;
- main_menu |= ui.button(w, "Resume");
+ main_menu |= ui.button(w, tr("c.menu.ingame.resume"));
ui.advance(Vec2::Y * 10.);
- main_menu |= ui.button(w, "Main menu");
- if ui.button(w, "Quit") {
+ main_menu |= ui.button(w, tr("c.menu.ingame.main_menu"));
+ if ui.button(w, tr("c.menu.ingame.quit")) {
self.next_state = Some(Box::new(State::Quit))
}
});
diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs
index f5f951bf..1f5f299e 100644
--- a/pixel-client/src/menu/main.rs
+++ b/pixel-client/src/menu/main.rs
@@ -21,6 +21,7 @@ use super::{
use crate::{
config::Config,
game::Game,
+ strings::tr,
render::{sprite::SpriteDraw, AtlasLayout, Renderer},
ui::UiState,
State,
@@ -85,7 +86,7 @@ impl MainMenu {
}
return;
}
- if ui.button(80., "Join") {
+ if ui.button(80., tr("c.menu.play.connect")) {
self.next_state = Some(Box::new(State::Ingame(IngameMenu::new(Game::new(
Network::connect(&self.server_address).unwrap(),
config,
@@ -93,13 +94,13 @@ impl MainMenu {
)))))
}
ui.textedit(80., &mut self.server_address);
- if ui.button(80., "Settings") {
+ if ui.button(80., tr("c.menu.settings")) {
self.settings = Some(SettingsMenu::default())
}
- if ui.button(80., "Credits") {
+ if ui.button(80., tr("c.menu.credits")) {
self.credits = Some(CreditsMenu::default())
}
- if ui.button(80., "Quit") {
+ if ui.button(80., tr("c.menu.quit")) {
self.next_state = Some(Box::new(State::Quit));
}
ui.fill();
diff --git a/pixel-client/src/menu/settings.rs b/pixel-client/src/menu/settings.rs
index d497ff48..6f4a20fe 100644
--- a/pixel-client/src/menu/settings.rs
+++ b/pixel-client/src/menu/settings.rs
@@ -15,7 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::{config::Config, render::sprite::SpriteDraw, ui::Ui};
+use crate::{config::Config, strings::tr, render::sprite::SpriteDraw, ui::Ui};
use hurrycurry_protocol::glam::Vec2;
use log::warn;
@@ -41,16 +41,17 @@ impl SettingsMenu {
ui.advance(Vec2::splat(20.));
ui.vertical(|ui| {
ui.advance(Vec2::splat(10.));
- ui.text("Settings");
+ ui.text(tr("c.menu.settings"));
ui.horizontal(|ui| {
- ui.text("Username: ");
+ ui.text(tr("c.settings.username"));
+ ui.advance(Vec2::X * 20.);
ui.textedit(100., &mut config.username);
});
ui.advance(ui.get_remaining() - Vec2::Y * 30.);
- if ui.button(80., "Back") {
+ if ui.button(80., tr("c.menu.back")) {
if let Err(e) = config.save() {
warn!("cannot save config: {e}");
} else {
diff --git a/pixel-client/src/strings.rs b/pixel-client/src/strings.rs
new file mode 100644
index 00000000..cf225c7d
--- /dev/null
+++ b/pixel-client/src/strings.rs
@@ -0,0 +1,42 @@
+use anyhow::{anyhow, Result};
+use std::{
+ collections::HashMap,
+ fs::read_to_string,
+ ops::Index,
+ path::Path,
+ sync::{LazyLock, Mutex},
+};
+
+pub struct Strings(HashMap<String, String>);
+impl Index<&'static str> for Strings {
+ type Output = str;
+ fn index(&self, index: &'static str) -> &Self::Output {
+ self.0.get(index).map(|s| s.as_str()).unwrap_or(index)
+ }
+}
+
+impl Strings {
+ pub fn load(path: &Path) -> Result<Self> {
+ Ok(Self(
+ read_to_string(path)?
+ .lines()
+ .skip(1)
+ .map(|l| {
+ let (k, v) = l.split_once("=").ok_or(anyhow!("'=' missing"))?;
+ Ok::<_, anyhow::Error>((k.to_owned(), v.replace("%n", "\n")))
+ })
+ .try_collect()?,
+ ))
+ }
+}
+
+static TR_LOAD: Mutex<Option<Strings>> = Mutex::new(None);
+static TR: LazyLock<Strings> = LazyLock::new(|| TR_LOAD.lock().unwrap().take().unwrap());
+
+pub fn tr<'a>(s: &'static str) -> &'a str {
+ &TR[s]
+}
+pub fn set_language(lang: &str) {
+ *TR_LOAD.lock().unwrap() =
+ Some(Strings::load(Path::new(&format!("locale/{lang}.ini"))).unwrap());
+}
diff --git a/pixel-client/src/tilemap.rs b/pixel-client/src/tilemap.rs
index edab8b61..b67fc224 100644
--- a/pixel-client/src/tilemap.rs
+++ b/pixel-client/src/tilemap.rs
@@ -15,15 +15,14 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use hurrycurry_protocol::{glam::IVec2, TileIndex};
-use log::warn;
-use sdl2::rect::Rect;
-use std::collections::{HashMap, HashSet};
-
use crate::render::{
sprite::{Sprite, SpriteDraw},
Renderer,
};
+use hurrycurry_protocol::{glam::IVec2, TileIndex};
+use log::warn;
+use sdl2::rect::Rect;
+use std::collections::{HashMap, HashSet};
#[derive(Default, Debug)]
pub struct Tilemap {