Détail du package

i18next-hmr

felixmosh26.7kMIT3.1.4

I18Next HMR🔥 webpack / vite plugin that allows reload translation resources instantly on the client & the server.

i18next, HMR, webpack, vite

readme

i18next-hmr

npm CI

I18Next HMR🔥 webpack / vite plugin that allows reload translation resources instantly on the client & the server.

Requirements

  • Node.js v10 or above
  • Webpack v4.x - v5.x
  • Vite v3

Installation

$ npm install --save-dev i18next-hmr

Usage

Add the plugin to your webpack config (or next.config.js).

// webpack.config.js
const { I18NextHMRPlugin } = require('i18next-hmr/webpack');

module.exports = {
  ...
  plugins: [
    new I18NextHMRPlugin({
      localesDir: path.resolve(__dirname, 'static/locales'), 
      localesDirs: [
         // use this property for multiple locales directories   
      ]
    })
  ]
};
// i18next.config.js
const Backend = require('i18next-http-backend');
const i18next = require('i18next');
const { HMRPlugin } = require('i18next-hmr/plugin');

const instance = i18next.use(Backend); // http-backend is required for client side reloading

if (process.env.NODE_ENV !== 'production') {
   instance.use(new HMRPlugin({
      webpack: {
         client: typeof window !== 'undefined', // enables client side HMR in webpack
         server: typeof window === 'undefined'  // enables server side HMR in webpack
      },
      vite: {
         client: typeof window !== 'undefined', // enables client side HMR in Vite
      }
   }));
}

instance.init(options, callback);

module.exports = instance;

Start the app with NODE_ENV=development

Server side

This lib will trigger i18n.reloadResources([lang], [ns]) on the server side with lang & namespace extracted from the translation filename that was changed.

⚠️ If your server side is bundled using Webpack, the lib will use the native HMR (if enabled), for it to work properly the lib must be bundled, therefore, you should specify the lib as not external. There are 2 ways to do that:

  1. If you are using webpack-node-externals specify i18next-hmr in the whitelist.
  2. (deprecated method) use a relative path to node_modules, something like:
    // server.entry.js
    if (process.env.NODE_ENV !== 'production') {
      const { applyServerHMR } = require('../node_modules/i18next-hmr/server');
      applyServerHMR(i18n);
    }

Client side

The lib will invoke Webpack's / Vite HMR to update client side, that will re-fetch (with cache killer) the updated translation files and trigger i18n.changelanguage(lang) to trigger listeners (which in React apps it will update the UI).

Example

Working examples can be found in the examples folder.

nextjs with next-i18next

screenshot

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v3.1.4

  • fix: vite example 4982c02
  • build(deps-dev): bump webpack in /examples/vue-i18next 44ea573
  • bump deps 1fe020e

v3.1.3

22 August 2024

  • build(deps): bump braces from 3.0.2 to 3.0.3 in /examples/react-i18next adc08ca
  • build(deps): bump braces from 3.0.2 to 3.0.3 84b4871
  • fix: move modification of queryString to try block 5ee107f

v3.1.2

4 April 2024

  • build(deps): bump express from 4.18.2 to 4.19.2 in /examples/vue-i18next ef51cbb
  • fix: typescript types defs 3c0dd84
  • Release 3.1.2 c4f1872

v3.1.1

3 April 2024

  • fix: remove usage of options.backend object since it may not contain queryStringParams dafd1e8
  • Release 3.1.1 87c8e01

v3.1.0

3 April 2024

  • feat: simplify webpack plugin to just inject a loader to user config instead of using NMF, to add support for RSPack da36693
  • build(deps): bump express in /examples/next-with-next-i18next d1ebe30
  • build(deps): bump express in /examples/react-i18next cfea2a8

v3.0.4

14 December 2023

  • Revert "build(deps): bump next in /examples/next-with-next-i18next" 00d3afc
  • build(deps): bump next in /examples/next-with-next-i18next 01045b0
  • build(deps): bump @babel/traverse in /examples/vue-i18next 25c8d9a

v3.0.3

21 September 2023

  • fix: align trigger new location, closes #148 #148
  • Release 3.0.3 faa1754

v3.0.2

18 September 2023

v3.0.1

13 September 2023

  • feat: add vite example b1332c1
  • build(deps): bump tough-cookie in /examples/react-i18next e300f5b
  • build(deps): bump semver from 5.7.1 to 5.7.2 in /examples/vue-i18next 1af7150

v3.0.0

12 September 2023

v2.0.1

22 May 2023

  • fix: make d.ts files express the correct type 92d0b19
  • fix: ignore if passed an empty i18n instance 7221a35
  • Release 2.0.1 4bff183

v2.0.0

22 May 2023

  • feat: use i18next plugin system to get the i18next instance. 2c60c1f
  • Release 2.0.0 7b0dc21

v1.11.4

22 May 2023

v1.11.3

22 May 2023

  • Release 1.11.3 ad67539
  • fix: make localesDirs plugin config an optional 3de3b0c

v1.11.2

18 May 2023

  • fix: add defaultNS as an optional ns value 3a20106
  • Bump webpack from 5.73.0 to 5.76.1 in /examples/react-i18next 290d870
  • Bump json5 from 1.0.1 to 1.0.2 in /examples/vue-i18next e3b4d67

v1.11.1

9 December 2022

  • Bump express from 4.17.1 to 4.18.2 in /examples/vue-i18next f571dc1
  • Bump loader-utils from 1.4.0 to 1.4.1 in /examples/vue-i18next 4cf7127
  • Bump vm2 from 3.9.10 to 3.9.11 187b33c

v1.11.0

16 August 2022

  • feat: pass changed files list to i18next getter, #118 0253f0a
  • Release 1.11.0 f845375

v1.10.0

11 August 2022

  • feat: Add support for watching multiple locales dirs, #116 5302b06
  • Release 1.10.0 ed603da

v1.9.0

11 August 2022

  • feat: add support for options.lng, options.fallbackLng, options.supportedLngs as a language source fae8a57
  • feat: add support for strict language detection ea6a877
  • feat: add support for fallbackNS 3cd4326

v1.8.1

20 July 2022

  • Bump deps 844a4ae
  • Bump terser from 4.8.0 to 4.8.1 in /examples/vue-i18next e88c511
  • Release 1.8.1 99f1de0

v1.8.0

19 July 2022

  • Add next-i18next v11 example, closes #32 #32
  • Update examples e171edc
  • Remove webpack 5 example 0c02c24
  • feat: add the ability to pass a getter for i18n instance 574deab

v1.7.8

19 July 2022

v1.7.7

28 April 2022

  • Bump next from 11.1.3 to 12.1.0 in /examples/next-with-next-i18next f3ec2a1
  • Bump next in /examples/next-with-next-i18next-without-custom-server 796982d
  • Deps bump ecb85f5

v1.7.6

28 April 2022

v1.7.5

17 August 2021

  • Bump dev deps 50e8672
  • Bump next in /examples/next-with-next-i18next-without-custom-server f85c2ba
  • Bump next from 10.2.3 to 11.1.0 in /examples/next-with-next-i18next 2ca15a1

v1.7.4

21 June 2021

v1.7.3

15 June 2021

v1.7.2

2 June 2021

  • Bump y18n in /examples/next-with-next-i18next-without-custom-server #36
  • Bump y18n from 4.0.0 to 4.0.1 in /examples/vue-i18next #35
  • Bump react-dev-utils from 9.1.0 to 11.0.4 in /examples/react-i18next #34
  • Bump y18n from 4.0.0 to 4.0.1 in /examples/next-with-next-i18next #33
  • Bump elliptic from 6.5.3 to 6.5.4 in /examples/razzle-ssr #31
  • Bump elliptic in /examples/next-with-next-i18next-without-custom-server #30
  • Bump elliptic from 6.5.3 to 6.5.4 in /examples/vue-i18next #29
  • Bump elliptic from 6.5.3 to 6.5.4 in /examples/next-with-next-i18next #28
  • Change to yarn.lock 53dc071
  • Bump browserslist from 4.13.0 to 4.16.6 in /examples/vue-i18next be25c2d
  • Sort package.json eace68d

v1.7.1

18 February 2021

v1.7.0

15 January 2021

  • Bump ini from 1.3.5 to 1.3.8 in /examples/razzle-ssr #24
  • Bump ini from 1.3.5 to 1.3.8 in /examples/next-with-next-i18next-without-custom-server #25
  • Add webpack 5 support, closes #26 #26
  • Bump deps 55d8773
  • Bump dev-deps d4604c9
  • Release 1.7.0 c3f397b

v1.6.3

12 December 2020

  • Bump ini from 1.3.5 to 1.3.8 #23
  • Bump ini from 1.3.5 to 1.3.8 in /examples/vue-i18next #22
  • Bump deps 6173642
  • Update issue templates e983cb8
  • Release 1.6.3 448c2ee

v1.6.2

16 October 2020

  • Bump next from 9.5.0 to 9.5.4 in /examples/next-with-next-i18next-without-custom-server #21
  • Bump next in /examples/next-with-next-i18next-without-custom-server 4ee84c8
  • Release 1.6.2 46818c5
  • fix: Webpack 5 doesn't have mtimes at start e7a16e8

v1.6.1

2 October 2020

  • Bump node-fetch from 2.6.0 to 2.6.1 #18
  • Reload translation when using lng-country combination, closes #19 #19
  • Release 1.6.1 ac57a5d

v1.6.0

6 September 2020

  • Add support for change in multiple files at once, closes #17 #17
  • Add an example which uses next-18next without custom server. fc8525f
  • Fix security warning bae4b8f
  • Release 1.6.0 64c95df

v1.5.6

26 July 2020

  • Bump lodash from 4.17.15 to 4.17.19 #13
  • Bump lodash from 4.17.15 to 4.17.19 in /examples/vue-i18next #15
  • Bump lodash from 4.17.15 to 4.17.19 in /examples/next-with-next-i18next #14
  • Update examples deps 8dc0caa
  • Add usage of release it 077944d
  • Release 1.5.6 9325398

v1.5.5

24 June 2020

  • Add support for backslashes paths, closes #12 #12
  • Reformat the code based on prettier 4960885
  • Bump v1.5.5 7ee8ea1
  • Create dependabot.yml 2303562

v1.5.4

18 June 2020

v1.5.3

11 June 2020

v1.5.2

10 June 2020

  • Bump websocket-extensions from 0.1.3 to 0.1.4 in /examples/vue-i18next #7
  • Ignore changes in none loaded namespace, closes #8 #8
  • Bump dev deps 9f76a85
  • Distinguish between name-spaces that containing each other name d8763ff
  • Bump v1.5.2 1a86f9d

v1.5.1

31 May 2020

v1.5.0

25 May 2020

  • Bump next from 9.3.0 to 9.3.2 in /examples/next-with-next-i18next #4
  • Add support for nested namespaces, closes #6 #6
  • Add razzle example 1ea77f3
  • Bump example deps 29c4e77
  • Bump dev deps d2fbd62

v1.4.1

10 March 2020

  • fix: handle cases when IgnoringWatchFileSystem is used as watcher (has wfs field) 1eb90a0
  • Bump v1.4.1 71aa933

v1.4.0

10 March 2020

  • feat: add native HMR support for server side 76adf27
  • fix: handle changes when i18next.language has lang & country d2eba98
  • feat: add native HMR support explanation. 94e29b5

v1.3.0

16 December 2019

v1.2.0

12 December 2019

v1.1.3

10 December 2019

v1.1.2

7 December 2019

v1.1.1

6 December 2019

v1.1.0

6 December 2019

  • Split server & client to separate methods (prevent race condition on server side) 8a96f50
  • Keep only the last server listener (in-case of server re-loaded) 7fed769

v1.0.3

6 December 2019

v1.0.2

6 December 2019

v1.0.1

6 December 2019

  • Add react-i18next example fd86257
  • Add next with next-i18next example 2e993e8
  • Update the package-lock.json 0d53840