From 920681f68c0b2128b35f821bcb184ae0e21c873e Mon Sep 17 00:00:00 2001 From: Jannik Date: Thu, 2 Oct 2025 21:41:03 +0200 Subject: [PATCH] feat(routing): detect TTS language from valhalla trip --- messages/de.json | 4 ++++ messages/en.json | 4 ++++ src/lib/services/navigation/TTS.ts | 13 +++++++++++-- src/lib/services/navigation/routing.svelte.ts | 13 ++++++++----- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/messages/de.json b/messages/de.json index 7d1a074..57a314d 100644 --- a/messages/de.json +++ b/messages/de.json @@ -160,5 +160,9 @@ "poi": { "fuel": "Tankstelle", "parking": "Parken" + }, + "routing": { + "off-route": "Sie sind von der Route abgekommen", + "back-on-route": "Sie sind wieder auf der Route" } } diff --git a/messages/en.json b/messages/en.json index 85063b1..525c5b0 100644 --- a/messages/en.json +++ b/messages/en.json @@ -160,5 +160,9 @@ "poi": { "fuel": "Fuel Station", "parking": "Parking" + }, + "routing": { + "off-route": "You went off route", + "back-on-route": "You are back on route" } } diff --git a/src/lib/services/navigation/TTS.ts b/src/lib/services/navigation/TTS.ts index 3de1ac0..391f291 100644 --- a/src/lib/services/navigation/TTS.ts +++ b/src/lib/services/navigation/TTS.ts @@ -1,6 +1,7 @@ import { duck, unduck } from "tauri-plugin-duck-api"; import { invoke } from "@tauri-apps/api/core"; import { m } from "$lang/messages"; +import { locales, type Locale } from "$lang/runtime"; export let tts: "tauri" | "web" | null = null; @@ -14,7 +15,15 @@ export async function initTTS() { } } -export default async function say(text: string) { +export function findLocaleForValhallaLanguage(language: string) { + for(const locale of locales) { + if(m["language.valhalla"]({ language }, { locale }) === language) { + return locale; + } + } +} + +export default async function say(text: string, language?: Locale) { if (!tts) { // alert("TTS not initialized"); // console.error("TTS not initialized"); @@ -24,7 +33,7 @@ export default async function say(text: string) { duck(); if (tts !== "web") { try { - await invoke("plugin:tts|speak", { text, language: m["language.tts"]() }); + await invoke("plugin:tts|speak", { text, language: language ? m["language.tts"]({}, { locale: language }) : m["language.tts"]() }); } catch (e) { console.error("Error speaking text", e); alert(e); diff --git a/src/lib/services/navigation/routing.svelte.ts b/src/lib/services/navigation/routing.svelte.ts index c7b6ef7..ff89bd3 100644 --- a/src/lib/services/navigation/routing.svelte.ts +++ b/src/lib/services/navigation/routing.svelte.ts @@ -1,10 +1,11 @@ import { location } from "$lib/components/lnv/location.svelte"; import { map } from "$lib/components/lnv/map.svelte"; -import say from "./TTS"; +import say, { findLocaleForValhallaLanguage } from "./TTS"; import type { ValhallaRequest } from "./ValhallaRequest"; import type { LngLatBoundsLike } from "maplibre-gl"; import { generateVoiceGuidance } from "./VoiceGuidance"; import { keepScreenOn } from "tauri-plugin-keep-screen-on-api"; +import { m } from "$lang/messages"; export const routing = $state({ geojson: { @@ -180,14 +181,14 @@ async function tickRoute() { if (!isOnShape(loc, polyline)) { console.log("Off route!"); if (!info.isOffRoute) { - say("You went off route."); + say(m["routing.off-route"]()); } info.isOffRoute = true; // TODO: Implement re-routing logic return; } else { if (info.isOffRoute) { - say("You are back on route."); + say(m["routing.back-on-route"]()); } info.isOffRoute = false; } @@ -212,7 +213,8 @@ async function tickRoute() { // currentManeuver.verbal_pre_transition_instruction, instruction, ); - say(instruction); + const locale = findLocaleForValhallaLanguage(trip.language); + say(instruction, locale); } } @@ -244,7 +246,8 @@ async function tickRoute() { "[Verbal instruction] ", currentManeuver.verbal_post_transition_instruction, ); - say(currentManeuver.verbal_post_transition_instruction); + const locale = findLocaleForValhallaLanguage(trip.language); + say(currentManeuver.verbal_post_transition_instruction, locale); } }