generated from DTS-STN/next-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from DTS-STN/serverSideTranslations-wrapper
refactor: next-i18next serverSideTranslations wrapper
- Loading branch information
Showing
5 changed files
with
67 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { FlatNamespace } from 'i18next' | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations' | ||
|
||
import nextI18nextConfig from '../../../next-i18next.config' | ||
|
||
/** | ||
* The default namespaces for translations. | ||
*/ | ||
export const defaultNamespaces: Array<FlatNamespace> = ['common'] | ||
|
||
/** | ||
* Retrieves namespaces based on the provided requirements. | ||
* Note: The namespace from {@link defaultNamespaces} will always be added. | ||
* @param namespacesRequired The namespaces required. | ||
* @returns The retrieved namespaces. | ||
*/ | ||
export const getNamespaces = ( | ||
namespacesRequired: | ||
| Readonly<FlatNamespace> | ||
| ReadonlyArray<FlatNamespace> | ||
| undefined = undefined | ||
) => { | ||
// default with namespaces to always needed | ||
const namespaces: Array<FlatNamespace> = defaultNamespaces | ||
|
||
if (!namespacesRequired) { | ||
return namespaces | ||
} | ||
|
||
if (typeof namespacesRequired === 'string') { | ||
return [...new Set([...namespaces, namespacesRequired])] | ||
} | ||
|
||
namespacesRequired | ||
return [...new Set([...namespaces, ...namespacesRequired])] | ||
} | ||
|
||
/** | ||
* A wrapper function for server-side translations using next-i18next. | ||
* @param locale - The locale to use for translations. If not provided, the default locale specified in `nextI18nextConfig` will be used. | ||
* @param namespacesRequired - The namespaces required for translations. It can be a single `FlatNamespace` or an array of `FlatNamespace`. If not provided, {@link defaultNamespaces} will be used. | ||
* @returns {Promise<object>} - A Promise that resolves to an object containing the translations for the specified locale and namespaces. | ||
*/ | ||
export const pageWithServerSideTranslations = async ( | ||
locale?: string, | ||
namespacesRequired: | ||
| FlatNamespace | ||
| Array<FlatNamespace> | ||
| undefined = undefined | ||
) => { | ||
return serverSideTranslations( | ||
locale ?? nextI18nextConfig.i18n.defaultLocale, | ||
getNamespaces(namespacesRequired), | ||
nextI18nextConfig | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters