aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-05-13 12:50:04 +0200
committermetamuffin <metamuffin@disroot.org>2025-05-13 12:50:04 +0200
commit55434f87ff252c784e5e00b4775b9555da31ebb0 (patch)
treefa3b9d95f96584216eb68143edb1cd4d024f0839
parent2a41d6b0c7c1327856b9cde247621822fe61355f (diff)
downloadjellything-55434f87ff252c784e5e00b4775b9555da31ebb0.tar
jellything-55434f87ff252c784e5e00b4775b9555da31ebb0.tar.bz2
jellything-55434f87ff252c784e5e00b4775b9555da31ebb0.tar.zst
translation endpoint
-rw-r--r--server/src/api.rs36
-rw-r--r--server/src/routes.rs9
-rw-r--r--ui/src/locale.rs5
3 files changed, 39 insertions, 11 deletions
diff --git a/server/src/api.rs b/server/src/api.rs
index 5ec3c5c..6c22010 100644
--- a/server/src/api.rs
+++ b/server/src/api.rs
@@ -4,7 +4,7 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use super::ui::error::MyResult;
-use crate::helper::A;
+use crate::{helper::A, locale::AcceptLanguage};
use jellycommon::{user::CreateSessionParams, NodeID, Visibility};
use jellyimport::asset_token::AssetInner;
use jellylogic::{
@@ -12,6 +12,7 @@ use jellylogic::{
session::{AdminSession, Session},
Database,
};
+use jellyui::locale::get_translation_table;
use rocket::{
get,
http::MediaType,
@@ -20,21 +21,42 @@ use rocket::{
request::{self, FromRequest},
response::Redirect,
serde::json::Json,
- Request, State,
+ Either, Request, State,
};
use serde_json::{json, Value};
-use std::ops::Deref;
+use std::{collections::HashMap, ops::Deref};
#[get("/api")]
pub fn r_api_root() -> Redirect {
Redirect::moved("https://jellything.metamuffin.org/book/api.html#jellything-http-api")
}
-#[get("/api/version")]
-pub fn r_api_version() -> &'static str {
+#[get("/version")]
+pub fn r_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
+#[get("/translations")]
+pub fn r_translations(
+ lang: AcceptLanguage,
+ aj: AcceptJson,
+) -> Either<Json<&'static HashMap<&'static str, &'static str>>, String> {
+ let AcceptLanguage(lang) = lang;
+ let table = get_translation_table(&lang);
+ if *aj {
+ Either::Left(Json(table))
+ } else {
+ let mut s = String::new();
+ for (k, v) in table {
+ s += k;
+ s += "=";
+ s += v;
+ s += "\n";
+ }
+ Either::Right(s)
+ }
+}
+
#[post("/api/create_session", data = "<data>")]
pub fn r_api_account_login(
database: &State<Database>,
@@ -55,8 +77,8 @@ pub fn r_api_asset_token_raw(_admin: A<AdminSession>, token: &str) -> MyResult<J
Ok(Json(AssetInner::deser(token)?))
}
-#[get("/api/nodes_modified?<since>")]
-pub fn r_api_nodes_modified_since(
+#[get("/nodes_modified?<since>")]
+pub fn r_nodes_modified_since(
_session: A<Session>,
database: &State<Database>,
since: u64,
diff --git a/server/src/routes.rs b/server/src/routes.rs
index cc68067..8692685 100644
--- a/server/src/routes.rs
+++ b/server/src/routes.rs
@@ -30,8 +30,8 @@ use crate::ui::{
use crate::CONF;
use crate::{
api::{
- r_api_account_login, r_api_asset_token_raw, r_api_nodes_modified_since, r_api_root,
- r_api_version,
+ r_api_account_login, r_api_asset_token_raw, r_api_root, r_nodes_modified_since,
+ r_translations, r_version,
},
compat::{
jellyfin::{
@@ -164,9 +164,10 @@ pub fn build_rocket(database: Database) -> Rocket<Build> {
// API
r_api_account_login,
r_api_asset_token_raw,
- r_api_nodes_modified_since,
+ r_nodes_modified_since,
r_api_root,
- r_api_version,
+ r_version,
+ r_translations,
// Compat
r_jellyfin_artists,
r_jellyfin_branding_configuration,
diff --git a/ui/src/locale.rs b/ui/src/locale.rs
index 0179c66..442bd9d 100644
--- a/ui/src/locale.rs
+++ b/ui/src/locale.rs
@@ -19,6 +19,7 @@ static LANG_TABLES: LazyLock<HashMap<Language, HashMap<&'static str, &'static st
(Language::English, include_str!("../../locale/en.ini")),
(Language::German, include_str!("../../locale/de.ini")),
] {
+ // TODO fallback to english
let tr_map = source
.lines()
.filter_map(|line| {
@@ -39,6 +40,10 @@ pub fn tr(lang: Language, key: &str) -> Cow<'static, str> {
}
}
+pub fn get_translation_table(lang: &Language) -> &'static HashMap<&'static str, &'static str> {
+ LANG_TABLES.get(lang).unwrap()
+}
+
pub struct TrString<'a>(Cow<'a, str>);
impl Render for TrString<'_> {
fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result {