-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3ef5ac
commit 87001f2
Showing
6 changed files
with
96 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
import { useSignal } from '@preact/signals' | ||
import { createContext } from 'preact' | ||
import { useContext } from 'preact/hooks' | ||
|
||
const localeSignal = useSignal<string | undefined>(undefined) | ||
interface LocaleContextProps { | ||
locale: string | ||
setLocale: (locale: string) => void | ||
loadNamespaceTranslations: ( | ||
namespace: string, | ||
) => Promise<{ [key: string]: string }> | ||
} | ||
|
||
export function setInitialLocale(initialLocale: string) { | ||
if (localeSignal.value === undefined) { | ||
localeSignal.value = initialLocale | ||
} | ||
const LocaleContext = createContext<LocaleContextProps>({ | ||
locale: 'en', | ||
setLocale: () => {}, | ||
loadNamespaceTranslations: async () => ({}), | ||
}) | ||
|
||
const localeSignal = useSignal('en') | ||
|
||
export function setInitialLocale(locale: string) { | ||
localeSignal.value = locale | ||
} | ||
|
||
export function useLocale() { | ||
if (localeSignal.value === undefined) { | ||
throw new Error( | ||
'Initial locale is not set. Make sure to initialize the locale with i18nPlugin.', | ||
) | ||
} | ||
|
||
const context = useContext(LocaleContext) | ||
return { | ||
locale: localeSignal.value, | ||
setLocale: (newLocale: string) => { | ||
localeSignal.value = newLocale | ||
context.setLocale(newLocale) | ||
}, | ||
loadNamespaceTranslations: context.loadNamespaceTranslations, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { useSignal } from '@preact/signals' | ||
import { useLocale } from './useLocale.ts' | ||
import { useEffect, useState } from 'preact/hooks' | ||
|
||
export function useTranslation(namespace: string) { | ||
const { locale } = useLocale() | ||
const translations = useSignal<{ [key: string]: string }>({}) | ||
const { locale, loadNamespaceTranslations } = useLocale() | ||
const [translations, setTranslations] = useState<Record<string, string>>({}) | ||
|
||
async function loadTranslations() { | ||
try { | ||
const response = await fetch(`/locales/${locale}/${namespace}.json`) | ||
const data: { [key: string]: string } = await response.json() | ||
translations.value = data | ||
} catch (error) { | ||
console.error(`Error loading translations for ${namespace}:`, error) | ||
useEffect(() => { | ||
async function loadTranslations() { | ||
const namespaceTranslations = await loadNamespaceTranslations(namespace) | ||
setTranslations(namespaceTranslations) | ||
} | ||
} | ||
loadTranslations() | ||
}, [namespace, locale]) | ||
|
||
function t(key: string): string { | ||
return translations.value[key] || key | ||
return translations[key] || key | ||
} | ||
|
||
return { t, loadTranslations } | ||
return { t } | ||
} |