2023-06-30 13:36:02 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
|
|
|
import { UIEventSource } from "../../../Logic/UIEventSource";
|
|
|
|
import LanguageUtils from "../../../Utils/LanguageUtils";
|
2023-09-15 01:16:33 +02:00
|
|
|
import { createEventDispatcher, onDestroy } from "svelte";
|
2023-06-30 13:36:02 +02:00
|
|
|
import ValidatedInput from "../ValidatedInput.svelte";
|
|
|
|
|
2023-10-24 22:01:10 +02: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-10-24 22:01:10 +02:00
|
|
|
let prefix = args[0] ?? ""
|
|
|
|
let postfix = args[1] ?? ""
|
2023-06-30 13:36:02 +02:00
|
|
|
|
2023-10-24 22:01:10 +02:00
|
|
|
let translations: UIEventSource<Record<string, string>> = value
|
2023-06-30 13:36:02 +02:00
|
|
|
|
|
|
|
const allLanguages: string[] = LanguageUtils.usedLanguagesSorted;
|
|
|
|
let currentLang = new UIEventSource("en");
|
|
|
|
const currentVal = new UIEventSource<string>("");
|
2023-09-15 01:16:33 +02:00
|
|
|
let dispatch = createEventDispatcher<{ submit }>()
|
2023-06-30 13:36:02 +02:00
|
|
|
|
|
|
|
function update() {
|
|
|
|
const v = currentVal.data;
|
|
|
|
const l = currentLang.data;
|
2023-10-24 22:01:10 +02:00
|
|
|
if(translations.data === "" || translations.data === undefined){
|
|
|
|
translations.data = {}
|
|
|
|
}
|
2023-06-30 13:36:02 +02:00
|
|
|
if (translations.data[l] === v) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
translations.data[l] = v;
|
|
|
|
translations.ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy(currentLang.addCallbackAndRunD(currentLang => {
|
|
|
|
console.log("Applying current lang:", currentLang);
|
2023-10-24 22:01:10 +02:00
|
|
|
if(!translations.data){
|
|
|
|
translations.data = {}
|
|
|
|
}
|
2023-06-30 13:36:02 +02:00
|
|
|
translations.data[currentLang] = translations.data[currentLang] ?? "";
|
|
|
|
currentVal.setData(translations.data[currentLang]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
onDestroy(currentVal.addCallbackAndRunD(v => {
|
|
|
|
update();
|
|
|
|
}));
|
|
|
|
|
|
|
|
</script>
|
2023-10-20 19:04:55 +02:00
|
|
|
<div class="flex font-bold space-x-1 m-1 mt-2 interactive">
|
|
|
|
<span>
|
|
|
|
{prefix}
|
|
|
|
</span>
|
2023-06-30 13:36:02 +02:00
|
|
|
<select bind:value={$currentLang}>
|
|
|
|
{#each allLanguages as language}
|
|
|
|
<option value={language}>
|
|
|
|
{language}
|
|
|
|
</option>
|
|
|
|
{/each}
|
|
|
|
</select>
|
2023-10-20 19:04:55 +02:00
|
|
|
<ValidatedInput type="string" cls="w-full" value={currentVal} on:submit={() => dispatch("submit")} />
|
|
|
|
<span>
|
|
|
|
{postfix}
|
|
|
|
</span>
|
2023-06-30 13:36:02 +02:00
|
|
|
</div>
|