Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for useTranslation in an API route (pages/api/*) #40

Closed
natterstefan opened this issue Jun 23, 2021 · 4 comments
Closed

Add support for useTranslation in an API route (pages/api/*) #40

natterstefan opened this issue Jun 23, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@natterstefan
Copy link

natterstefan commented Jun 23, 2021

Is your feature request related to a problem? Please describe.

I am looking for an i18n-library that I can use in my Next app on both the client- and server-side (especially in API routes). At the moment I am using https://github.com/isaachinman/next-i18next, which does not solve the problem. I am also about to test https://github.com/vinissimus/next-translate#gett, which could be useful as well.

But I asked myself if next-intl could solve that problem as well for me.

In my current use case I try to find a way to use https://react-pdf.org/ in my current Next setup.

Describe the solution you'd like

Add pages/api/* support, like:

import useApiTranslations from 'next-intl/useApiTranslations'

export default async function handler(req, res) {
  const t = await useApiTranslations(req.query.__nextLocale, 'common')
  const title = t('title')

  res.statusCode = 200
  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify({ title }))
}

ATM it is not possible to use it for instance because of this line:

import {useRouter} from 'next/router';

Describe alternatives you've considered

As mentioned above, the idea came to me after seeing the getT feature in next-translate.

@natterstefan natterstefan added the enhancement New feature or request label Jun 23, 2021
@amannn
Copy link
Owner

amannn commented Jun 24, 2021

Hi, and thanks for the report! 🙂

As discussed via chat yesterday, the lower level library of next-intl, called use-intl, might be a good fit for react-pdf.

Can you try it out and let me know if it works for you?

@natterstefan
Copy link
Author

Hi @amannn,

yes, I will try it as discussed. I'll let you know about the results.

Thanks for your help.

@natterstefan
Copy link
Author

Hi @amannn,

I created an example and it works! 🚀

Thanks.

@amannn
Copy link
Owner

amannn commented Jun 27, 2021

Really cool, thanks for sharing! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants