Installations
npm install modified-next-i18next
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=10
Node Version
14.0.0
NPM Version
6.14.4
Score
36.7
Supply Chain
80.6
Quality
74.4
Maintenance
100
Vulnerability
92.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
H3RSKO
Download Statistics
Total Downloads
1,754
Last Day
2
Last Week
9
Last Month
62
Last Year
1,123
GitHub Statistics
8 Commits
1 Watching
1 Branches
1 Contributors
Bundle Size
70.88 kB
Minified
20.22 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0
Package Id
modified-next-i18next@3.0.0
Unpacked Size
90.07 kB
Size
16.37 kB
File Count
40
NPM Version
6.14.4
Node Version
14.0.0
Total Downloads
Cumulative downloads
Total Downloads
1,754
Last day
0%
2
Compared to previous day
Last week
80%
9
Compared to previous week
Last month
51.2%
62
Compared to previous month
Last year
447.8%
1,123
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
8
Dev Dependencies
35
next-i18next
The easiest way to translate your NextJs apps.
If you are using next-i18next in production, please consider sponsoring the package with any amount you think appropriate.
What is this?
Although NextJs provides internationalised routing directly, it does not handle any management of translation content, or the actual translation functionality itself. All NextJs does is keep your locales and URLs in sync.
To complement this, next-i18next
provides the remaining functionality – management of translation content, and components/hooks to translate your React components – while fully supporting SSG/SSR, multiple namespaces, codesplitting, etc.
While next-i18next
uses i18next and react-i18next under the hood, users of next-i18next
simply need to include their translation content as JSON files and don't have to worry about much else.
A live demo is available here. This demo app is the simple example - nothing more, nothing less.
Why next-i18next?
Easy to set up, easy to use: setup only takes a few steps, and configuration is simple.
No other requirements: next-i18next
simplifies internationalisation for your NextJs app without extra dependencies.
Production ready: next-i18next
supports passing translations and configuration options into pages as props with SSG/SSR support.
How does it work?
Your next-i18next.config.js
file will provide configuration for next-i18next
.
After configuration, appWithTranslation
allows us to use the t
(translate) function in our components via hooks.
Then we add serverSideTranslation
to getStaticProps or getServerSideProps (depending on your case) in our page-level components.
Now our NextJs app is fully translatable!
Setup
1. Installation
1yarn add next-i18next
You need to also have react
and next
installed.
2. Translation content
By default, next-i18next
expects your translations to be organised as such:
.
└── public
└── locales
├── en
| └── common.json
└── de
└── common.json
This structure can also be seen in the simple example.
If you want to structure your translations/namespaces in a custom way, you will need to pass modified localePath
and localeStructure
values into the initialisation config.
3. Project setup
First, create a next-i18next.config.js
file in the root of your project. The syntax for the nested i18n
object comes from NextJs directly.
This tells next-i18next
what your defaultLocale
and other locales are, so that it can preload translations on the server:
next-i18next.config.js
1module.exports = { 2 i18n: { 3 defaultLocale: 'en', 4 locales: ['en', 'de'], 5 }, 6}
Now, create or modify your next.config.js
file, by passing the i18n
object into your next.config.js
file, to enable localised URL routing:
next.config.js
1const { i18n } = require('./next-i18next.config') 2 3module.exports = { 4 i18n, 5}
There are three functions that next-i18next
exports, which you will need to use to translate your project:
appWithTranslation
This is a HOC which wraps your _app
:
1import { appWithTranslation } from 'next-i18next' 2 3const MyApp = ({ Component, pageProps }) => <Component {...pageProps} /> 4 5export default appWithTranslation(MyApp)
The appWithTranslation
HOC is primarily responsible for adding a I18nextProvider
.
serverSideTranslations
This is an async function that you need to include on your page-level components, via either getStaticProps
or getServerSideProps
(depending on your use case):
1import { serverSideTranslations } from 'next-i18next/serverSideTranslations' 2 3export async function getStaticProps({ locale }) { 4 return { 5 props: { 6 ...(await serverSideTranslations(locale, ['common', 'footer'])), 7 // Will be passed to the page component as props 8 } 9 } 10}
Note that serverSideTranslations
must be imported from next-i18next/serverSideTranslations
– this is a separate module that contains NodeJs-specific code.
Also, note that serverSideTranslations
is not compatible with getInitialProps
, as it only can execute in a server environment, whereas getInitialProps
is called on the client side when navigating between pages.
The serverSideTranslations
HOC is primarily responsible for passing translations and configuration options into pages, as props.
useTranslation
This is the hook which you'll actually use to do the translation itself. The useTranslation
hook comes from react-i18next
, but can be imported from next-i18next
directly:
1import { useTranslation } from 'next-i18next' 2 3export const Footer = () => { 4 5 const { t } = useTranslation('footer') 6 7 return ( 8 <footer> 9 <p> 10 {t('description')} 11 </p> 12 </footer> 13 ) 14}
4. Declaring namespace dependencies
By default, next-i18next
will send all your namespaces down to the client on each initial request. This can be an appropriate approach for smaller apps with less content, but a lot of apps will benefit from splitting namespaces based on route.
To do that, you can pass an array of required namespaces for each page into serverSideTranslations
. You can see this approach in examples/simple/pages/index.js.
Note: useTranslation
provides namespaces to the component that you use it in. However, serverSideTranslations
provides the total available namespaces to the entire React tree and belongs on the page level. Both are required.
5. Advanced configuration
Passing other config options
If you need to modify more advanced configuration options, you can pass them via next-i18next.config.js
. For example:
1const path = require('path') 2 3module.exports = { 4 i18n: { 5 defaultLocale: 'en', 6 locales: ['en', 'de'], 7 }, 8 localePath: path.resolve('./my/custom/path') 9}
Unserialisable configs
Some i18next
plugins (which you can pass into config.use
) are unserialisable, as they contain functions and other JavaScript primitives.
You may run into this if your use case is more advanced. You'll see NextJs throw an error like:
Error: Error serializing `._nextI18Next.userConfig.use[0].process` returned from `getStaticProps` in "/my-page".
Reason: `function` cannot be serialized as JSON. Please only return JSON serializable data types.
To fix this, you'll need to set config.serializeConfig
to false
, and manually pass your config into appWithTranslation
:
1import { appWithTranslation } from 'next-i18next' 2import nextI18NextConfig from '../next-i18next.config.js' 3 4const MyApp = ({ Component, pageProps }) => <Component {...pageProps} /> 5 6export default appWithTranslation(MyApp, nextI18NextConfig)
1import { serverSideTranslations } from 'next-i18next/serverSideTranslations' 2 3import nextI18NextConfig from '../next-i18next.config.js' 4 5export const getStaticProps = async ({ locale }) => ({ 6 props: { 7 ...await serverSideTranslations(locale, ['common', 'footer'], nextI18NextConfig), 8 } 9})
Options
Key | Default value |
---|---|
defaultNS | 'common' |
localeExtension | 'json' |
localePath | './public/locales' |
localeStructure | '{{lng}}/{{ns}}' |
serializeConfig | true |
strictMode | true |
use (for plugins) | [] |
All other i18next options can be passed in as well.
Notes
For Docker deployment, note that if you use the Dockerfile
from Next.js docs do not forget to copy next.config.js
and next-i18next.config.js
into the Docker image.
COPY --from=builder /app/next.config.js ./next.config.js
COPY --from=builder /app/next-i18next.config.js ./next-i18next.config.js
Contributors
Thanks goes to these wonderful people (emoji key):
Rob Capellini 💻 ⚠️ | Alexander Kachkaev 📢 💬 🤔 💻 ⚠️ | Mathias Wøbbe 💻 🤔 ⚠️ | Lucas Feliciano 🤔 👀 | Ryan Leung 💻 | Nathan Friemel 💻 📖 💡 🤔 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Sponsors
No vulnerabilities found.
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/8 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
48 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2jc-4fpr-4vhg
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-vxf5-wxwp-m7g9
- Warn: Project is vulnerable to: GHSA-9gr3-7897-pp7m
- Warn: Project is vulnerable to: GHSA-25mp-g6fv-mqxx
- Warn: Project is vulnerable to: GHSA-fmvm-x8mv-47mj
- Warn: Project is vulnerable to: GHSA-c59h-r6p8-q9wc
- Warn: Project is vulnerable to: GHSA-g77x-44xx-532m
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-wpg7-2c88-r8xv
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
1.7
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More