@elsoul/fresh-i18n
@elsoul/fresh-i18n
is a simple and flexible internationalization (i18n) plugin
for Deno's Fresh framework. It allows you to easily manage multiple languages in
your Fresh app using JSON-based translations and locale detection.
useTranslation()
to fetch translations and
useLocale()
to get or change the current locale.import { i18nPlugin } from 'jsr:@elsoul/fresh-i18n'
import { i18nPlugin } from 'https://deno.land/x/fresh_i18n/mod.ts'
In your main.ts
file, register the plugin, specifying the available locales
and default language, along with any namespaces you wish to use.
import { App, fsRoutes, staticFiles, trailingSlashes } from 'fresh' import { i18nPlugin } from '@elsoul/fresh-i18n' export const app = new App({ root: import.meta.url, }) .use(staticFiles()) // Serve static files .use(trailingSlashes('never')) // Do not add trailing slashes to URLs .use(i18nPlugin({ languages: ['en', 'ja'], // Supported languages defaultLocale: 'en', // Default language localesDir: './locales', // Directory path to JSON files }, ['common', 'homepage'])) // Example of namespace customization await fsRoutes(app, { loadIsland: (path) => import(`./islands/${path}`), loadRoute: (path) => import(`./routes/${path}`), }) if (import.meta.main) { await app.listen() }
In the locales
directory, create folders for each locale and JSON files for
each namespace.
locales/en/common.json
{ "welcome": "Welcome", "title": "Home" }
locales/ja/common.json
{ "welcome": "ようこそ", "title": "ホーム" }
Use the useTranslation()
and useLocale()
hooks in your components to fetch
translations and switch between locales dynamically.
import { useLocale, useTranslation } from '@elsoul/fresh-i18n' export default function Home() { const { t } = useTranslation('common') // Use the "common" namespace const { locale, changeLanguage } = useLocale() return ( <div> <h1>{t('title')}</h1> {/* Outputs 'Home' or 'ホーム' */} <p>{t('welcome')}</p> {/* Outputs 'Welcome' or 'ようこそ' */} <p>Current language: {locale}</p> <button onClick={() => changeLanguage('en')}>English</button> <button onClick={() => changeLanguage('ja')}>日本語</button> </div> ) }
Use the changeLanguage()
function from the useLocale()
hook to dynamically
switch between languages.
const { changeLanguage } = useLocale() changeLanguage('ja') // Switch to Japanese
Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.
This package is open-source and available under the Apache-2.0 License.
Add Package
deno add jsr:@elsoul/fresh-i18n
Import symbol
import * as fresh_i__n from "@elsoul/fresh-i18n";
---- OR ----
Import directly with a jsr specifier
import * as fresh_i__n from "jsr:@elsoul/fresh-i18n";
Add Package
npx jsr add @elsoul/fresh-i18n
Import symbol
import * as fresh_i__n from "@elsoul/fresh-i18n";
Add Package
yarn dlx jsr add @elsoul/fresh-i18n
Import symbol
import * as fresh_i__n from "@elsoul/fresh-i18n";
Add Package
pnpm dlx jsr add @elsoul/fresh-i18n
Import symbol
import * as fresh_i__n from "@elsoul/fresh-i18n";
Add Package
bunx jsr add @elsoul/fresh-i18n
Import symbol
import * as fresh_i__n from "@elsoul/fresh-i18n";