Move to TypeScript

This commit is contained in:
Luca Bosin
2023-08-25 13:16:49 +02:00
parent c3e571fcb8
commit dc728b86fe
27 changed files with 160 additions and 246 deletions

58
src/lib/data/language.ts Normal file
View File

@ -0,0 +1,58 @@
import { writable, derived, type Writable } from "svelte/store";
export const language: Writable<TranslationKey> = writable('de');
const translations: Record<TranslationKey, Record<string, string>> = {
de: {
'gallery': 'Galerie',
'album': 'Album',
'albums': 'Alben',
'photo': 'Foto',
'photos': 'Fotos',
'video': 'Video',
'videos': 'Videos',
'back': 'Zurück',
'small': 'Klein',
'medium': 'Mittel',
'large': 'Groß',
'open': 'Öffnen',
'download': 'Herunterladen',
'download-all': 'Alle herunterladen',
},
en: {
'gallery': 'Gallery',
'album': 'Album',
'albums': 'Albums',
'photo': 'Photo',
'photos': 'Photos',
'video': 'Video',
'videos': 'Videos',
'back': 'Back',
'small': 'Small',
'medium': 'Medium',
'large': 'Large',
'open': 'Open',
'download': 'Download',
'download-all': 'Download all',
}
};
export const str = derived(language, $language => {
function translate(key: string, ...args: any[]) {
const str = translations[$language][key];
if (str === undefined) return key;
return str.replace(/\{(\d+)\}/g, (_, i) => args[i]);
}
return translate;
});
export const strf = derived(language, $language => {
function translate(translations: Translation | string, ...args: any[]) {
if (translations === undefined) return undefined;
if (typeof translations === 'string') return translations;
const str = translations[$language];
if (str === undefined) return translations.de;
return str.replace(/\{(\w+)\}/g, (_, i) => args[i]);
}
return translate;
});