Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const App = () => {
<div class="flex flex-col gap-4">
<h1>
<Trans
key="welcome"
key="nested.welcome"
fallback="Welcome, <bold>{{name}}</bold> (<bold>fallback</bold>)!"
replace={{ name: name() }}
components={{
Expand Down
14 changes: 14 additions & 0 deletions example/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import "i18next";

// import type lang from "./locale/en/lang.json";
// import type translation from "./locale/en/translation.json";

// declare module "i18next" {
// interface CustomTypeOptions {
// defaultNS: "translation";
// resources: {
// lang: typeof lang;
// translation: typeof translation;
// };
// }
// }
4 changes: 3 additions & 1 deletion example/locale/de/translation.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"welcome": "Willkommen, <bold>{{name}}</bold>!"
"nested": {
"welcome": "Willkommen, <bold>{{name}}</bold>!"
}
}
4 changes: 3 additions & 1 deletion example/locale/en/translation.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"welcome": "Welcome, <bold>{{name}}</bold>!"
"nested": {
"welcome": "Welcome, <bold>{{name}}</bold>!"
}
}
30 changes: 20 additions & 10 deletions src/trans.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import type { TFunction, TOptions } from "i18next";
import type { Namespace, ParseKeys, TFunction, TOptions } from "i18next";
import { type Component, createMemo, type JSX } from "solid-js";
import { useTranslation } from "./use-translation.ts";

export type TransEmbeddedComponent = Component<{ children?: string }>;

export type TransProps = {
key: string;
export type TransProps<
Key extends ParseKeys<Ns, TOpt, KPrefix>,
Ns extends Namespace,
KPrefix = undefined,
TOpt extends TOptions = TOptions,
> = {
key: Key | Key[];
fallback?: string;

components?: Record<string, TransEmbeddedComponent>;
replace?: TOptions["replace"];
replace?: TOpt["replace"];

t?: TFunction;
options?: TOptions;
t?: TFunction<Ns, KPrefix>;
options?: TOpt;
};

export const Trans: Component<TransProps> = (props) => {
export const Trans = <
Key extends ParseKeys<Ns, TOpt, KPrefix>,
Ns extends Namespace,
KPrefix = undefined,
TOpt extends TOptions = TOptions,
>(
props: TransProps<Key, Ns, KPrefix, TOpt>,
): JSX.Element => {
const [contextT] = useTranslation(() => props.options?.ns);

const t = ((...args: Parameters<TFunction>) =>
props.t
? props.t(...(args as Parameters<TFunction>))
: contextT(...(args as Parameters<TFunction>))) as TFunction;
props.t ? props.t(...args) : contextT(...args)) as TFunction;

const mergedOptions = createMemo(
(): TOptions => ({
Expand Down