question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the ```t``` function in an API call on Vercel?

See original GitHub issue

Hey,

I use next-i18next to translate my app and it works really great, but I can’t figure out how to use it properly in the API calls. I don’t want to use the locale detection because I send the locale in the request body.

I have an API endpoint like pages/api/image/[id].js. In this file I’m trying to use the t function like #1437 solution:

import { i18n } from 'next-i18next';
const createImage = async ( req, res ) => {
  i18n?.init() // Initialize i18n
  i18n?.changeLanguage('en')
  
  const translated = i18n?.t('key')
  // ...
}

This code works well on my local machine, but after deployment on Vercel the “translated” variable always equals “undefined”. I also didn’t find an answer in the #918 issue.

A few notes:

  1. Of course, I use the appWithTranslation HOC.
  2. This API endpoint can be called directly (outside of my app) like site.com/api/image/12345_en.
  3. I use this code for image generation (open graph and others).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
isaachinmancommented, May 30, 2022

Please note that you can use createClient from next-i18next and instantiate an i18next instance yourselves.

0reactions
rolznzcommented, Nov 7, 2022

Do you seriously expect people to have to include from the dist file?

I managed to get it working locally using https://github.com/i18next/i18next-fs-backend

import i18next from "i18next";
import Backend from "i18next-fs-backend";
const i18n = await i18next.use(Backend).init({
    lng: locale,
    ns: "common",
    backend: {
      loadPath: path.join(process.cwd(), "/public/locales/{{lng}}/{{ns}}.json"),
      addPath: path.join(
        process.cwd(),
        "/public/locales/{{lng}}/{{ns}}.missing.json"
      ),
    },
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

REST API – Vercel Docs
A comprehensive guide to using the Vercel REST API and gaining control over the Vercel platform.
Read more >
Serverless Functions – Vercel Docs
To use JavaScript, check the Node.js tab. handler.ts. import { NextApiRequest, NextApiResponse } from 'next'; export default function handler( request: ...
Read more >
Handling Node.js Request Bodies with Vercel
An example of how to parse a request body using Node.js with a Helper property. This function takes a POST request, parses the...
Read more >
How can I use files in Serverless Functions on Vercel?
Learn how to import files inside Serverless Functions on Vercel. ... The example code below is for an api endpoint that you can...
Read more >
Vercel API
When building an integration with Vercel, you can request Vercel Access Tokens to authenticate and use the Vercel API. You must provide an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found