Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change i18n library from next-translate to next-i18next #10

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module.exports = {
emotionAlias: false,
},
webpackFinal: async (config) => {
config.resolve = {
...config.resolve,
fallback: {
...(config.resolve || {}).fallback,
fs: false,
},
}

config.resolve.plugins = [
...(config.resolve.plugins || []),
new TsconfigPathsPlugin({
Expand Down
48 changes: 22 additions & 26 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import '../styles/globals.css'
import I18nProvider from 'next-translate/I18nProvider'
import { I18nextProvider } from 'react-i18next'
import { ThemeProvider } from '@mui/material'
import { RouterContext } from 'next/dist/shared/lib/router-context'

import i18nConfig from '../i18n.json'
import i18n from '../utils/i18n'
import { i18n as i18nConfig } from '../next-i18next.config'
import { theme } from '../utils/theme'

const namespaces = [...new Set(Object.values(i18nConfig.pages).flat())]
const translations = Object.assign(
{},
...i18nConfig.locales.map((locale) => ({
[locale]: Object.assign(
{},
...namespaces.map((ns) => ({
[ns]: require(`../locales/${locale}/${ns}.json`),
})),
),
})),
)
import { useEffect } from 'react'

export const globalTypes = {
locale: {
Expand All @@ -26,22 +15,29 @@ export const globalTypes = {
defaultValue: i18nConfig.defaultLocale,
toolbar: {
icon: 'globe',
items: [{ value: 'ja', right: 'JA', title: '日本語' }],
items: [
{ value: 'en', right: 'EN', title: 'English' },
{ value: 'ko', right: 'KO', title: '한국어' },
{ value: 'ja', right: 'JA', title: '日本語' },
],
},
},
}

export const decorators = [
(Story, { globals }) => (
<ThemeProvider theme={theme}>
<I18nProvider
lang={globals.locale}
namespaces={translations[globals.locale]}
>
<Story />
</I18nProvider>
</ThemeProvider>
),
(Story, { globals }) => {
useEffect(() => {
i18n.changeLanguage(globals.locale)
}, [globals.locale])

return (
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<Story />
</ThemeProvider>
</I18nextProvider>
)
},
]

export const parameters = {
Expand Down
13 changes: 13 additions & 0 deletions components/SampleLocale/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<SampleLocale /> displayed well 1`] = `
<div>
<p>
English
|
Korean
|
Japanese
</p>
</div>
`;
14 changes: 14 additions & 0 deletions components/SampleLocale/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import { ComponentStory, ComponentMeta } from '@storybook/react'

import { SampleLocale } from '.'

export default {
title: 'Components/SampleLocale',
component: SampleLocale,
parameters: { controls: { disabled: true } },
} as ComponentMeta<typeof SampleLocale>

const Template: ComponentStory<typeof SampleLocale> = () => <SampleLocale />

export const Default = Template.bind({})
49 changes: 49 additions & 0 deletions components/SampleLocale/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useEffect } from 'react'
import { render, screen } from '@testing-library/react'
import { I18nextProvider } from 'react-i18next'
import i18n from 'utils/i18n'

import { SampleLocale } from './index'

interface Props {
readonly locale: 'en' | 'ko' | 'ja'
}
const TestComponent = ({ locale }: Props) => {
useEffect(() => {
i18n.changeLanguage(locale)
}, [locale])

return (
<I18nextProvider i18n={i18n}>
<SampleLocale />
</I18nextProvider>
)
}

describe('<SampleLocale />', () => {
it('displayed well', () => {
const { container, rerender } = render(<TestComponent locale="en" />)

expect(
screen.queryByText('English | Korean | Japanese'),
).toBeInTheDocument()
expect(screen.queryByText('영어 | 한국어 | 일본어')).not.toBeInTheDocument()
expect(screen.queryByText('英語 | 韓国語 | 日本語')).not.toBeInTheDocument()

expect(container).toMatchSnapshot()

rerender(<TestComponent locale="ko" />)
expect(
screen.queryByText('English | Korean | Japanese'),
).not.toBeInTheDocument()
expect(screen.queryByText('영어 | 한국어 | 일본어')).toBeInTheDocument()
expect(screen.queryByText('英語 | 韓国語 | 日本語')).not.toBeInTheDocument()

rerender(<TestComponent locale="ja" />)
expect(
screen.queryByText('English | Korean | Japanese'),
).not.toBeInTheDocument()
expect(screen.queryByText('영어 | 한국어 | 일본어')).not.toBeInTheDocument()
expect(screen.queryByText('英語 | 韓国語 | 日本語')).toBeInTheDocument()
})
})
11 changes: 11 additions & 0 deletions components/SampleLocale/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useTranslation } from 'next-i18next'

export const SampleLocale = () => {
const { t } = useTranslation('common')

return (
<p>
{t('English')} | {t('Korean')} | {t('Japanese')}
</p>
)
}
8 changes: 0 additions & 8 deletions i18n.json

This file was deleted.

3 changes: 0 additions & 3 deletions locales/ja/common.json

This file was deleted.

7 changes: 7 additions & 0 deletions next-i18next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'ko', 'ja'],
localeDetection: false,
},
}
8 changes: 5 additions & 3 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @type {import('next').NextConfig} */
const nextTranslate = require('next-translate')
const { i18n } = require('./next-i18next.config')

module.exports = nextTranslate()
module.exports = {
reactStrictMode: true,
i18n,
}
Loading