MapComplete/src/UI/InputElement/Helpers/TranslationInput.svelte

107 lines
2.8 KiB
Svelte
Raw Normal View History

<script lang="ts">
2023-11-09 16:30:26 +01:00
import { UIEventSource } from "../../../Logic/UIEventSource"
import LanguageUtils from "../../../Utils/LanguageUtils"
import { createEventDispatcher, onDestroy } from "svelte"
import ValidatedInput from "../ValidatedInput.svelte"
2024-04-01 02:29:37 +02:00
import { del } from "idb-keyval"
2023-11-09 16:30:26 +01:00
export let value: UIEventSource<Record<string, string>> = new UIEventSource<
Record<string, string>
>({})
2023-10-20 19:04:55 +02:00
export let args: string[] = []
2023-11-09 16:30:26 +01:00
let prefix = args[0] ?? ""
let postfix = args[1] ?? ""
let translations: UIEventSource<Record<string, string>> = value
2023-11-09 16:30:26 +01:00
const allLanguages: string[] = LanguageUtils.usedLanguagesSorted
let currentLang = new UIEventSource("en")
const currentVal = new UIEventSource<string>("")
2024-04-01 02:29:37 +02:00
/**
* Mostly the same as currentVal, but might be the empty string as well
*/
const currentValRaw = new UIEventSource<string>("")
2023-09-15 01:16:33 +02:00
let dispatch = createEventDispatcher<{ submit }>()
function update() {
2024-04-01 02:29:37 +02:00
let v = currentValRaw.data
2023-11-09 16:30:26 +01:00
const l = currentLang.data
2024-04-01 02:29:37 +02:00
console.log("Updating translation input for value", v, " and language", l)
2024-02-20 13:33:38 +01:00
if (<any>translations.data === "" || translations.data === undefined) {
translations.data = {}
}
2024-04-01 02:29:37 +02:00
if (v === "") {
delete translations.data[l]
translations.ping()
return
}
if (translations.data[l] === v) {
2023-11-09 16:30:26 +01:00
return
}
2023-11-09 16:30:26 +01:00
translations.data[l] = v
translations.ping()
}
2023-11-09 16:30:26 +01:00
onDestroy(
currentLang.addCallbackAndRunD((currentLang) => {
if (!translations.data) {
translations.data = {}
}
translations.data[currentLang] = translations.data[currentLang] ?? ""
2024-04-01 02:29:37 +02:00
if (translations.data[currentLang] === "") {
delete translations.data[currentLang]
}
currentVal.setData(translations.data[currentLang] ?? "")
currentValRaw.setData(translations.data[currentLang])
2023-11-09 16:30:26 +01:00
})
)
2023-11-09 16:30:26 +01:00
onDestroy(
2024-04-01 02:29:37 +02:00
currentValRaw.addCallbackAndRunD(() => {
2023-11-09 16:30:26 +01:00
update()
})
)
2024-04-01 02:29:37 +02:00
</script>
2024-04-13 02:40:21 +02:00
2024-04-01 02:29:37 +02:00
<div class="flex flex-col gap-y-1">
<div class="interactive m-1 mt-2 flex space-x-1 font-bold">
2024-04-13 02:40:21 +02:00
<span>
{prefix}
</span>
2024-04-01 02:29:37 +02:00
<select bind:value={$currentLang}>
{#each allLanguages as language}
<option value={language}>
{language}
2024-04-11 17:13:48 +02:00
{#if $translations?.[language] !== undefined}
2024-04-01 02:29:37 +02:00
*
{/if}
</option>
{/each}
</select>
<ValidatedInput
type="string"
cls="w-full"
value={currentVal}
unvalidatedText={currentValRaw}
on:submit={() => dispatch("submit")}
/>
<span>
2024-04-13 02:40:21 +02:00
{postfix}
</span>
2024-04-01 02:29:37 +02:00
</div>
You have currently set translations for
<ul>
{#each Object.keys($translations) as l}
2024-04-13 02:40:21 +02:00
<li>
<button class="small" on:click={() => currentLang.setData(l)}>
<b>{l}:</b>
{$translations[l]}
</button>
</li>
2024-04-01 02:29:37 +02:00
{/each}
</ul>
</div>