Functions
These functions are designed for server context only. Since there are different functions for various purposes, make sure to check each one to understand its specific application.
createTranslator
The createTranslator function returns a translator function which is used to get translated messages for the given locale and namespace.
This function can be used outside of components where the current locale cannot be inferred through the rendering context, such as in Server Actions.
"use server";
import { createTranslator, Locale } from "next-globe-gen";
export async function serverAction(locale: Locale, formData: FormData) {
const t = createTranslator(locale, "dashboard");
/* Other server action logic*/
}
"use client";
import { useLocale } from "next-globe-gen";
import { serverAction } from "./actions";
export function ServerActionForm() {
const locale = useLocale();
const serverActionWithLocale = serverAction.bind(null, locale);
return <form action={serverActionWithLocale}>{/* Inputs */}</form>;
}
Parameters
createTranslator(locale: Locale, namespace?: Namespace);
| Param | Type | Required | Description |
|---|---|---|---|
locale | Locale | Yes | Access messages in the given locale |
namespace | Namespace | - | Access messages by a given namespace |
Namespace of some deeply nested messages object is it's full object dot notation syntax as a string, possibly prefixed with a filename namespace. (i.e. filename.deeply.nested.object)
Returns
createTranslator returns a t function.
t function
t function is returned by the useTranslations hook and the createTranslator function. It is used to translate the messages in the given namespace.
Parameters
t<N extends Namespace, K extends NamespaceKey<N>>(key: K, args?: MessageArguments<Message<N, K>>);
| Param | Type | Required | Description |
|---|---|---|---|
key | NamespaceKey<N> | Yes | Access a message by a key in the given namespace |
args | MessageArguments<Message<N, K>> | * | Arguments for the message interpolation patterns |
* args param is required only if the given message has interpolation patterns.
Key of the message is it's full object dot notation syntax as a string, possibly prefixed with a filename namespace. (i.e. filename.deeply.nested.object.key)
Returns
t function returns a translated and interpolated message.
createHref
The createHref function can be used to construct a localized href of a route outside of components. This function is almost equivalent to the useHref hook, but the locale is required to be passed since it cannot be inferred from rendering context.
Parameters
/* 1. With static route */
createHref<R extends StaticRoute>(route: R, locale: Locale);
/* 2. With dynamic route */
createHref<R extends StaticRoute>(route: R, params: RouteParams<R>, locale: Locale);
/* 3. With route options object */
createHref<R extends CreateHrefOptions<Route>>({ pathname, params, locale, query }: R);
Check useHref section what values should be given in the CreateHrefOptions object accepts the same options as the HrefOptions but locale is required.
Returns
createHref returns the localized version of the given route.
redirect
The redirect function extends the Next.js redirect function to allow you to redirect the user to localized pathnames with 307 status code.
Parameters
/* 1. With static route */
redirect<R extends StaticRoute>(route: R, opts?: { locale?: Locale, type?: string })
/* 2. With dynamic route */
redirect<R extends DynamicRoute>(route: R, opts: { params: RouteParams<R>, locale?: Locale, type?: string })
/* 3. With route options object */
redirect<R extends HrefOptions<Route>>(route: R, opts?: { type?: string })
Check useHref section what values should be given in the HrefOptions object.
redirect in Route Handlers and Server ActionsWhen redirect is used in Route Handlers or Server Actions, the locale needs to be provided explicitly in options, since it cannot be inferred through the rendering context as in Server Components.
Returns
redirect does not return a value.
permanentRedirect
The permanentRedirect function extends the Next.js permanentRedirect function to allow you to redirect the user to localized pathnames with 308 status code.
Parameters
/* 1. With static route */
permanentRedirect<R extends StaticRoute>(route: R, opts?: { locale?: Locale, type?: string })
/* 2. With dynamic route */
permanentRedirect<R extends DynamicRoute>(route: R, opts: { params: RouteParams<R>, locale?: Locale, type?: string })
/* 3. With route options object */
permanentRedirect<R extends HrefOptions<Route>>(route: R, opts?: { type?: string })
Check useHref section what values should be given in the HrefOptions object.
permanentRedirect in Route Handlers and Server ActionsWhen permanentRedirect is used in Route Handlers or Server Actions, the locale needs to be provided explicitly in options, since it cannot be inferred through the rendering context as in Server Components.
Returns
permanentRedirect does not return a value.