-
Notifications
You must be signed in to change notification settings - Fork 22
/
serverSideTranslations.ts
41 lines (36 loc) · 1.51 KB
/
serverSideTranslations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Separate file so it's loaded only on the server.
import { CreateClientReturn } from 'next-i18next'
// @ts-ignore
import { createConfig } from 'next-i18next/dist/commonjs/config/createConfig'
// @ts-ignore
import { default as createClient } from 'next-i18next/dist/commonjs/createClient/node'
import { serverSideTranslations as _serverSideTranslations } from 'next-i18next/serverSideTranslations'
// Use English as default locale.
export const serverSideTranslations = async (
initialLocale?: string,
namespacesRequired?: string[] | undefined
) => _serverSideTranslations(initialLocale ?? 'en', namespacesRequired)
// Create t function for use in server side props loading.
export const serverSideTranslationsWithServerT = async (
initialLocale?: string,
namespacesRequired?: string[] | undefined
) => {
const i18nProps = await serverSideTranslations(
initialLocale,
namespacesRequired
)
// For some reason, the T function on the server is not immediately loaded
// after awaiting serverSideTranslations, so let's manually instantiate our
// own version of the client given the config that was loaded by the library.
// https://github.com/i18next/next-i18next/issues/1698#issuecomment-1046754181
const internalConfig = createConfig({
...i18nProps._nextI18Next.userConfig,
lng: i18nProps._nextI18Next.initialLocale,
})
const client: CreateClientReturn = await createClient(internalConfig)
const serverT = await client.i18n.init(await client.initPromise)
return {
i18nProps,
serverT,
}
}