Installations
npm install storybook-react-i18next
Score
48.1
Supply Chain
94.1
Quality
82.1
Maintenance
100
Vulnerability
100
License
Developer
stevensacks
Module System
ESM
Statistics
21 Stars
185 Commits
12 Forks
4 Watching
1 Branches
6 Contributors
Updated on 12 Nov 2024
Languages
TypeScript (64.38%)
JavaScript (35.3%)
HTML (0.32%)
Total Downloads
Cumulative downloads
Total Downloads
11,466,813
Last day
30.1%
19,769
Compared to previous day
Last week
7.9%
95,381
Compared to previous week
Last month
-1.2%
393,011
Compared to previous month
Last year
11.4%
5,016,658
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
4
Dev Dependencies
36
Storybook react-i18next addon
Easy react-i18next Storybook integration.
Required Peer Dependencies:
- storybook -
^8.2.0
- i18next -
^22.0.0 || ^23.0.0
- i18next-browser-languagedetector -
^7.0.0 || ^8.0.0
- i18next-http-backend:
^2.0.0
- react-i18next -
^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0
This Storybook addon assumes your project is already set up with i18next and react-i18next, with all the required packages installed, and that it is properly configured and working.
Installation
Install this addon as a devDependency.
1npm i -D storybook-react-i18next
You will need to install i18next
and react-i18next
as dependencies to your project if they are not already installed.
1npm i -S i18next react-i18next i18next-browser-languagedetector i18next-http-backend
Usage
After installing, follow these 3 steps to enable this addon in Storybook.
main.ts
Insert this addon into your addons array:
1{ 2 addons: [ 3 // other addons... 4 'storybook-react-i18next', 5 ] 6}
i18next.ts
Create a file in your .storybook
folder called i18next.ts
(or whatever you like).
In this file, copy and paste the below code and make whatever modifications you need (paths to resource files, languages, etc.).
1import {initReactI18next} from 'react-i18next'; 2import i18n from 'i18next'; 3import Backend from 'i18next-http-backend'; 4import LanguageDetector from 'i18next-browser-languagedetector'; 5 6const ns = ['common']; 7const supportedLngs = ['en', 'fr', 'ja']; 8const resources = ns.reduce((acc, n) => { 9 supportedLngs.forEach((lng) => { 10 if (!acc[lng]) acc[lng] = {}; 11 acc[lng] = { 12 ...acc[lng], 13 [n]: require(`../public/locales/${lng}/${n}.json`), 14 }; 15 }); 16 return acc; 17}, {}); 18 19i18n.use(initReactI18next) 20 .use(LanguageDetector) 21 .use(Backend) 22 .init({ 23 //debug: true, 24 lng: 'en', 25 fallbackLng: 'en', 26 defaultNS: 'common', 27 ns, 28 interpolation: {escapeValue: false}, 29 react: {useSuspense: false}, 30 supportedLngs, 31 resources, 32 }); 33 34export default i18n;
Refer to the i18next Configuration Options documentation for detailed information about the configuration options.
preview.ts
In your preview.ts
, you need to add the locales
and locale
to initialGlobals
(or globals
if you're not using the latest version of storybook), as well as adding i18n
that you exported from the above file to parameters.
locales
is an object where the keys are the "ids" of the locales/languages and the values are the names you want to display in the dropdown.
locale
is what you want the default locale to be.
1import i18n from './i18next'; 2 3const preview: Preview = { 4 initialGlobals: { 5 locale: 'en', 6 locales: { 7 en: 'English', 8 fr: 'Français', 9 ja: '日本語', 10 }, 11 }, 12 parameters: { 13 i18n, 14 }, 15}; 16 17export default preview;
You can also use full locale strings as keys. It depends on your i18next configuration.
1import i18n from './i18next'; 2 3const preview: Preview = { 4 initialGlobals: { 5 locale: 'en_US', 6 locales: { 7 en_US: 'English (US)', 8 en_GB: 'English (GB)', 9 fr_FR: 'Français', 10 ja_JP: '日本語', 11 }, 12 }, 13 parameters: { 14 i18n, 15 }, 16}; 17 18export default preview;
The locales
object can also have values as an object with keys of title
, left
, or right
.
This is useful if you want to include an emoji flag or some other string to the left or right side.
For example:
1import i18n from './i18next'; 2 3const preview: Preview = { 4 initialGlobals: { 5 locale: "en", 6 locales: { 7 en: {icon: '🇺🇸', title: 'English', right: 'EN'}, 8 fr: {icon: '🇫🇷', title: 'Français', right: 'FR'}, 9 ja: {icon: '🇯🇵', title: '日本語', right: 'JP'}, 10 }, 11 }, 12 parameters: { 13 i18n, 14 }, 15}; 16 17export default preview;
Or something like this:
1import i18n from './i18next'; 2 3const preview: Preview = { 4 initialGlobals: { 5 locale: 'en_US', 6 locales: { 7 en_US: {title: 'English', right: 'US'}, 8 en_GB: {title: 'English', right: 'GB'}, 9 fr_FR: {title: 'Français', right: 'FR'}, 10 ja_JP: {title: '日本語', right: 'JP'}, 11 }, 12 }, 13 parameters: { 14 i18n, 15 }, 16}; 17 18export default preview;
Story Parameters Locale
If you want to have a story use a specific locale, set it in that Story's parameters.
1export const Default: StoryObj = { 2 render: () => <YourComponent/>, 3}; 4 5export const Japanese: StoryObj = { 6 parameters: { 7 locale: 'ja', 8 }, 9 render: () => <YourComponent/>, 10};
Note that doing this switches the current locale to the parameter one, so when you change to a story without a parameter, it will stay at the last selected locale.
In the above example, if you view the Japanese story and then click back on the Default story, the locale will stay ja
.
Once you have finished these steps and launch storybook, you should see a globe icon in the toolbar.
Clicking this globe icon will show a dropdown with the locales you defined in parameters
.
Switching locales will use the strings defined in your locale json files.
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
no dangerous workflow patterns detected
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/stevensacks/storybook-react-i18next/release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/stevensacks/storybook-react-i18next/release.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:22
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
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
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
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
11 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
Score
2.5
/10
Last Scanned on 2024-11-11
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