Skip to content

Commit 5a0fde7

Browse files
Fix i18next host toggles for Playwright suite (#4385)
1 parent 8494edd commit 5a0fde7

File tree

11 files changed

+141
-51
lines changed

11 files changed

+141
-51
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { type MouseEvent } from 'react';
2+
import i18nService from 'i18next-shared-lib/lib/i18nService';
3+
4+
import useReactRemoteTranslation from '../i18n/useReactRemoteTranslation';
5+
6+
const ReactRemoteContent = () => {
7+
const { t } = useReactRemoteTranslation('react-remote-main');
8+
9+
const switchLanguage = () => {
10+
i18nService.switchLanguage();
11+
};
12+
13+
const handleButtonClick = (event: MouseEvent<HTMLButtonElement>) => {
14+
event.stopPropagation();
15+
switchLanguage();
16+
};
17+
18+
return (
19+
<div
20+
style={{
21+
border: 'dashed 5px blue',
22+
}}
23+
>
24+
<header>{t('remoteTitle')}</header>
25+
<aside>
26+
<button onClick={handleButtonClick}>{t('changeLanguageButtonLabel')}</button>
27+
</aside>
28+
<main>{t('remoteContent')}</main>
29+
</div>
30+
);
31+
};
32+
33+
export default ReactRemoteContent;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import reactRemoteMain from './react-remote-main.json';
2+
3+
const TranslationsEN = {
4+
'react-remote-main': reactRemoteMain,
5+
};
6+
7+
export default TranslationsEN;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"remoteTitle": "React Remote : Title",
3+
"changeLanguageButtonLabel": "React Remote : change language",
4+
"remoteContent": "React Remote : I'm the remote child !"
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import reactRemoteMain from './react-remote-main.json';
2+
3+
const TranslationsFR = {
4+
'react-remote-main': reactRemoteMain,
5+
};
6+
7+
export default TranslationsFR;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"remoteTitle": "React Remote : Titre",
3+
"changeLanguageButtonLabel": "React Remote : changer la langue",
4+
"remoteContent": "React Remote : Je suis le remote child"
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import useInstanceTranslation from 'i18next-shared-lib/lib/useInstanceTranslation';
2+
3+
import TranslationsEN from './react-remote/en';
4+
import TranslationsFR from './react-remote/fr';
5+
6+
const useReactRemoteTranslation = useInstanceTranslation('react-remote', {
7+
en: TranslationsEN,
8+
fr: TranslationsFR,
9+
});
10+
11+
export default useReactRemoteTranslation;

i18next-nextjs-react/next-host/next.config.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

i18next-nextjs-react/next-host/pages/_app.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import '../styles/globals.css';
22
import type { AppProps } from 'next/app';
3+
import { useEffect } from 'react';
34

45
function MyApp({ Component, pageProps }: AppProps) {
6+
useEffect(() => {
7+
const selectors = ['style[data-next-hide-fouc="true"]', 'noscript[data-next-hide-fouc="true"]'];
8+
9+
selectors.forEach((selector) => {
10+
document.querySelectorAll(selector).forEach((element) => element.remove());
11+
});
12+
13+
if (document.body.style.display === 'none') {
14+
document.body.style.removeProperty('display');
15+
}
16+
}, []);
17+
518
return <Component {...pageProps} />;
619
}
720

i18next-nextjs-react/next-host/pages/index.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import type { NextPage } from 'next';
2-
import useNextHostTranslation from '../i18n/useNextHostTranslation';
32
import dynamic from 'next/dynamic';
4-
const ReactRemoteContent = dynamic(() => import('reactRemote/Content'), { ssr: false });
3+
import { useEffect, useState, type MouseEvent } from 'react';
54
import i18nService from 'i18next-shared-lib/lib/i18nService';
65

7-
console.log(__webpack_share_scopes__);
8-
setTimeout(() => console.log(__webpack_share_scopes__), 1000);
6+
import useNextHostTranslation from '../i18n/useNextHostTranslation';
7+
8+
const ReactRemoteContent = dynamic(() => import('../components/ReactRemoteContent'), { ssr: false });
99

1010
const Home: NextPage = () => {
1111
const { t } = useNextHostTranslation('next-main');
12+
const [isRemoteVisible, setIsRemoteVisible] = useState(false);
13+
14+
useEffect(() => {
15+
setIsRemoteVisible(true);
16+
}, []);
17+
1218
const switchLanguage = () => {
1319
i18nService.switchLanguage();
1420
};
21+
22+
const handleSectionClick = () => {
23+
switchLanguage();
24+
};
25+
26+
const handleButtonClick = (event: MouseEvent<HTMLButtonElement>) => {
27+
event.stopPropagation();
28+
switchLanguage();
29+
};
1530
return (
1631
<main
1732
style={{
@@ -27,16 +42,24 @@ const Home: NextPage = () => {
2742
<h1>Next Host</h1>
2843
</header>
2944
<section
45+
onClick={handleSectionClick}
3046
style={{
3147
marginBottom: 10,
48+
cursor: 'pointer',
3249
}}
3350
>
3451
<p>{t('mainText')}</p>
35-
<button onClick={switchLanguage}>{t('changeLanguageButtonLabel')}</button>
36-
</section>
37-
<section>
38-
<h2>{`${t('remoteChildTitle')} :`}</h2>
39-
<ReactRemoteContent />
52+
<button onClick={handleButtonClick}>{t('changeLanguageButtonLabel')}</button>
53+
{isRemoteVisible && (
54+
<div
55+
style={{
56+
marginTop: 10,
57+
}}
58+
>
59+
<h2>{`${t('remoteChildTitle')} :`}</h2>
60+
<ReactRemoteContent />
61+
</div>
62+
)}
4063
</section>
4164
</main>
4265
);

i18next-nextjs-react/react-host/src/App.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { type MouseEvent } from 'react';
22
import { createRoot } from 'react-dom/client';
33

44
import './index.css';
@@ -12,6 +12,15 @@ const App = () => {
1212
const switchLanguage = () => {
1313
i18nService.switchLanguage();
1414
};
15+
16+
const handleSectionClick = () => {
17+
switchLanguage();
18+
};
19+
20+
const handleButtonClick = (event: MouseEvent<HTMLButtonElement>) => {
21+
event.stopPropagation();
22+
switchLanguage();
23+
};
1524
return (
1625
<main
1726
style={{
@@ -27,16 +36,22 @@ const App = () => {
2736
<h1>React Host</h1>
2837
</header>
2938
<section
39+
onClick={handleSectionClick}
3040
style={{
3141
marginBottom: 10,
42+
cursor: 'pointer',
3243
}}
3344
>
3445
<p>{t('mainText')}</p>
35-
<button onClick={switchLanguage}>{t('changeLanguageButtonLabel')}</button>
36-
</section>
37-
<section>
38-
<h2>{`${t('remoteChildTitle')} :`}</h2>
39-
<ReactRemoteContent />
46+
<button onClick={handleButtonClick}>{t('changeLanguageButtonLabel')}</button>
47+
<div
48+
style={{
49+
marginTop: 10,
50+
}}
51+
>
52+
<h2>{`${t('remoteChildTitle')} :`}</h2>
53+
<ReactRemoteContent />
54+
</div>
4055
</section>
4156
</main>
4257
);

0 commit comments

Comments
 (0)