generated from hemengke1997/ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc33301
commit 1127b80
Showing
33 changed files
with
623 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
!.vscode/settings.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"i18n-ally.localesPaths": ["src/locales"], | ||
"i18n-ally.keystyle": "nested", | ||
"i18n-ally.namespace": true, | ||
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { | ||
addFile, | ||
editFile, | ||
isBuild, | ||
isServe, | ||
page, | ||
removeDir, | ||
removeFile, | ||
renameDir, | ||
untilBrowserLogAfter, | ||
untilUpdated, | ||
viteTestUrl, | ||
} from '~utils' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
describe('e2e', () => { | ||
test('should render en by default', async () => { | ||
expect(await page.textContent('#language')).toBe('en') | ||
}) | ||
|
||
test.runIf(isServe)('should lazyload locale js after click', async () => { | ||
let request = page.waitForResponse((res) => res.url().includes('@i18n/virtual:i18n-zh'), { | ||
timeout: 500, | ||
}) | ||
await page.click('#zh') | ||
let response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
|
||
request = page.waitForResponse((res) => res.url().includes('@i18n/virtual:i18n-de'), { | ||
timeout: 500, | ||
}) | ||
await page.click('#de') | ||
response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
}) | ||
|
||
test('should change language', async () => { | ||
await page.click('#zh') | ||
|
||
await untilUpdated(() => page.textContent('#language'), '中文') | ||
|
||
await page.click('#de') | ||
|
||
await untilUpdated(() => page.textContent('#language'), 'Deutsch') | ||
|
||
await page.click('#en') | ||
}) | ||
|
||
test('should set html attribute lang', async () => { | ||
expect(await page.getAttribute('html', 'lang')).toBe('en') | ||
|
||
await page.click('#zh') | ||
|
||
expect(await page.getAttribute('html', 'lang')).toBe('zh') | ||
|
||
await page.click('#de') | ||
|
||
expect(await page.getAttribute('html', 'lang')).toBe('de') | ||
|
||
await page.click('#en') | ||
}) | ||
|
||
test('should set url query', async () => { | ||
await page.click('#zh') | ||
let currentUrl = page.url() | ||
let urlSearchParams = new URLSearchParams(currentUrl.split('?')[1]) | ||
let lang = urlSearchParams.get('lang') | ||
expect(lang).toBe('zh') | ||
|
||
await page.click('#en') | ||
currentUrl = page.url() | ||
urlSearchParams = new URLSearchParams(currentUrl.split('?')[1]) | ||
lang = urlSearchParams.get('lang') | ||
expect(lang).toBe('en') | ||
}) | ||
|
||
test('should fallback to fallbackLng when language is not found', async () => { | ||
await untilBrowserLogAfter( | ||
() => page.goto(`${viteTestUrl}/?lang=not-exist`), | ||
/.*Language 'not-exist' is detected.*/, | ||
) | ||
}) | ||
}) | ||
|
||
describe.skipIf(isBuild)('server related tests', () => { | ||
describe('hmr', () => { | ||
test('should trigger hmr when locale files changed', async () => { | ||
await page.click('#en') | ||
editFile('src/locales/en/test.json', (text) => { | ||
return text.replace(`"key": "en"`, `"key": "updated en"`) | ||
}) | ||
|
||
await untilUpdated(() => page.textContent('#language'), 'updated en') | ||
}) | ||
|
||
test('should page reload when locale dir removed', async () => { | ||
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 }) | ||
removeDir('src/locales/zh-tw/') | ||
const response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
}) | ||
|
||
test('should page reload when locale files removed', async () => { | ||
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 }) | ||
removeFile('src/locales/de/test.json') | ||
const response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
}) | ||
|
||
test('should page reload when locale dir name changed', async () => { | ||
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 }) | ||
|
||
renameDir('src/locales/en/', 'src/locales/en-US/') | ||
let response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
renameDir('src/locales/en-US/', 'src/locales/en/') | ||
response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
}) | ||
|
||
test('should page reload when locale file added', async () => { | ||
const request = page.waitForResponse(/src\/App\.tsx$/, { timeout: 500 }) | ||
addFile('src/locales/en/test.json', '{}') | ||
const response = await request.then(() => ({ status: () => 1 })) | ||
expect(response.status()).toBe(1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>vite-plugin-i18n-detector-demo</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "spa", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "cross-env DEBUG=vite-plugin-i18n* vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"i18next": "^23.5.1", | ||
"i18next-browser-languagedetector": "^7.1.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-i18next": "^13.2.2", | ||
"vite-plugin-i18n-detector": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.2.28", | ||
"@types/react-dom": "^18.2.13", | ||
"@vitejs/plugin-react": "^4.1.0", | ||
"cross-env": "^7.0.3", | ||
"typescript": "^5.2.2", | ||
"vite": "^4.4.11" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#root { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
padding: 2rem; | ||
text-align: center; | ||
} | ||
|
||
.logo { | ||
height: 6em; | ||
padding: 1.5em; | ||
will-change: filter; | ||
transition: filter 300ms; | ||
} | ||
.logo:hover { | ||
filter: drop-shadow(0 0 2em #646cffaa); | ||
} | ||
.logo.react:hover { | ||
filter: drop-shadow(0 0 2em #61dafbaa); | ||
} | ||
|
||
@keyframes logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
a:nth-of-type(2) .logo { | ||
animation: logo-spin infinite 20s linear; | ||
} | ||
} | ||
|
||
.card { | ||
padding: 2em; | ||
} | ||
|
||
.read-the-docs { | ||
color: #888; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useReducer } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import './App.css' | ||
|
||
function App() { | ||
const { t, i18n } = useTranslation() | ||
|
||
const [, update] = useReducer((x) => ++x, 0) | ||
|
||
return ( | ||
<div className='App'> | ||
<h2>{t('test.tip')}</h2> | ||
|
||
<div id='language' style={{ marginBottom: '16px' }}> | ||
{t('test.key')} | ||
</div> | ||
|
||
<button | ||
onClick={async () => { | ||
await i18n.changeLanguage('zh') | ||
update() | ||
}} | ||
id='zh' | ||
style={{ marginRight: 8 }} | ||
> | ||
中文 | ||
</button> | ||
<button | ||
onClick={async () => { | ||
await i18n.changeLanguage('en') | ||
update() | ||
}} | ||
id='en' | ||
style={{ marginRight: 8 }} | ||
> | ||
english | ||
</button> | ||
<button | ||
onClick={async () => { | ||
await i18n.changeLanguage('de') | ||
update() | ||
}} | ||
id='de' | ||
style={{ marginRight: 8 }} | ||
> | ||
Deutsch | ||
</button> | ||
<button | ||
onClick={async () => { | ||
await i18n.changeLanguage('zh-tw') | ||
update() | ||
}} | ||
id='zh-tw' | ||
style={{ marginRight: 8 }} | ||
> | ||
繁體 | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
export default App |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const lookupTarget = 'lang' | ||
export const fallbackLng = 'en' |
Oops, something went wrong.