@@ -5,7 +5,7 @@ import { getContext, setContext } from "svelte";
55import type { Readable } from "svelte/store" ;
66import { derived , get , writable } from "svelte/store" ;
77
8- export const getI18nStore = ( i18nNextOptions ?: Partial < InitOptions > ) => {
8+ const createI18nStore = ( i18nNextOptions ?: Partial < InitOptions > ) => {
99 i18nNext . use ( LanguageDetector ) . init ( {
1010 fallbackLng : "en" ,
1111 detection : {
@@ -15,13 +15,37 @@ export const getI18nStore = (i18nNextOptions?: Partial<InitOptions>) => {
1515 interpolation : {
1616 escapeValue : false // not needed for svelte as it escapes by default
1717 } ,
18+ defaultNS : "translation" ,
1819 ...i18nNextOptions
1920 } ) ;
2021 return derived ( [ currentLanguage ] , ( ) => i18nNext ) ;
2122} ;
2223
2324export function initI18n ( i18nNextOptions ?: Partial < InitOptions > ) {
24- setContext ( "i18n" , getI18nStore ( i18nNextOptions ) ) ;
25+ setContext ( "i18n" , createI18nStore ( i18nNextOptions ) ) ;
26+ }
27+
28+ export function addTranslations ( locale : string , translations : Record < string , unknown > ) {
29+ i18nNext . addResourceBundle ( locale , "translation" , translations , true , true ) ;
30+ translationsUpdated . set ( true ) ;
31+ }
32+
33+ export async function addTranslationsFromUrl ( url : string ) {
34+ // load translations async
35+ await fetch ( url ) . then ( async ( response ) => {
36+ const translations = await response . json ( ) ;
37+ for ( const translation of translations . elements ) {
38+ const keyValueMap = translation . translations . reduce (
39+ ( acc , curr ) => {
40+ acc [ curr . key ] = curr . value ;
41+ return acc ;
42+ } ,
43+ { } as Record < string , string >
44+ ) ;
45+ i18nNext . addResourceBundle ( translation . language , "translation" , keyValueMap , true , true ) ;
46+ }
47+ translationsUpdated . set ( true ) ;
48+ } ) ;
2549}
2650
2751export function getI18n ( ) : Readable < typeof i18nNext > {
@@ -37,8 +61,11 @@ export type InlineTranslation = {
3761
3862export const currentLanguage = writable ( "de" ) ;
3963i18nNext . on ( "languageChanged" , ( lng ) => {
40- currentLanguage . set ( lng ) ;
64+ const formattedLanguage = lng . split ( "-" ) [ 0 ] ;
65+ currentLanguage . set ( formattedLanguage ) ;
4166} ) ;
67+ export const availableLanguages = writable ( [ "de" ] ) ;
68+ export const translationsUpdated = writable ( undefined ) ;
4269
4370export function translateInlineTranslation (
4471 translation : InlineTranslation | string ,
@@ -63,3 +90,10 @@ export const t = derived(currentLanguage, (currentLanguage: string) => {
6390 return translateInlineTranslation ( key , { language : currentLanguage } ) ;
6491 } ;
6592} ) ;
93+
94+ export function isInlineTranslation ( obj : InlineTranslation ) : obj is InlineTranslation {
95+ if ( ! ( obj instanceof Object ) ) return false ;
96+ for ( const [ key , value ] of Object . entries ( obj ) )
97+ if ( typeof key !== "string" || typeof value !== "string" ) return false ;
98+ return true ;
99+ }
0 commit comments