From 55434f87ff252c784e5e00b4775b9555da31ebb0 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 13 May 2025 12:50:04 +0200 Subject: translation endpoint --- server/src/api.rs | 36 +++++++++++++++++++++++++++++------- server/src/routes.rs | 9 +++++---- 2 files changed, 34 insertions(+), 11 deletions(-) (limited to 'server') 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 */ 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>, 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 = "")] pub fn r_api_account_login( database: &State, @@ -55,8 +77,8 @@ pub fn r_api_asset_token_raw(_admin: A, token: &str) -> MyResult")] -pub fn r_api_nodes_modified_since( +#[get("/nodes_modified?")] +pub fn r_nodes_modified_since( _session: A, database: &State, 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 { // 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, -- cgit v1.2.3-70-g09d2