i18next / next-app-dir-i18next-example

Next.js 13/14 app directory feature in combination with i18next
https://locize.com/blog/next-app-dir-i18n/
175 stars 36 forks source link

outside of react component does not work ? #36

Open xian107 opened 8 months ago

xian107 commented 8 months ago

// 'lng' how to get it // Do not use cookies http.js const isServer = typeof window === 'undefined'; const http = axios.create({ baseURL: process.env.REACT_APP_API_PATH, timeout: 60000, }); http.interceptors.response.use( async (response) => { if (error.response?.status === 4005) { if(isServer){ const { useTranslation } = (await import('@/app/i18n/index')); // 'lng' how to get it const { t } = useTranslation(lng, 'h-page'); message.error(t('K9')); // please login again }else{ const { useTranslation } = (await import('@/app/i18n/client')); // 'lng' how to get it const { t } = useTranslation(lng, 'h-page'); message.error(t('K9')); // please login again }
} })

order.tsx import http from '@/app/http'; import React from 'react' export default function Order() { http.get('/api/order/list').then(res => {}) return (

) }

xian107 commented 8 months ago

@adrai @jamuhl @davidknezic @omar2205

adrai commented 8 months ago

in general: https://github.com/i18next/next-i18next/issues/2079#issuecomment-1386970151 https://github.com/i18next/next-i18next/issues/2171

on client side you can do this:

import i18next from 'i18next'
// ...
i18next.t('mykey')

but on server side you always need a way to get the language first...