From 5c1ddf7bbcbb87e9e510ced7d9e979f08d62486f Mon Sep 17 00:00:00 2001 From: KishiTheMechanic Date: Sat, 26 Oct 2024 23:22:40 +0200 Subject: [PATCH] fix --- deno.json | 4 ++-- mod.ts | 2 +- src/Link.tsx | 3 ++- src/i18nPlugin.ts | 2 +- src/useLocale.ts | 11 ++++------- src/useTranslation.ts | 10 +++------- 6 files changed, 13 insertions(+), 19 deletions(-) diff --git a/deno.json b/deno.json index 614ab4b..0644175 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@elsoul/fresh-i18n", - "version": "0.6.0", + "version": "0.6.1", "description": "A simple and flexible internationalization (i18n) plugin for Deno's Fresh framework.", "runtimes": ["deno", "browser"], "exports": "./mod.ts", @@ -17,7 +17,7 @@ "@preact/signals": "npm:@preact/signals@1.3.0", "@preact/signals-core": "npm:@preact/signals-core@1.8.0", "@std/expect": "jsr:@std/expect@1.0.5", - "@std/path": "jsr:@std/path@1.0.7" + "@std/path": "jsr:@std/path@1.0.6" }, "fmt": { "options": { diff --git a/mod.ts b/mod.ts index 6236316..d863396 100644 --- a/mod.ts +++ b/mod.ts @@ -2,6 +2,6 @@ export { i18nPlugin } from '@/src/i18nPlugin.ts' export { useTranslation } from '@/src/useTranslation.ts' export { useLocale } from '@/src/useLocale.ts' export { usePathname } from '@/src/usePathname.ts' -export { Link } from '@/src/Link.tsx' +export { Link } from './src/Link.tsx' export type { TranslationState } from '@/src/types.ts' diff --git a/src/Link.tsx b/src/Link.tsx index e410292..05c90a2 100644 --- a/src/Link.tsx +++ b/src/Link.tsx @@ -1,3 +1,4 @@ +import type { h } from 'preact' import { currentLocale } from '@/src/store.ts' import type { JSX } from 'preact' @@ -17,7 +18,7 @@ type LinkProps = JSX.IntrinsicElements['a'] & { * @param props - The properties for the Link component. * @returns A link component with the current locale prefixed to the href. */ -export function Link({ href, children, ...props }: LinkProps) { +export function Link({ href, children, ...props }: LinkProps): h.JSX.Element { const localizedHref = `/${currentLocale.value}${ href.startsWith('/') ? href : `/${href}` }` diff --git a/src/i18nPlugin.ts b/src/i18nPlugin.ts index d358d20..fe73b33 100644 --- a/src/i18nPlugin.ts +++ b/src/i18nPlugin.ts @@ -1,4 +1,4 @@ -import { join } from 'jsr:@std/path' +import { join } from '@std/path' import { pathname, translationData } from '@/src/store.ts' import type { MiddlewareFn } from '@/src/types.ts' diff --git a/src/useLocale.ts b/src/useLocale.ts index 97386fa..7b08bc7 100644 --- a/src/useLocale.ts +++ b/src/useLocale.ts @@ -5,12 +5,10 @@ import { currentLocale } from '@/src/store.ts' * * @returns An object containing the current locale and a function to change the locale. */ -export function useLocale() { - /** - * Updates the current locale and redirects to the new URL with updated locale in the path. - * - * @param newLocale - The new locale to set (e.g., 'en', 'ja'). - */ +export function useLocale(): { + locale: string + changeLanguage: (newLocale: string) => void +} { const changeLanguage = (newLocale: string) => { if (newLocale === currentLocale.value) return @@ -18,7 +16,6 @@ export function useLocale() { const currentPath = globalThis.location.pathname.split('/').filter(Boolean) const updatedPath = `/${newLocale}/${currentPath.slice(1).join('/')}` - // Redirect to the new path with the updated locale globalThis.location.href = `${globalThis.location.origin}${updatedPath}${globalThis.location.search}` } diff --git a/src/useTranslation.ts b/src/useTranslation.ts index 40aad34..7c7e5fc 100644 --- a/src/useTranslation.ts +++ b/src/useTranslation.ts @@ -6,13 +6,9 @@ import { translationData } from '@/src/store.ts' * @param namespace - The namespace of translations to retrieve (e.g., 'common', 'company'). * @returns An object containing a function to fetch translations by key within the given namespace. */ -export function useTranslation(namespace: string) { - /** - * Fetches the translation for a specific key within the namespace. - * - * @param key - The translation key to retrieve (e.g., 'title', 'welcome'). - * @returns The translated string, or the key itself if no translation is found. - */ +export function useTranslation( + namespace: string, +): { t: (key: string) => string } { const translate = (key: string): string => { return translationData.value[namespace]?.[key] ?? key }