Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KishiTheMechanic committed Oct 26, 2024
1 parent 0bd1c03 commit 5c1ddf7
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -17,7 +17,7 @@
"@preact/signals": "npm:@preact/[email protected]",
"@preact/signals-core": "npm:@preact/[email protected]",
"@std/expect": "jsr:@std/[email protected]",
"@std/path": "jsr:@std/[email protected].7"
"@std/path": "jsr:@std/[email protected].6"
},
"fmt": {
"options": {
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 2 additions & 1 deletion src/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { h } from 'preact'
import { currentLocale } from '@/src/store.ts'
import type { JSX } from 'preact'

Expand All @@ -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}`
}`
Expand Down
2 changes: 1 addition & 1 deletion src/i18nPlugin.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
11 changes: 4 additions & 7 deletions src/useLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ 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

currentLocale.value = newLocale
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}`
}
Expand Down
10 changes: 3 additions & 7 deletions src/useTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 5c1ddf7

Please sign in to comment.