Documentation
supastarter for Next.jsAPI

Use locale in API endpoints

Learn how to use the users locale in your supastarter API endpoints.

Sometimes you need to use the users locale in your API endpoints. For example, you might want to fetch data from a database that is localized or if you want to send an email in the users locale.

To make the users locale available in your endpoint handler, you can use the localeMiddleware.

import { localeMiddleware } from "../middleware/locale";
 
export const emailRouter = new Hono()
    .basePath("/send-localized-email")
    .get("/",
        localeMiddleware,
        // ...
        async (c) => {
            const locale = c.get("locale");
            // do something with the locale
        });

This middleware will either provide the locale from the locale cookie of the user or the default locale of the application.

On this page

No Headings