Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 8, 2023
1 parent 304cc5f commit d8cf0d8
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 97 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Github Pages Deploy # 指定action的名字

on:
push: # 指定触发事件
branches:
- master # 指定触发 action 的分支

# 允许一个并发的部署
concurrency:
group: pages
cancel-in-progress: true

jobs:
main:
runs-on: ubuntu-latest
steps:
# 拉取github仓库代码
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Install pnpm
uses: pnpm/action-setup@v2.0.1
with:
version: 6.31.0

# 执行依赖安装
- name: 安装依赖
run: |
pnpm install --no-frozen-lockfile
# 执行构建步骤
- name: 构建
run: |
pnpm run build:pages
# 执行部署
- name: 部署
uses: JamesIves/github-pages-deploy-action@releases/v3 # 这个action会根据配置自动推送代码到指定分支
with:
ACCESS_TOKEN: ${{ secrets.ghb_token }} # 指定密钥
BRANCH: gh-pages # #指定推送到的远程分支
FOLDER: playground-dist # 指定构建之后要推送哪个目录的代码
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
playground-dist
122 changes: 122 additions & 0 deletions README-en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# vite-plugin-i18n-detector

> Lazyloadable i18n locales detector
## Features

- Unawared DX
- **Lazyload** locale resource
- Options like 'i18n-ally'

## Install

```bash
pnpm add vite-plugin-i18n-detector
```

## Example

### vite.config.ts
```ts
import path from 'path'
import { defineConfig } from 'vite'
import { i18nDetector } from 'vite-plugin-i18n-detector'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
i18nDetector({
localesPaths: [path.join(__dirname, './src/locale')],
pathMatcher: '{locale}/{namespaces}.{ext}',
enabledParsers: ['json', 'json5'],
}),
],
})

```

### i18next example

```tsx
import ReactDOM from 'react-dom/client'
import i18next from 'i18next'
import { initReactI18next } from 'react-i18next'
import { setupI18n } from 'vite-plugin-i18n-detector/client' // If you use i18next
import App from './App'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)

// The following is an example
const fallbackLng = 'en'
const lookupTarget = 'lang'

i18next
.use(initReactI18next)
.init({
fallbackLng,
resources: {},
})

const { loadResource, onLanguageChanged } = setupI18n({
language: i18next.language,
onInit(langs) {
if (!langs.includes(i18next.language)) {
i18next.changeLanguage(fallbackLng)
}
},
onLocaleChange: () => {
root.render(
<App />
)
},
fallbackLng,
setQuery: {
lookupTarget,
}
})

const _changeLanguage = i18next.changeLanguage
i18next.changeLanguage = async (lang: string | undefined, ...args) => {
let currentLng = i18next.language
// If language did't change, return
if (currentLng === lang) return undefined as any
currentLng = lang || currentLng
await loadResource(lang)
return _changeLanguage(lang, ...args)
}

i18next.on('languageChanged', (lang) => {
onLanguageChanged(lang)
})
```

### App.tsx

```tsx
import { useTranslation } from 'react-i18next'

function App() {
const { t, i18n } = useTranslation()

return <div onClick={() => i18n.changeLanguage('zh')}>{t('namespace.key')}</div>
}
```


### .vscode => settings.json
``` json
{
"i18n-ally.localesPaths": ["src/locale"],
"i18n-ally.keystyle": "flat",
"i18n-ally.enabledParsers": ["json", "json5"],
"i18n-ally.enabledFrameworks": ["react", "i18next"],
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.{ext}",
"i18n-ally.sourceLanguage": "en"
}
```


## ⚠️ Warning

Currently, we only support `.json(5)` file
109 changes: 14 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,107 +1,26 @@
# vite-plugin-i18n-detector

> Lazyloadable i18n locales detector
> i18n国际化资源懒加载 vite 插件
## Features
## 特性

- Unawared DX
- **Lazyload** locale resource
- Options like 'i18n-ally'
- 无感知开发体验
- **懒加载**当前语言资源文件
- `i18n-ally` 的配置项

## Install
## 安装

```bash
pnpm add vite-plugin-i18n-detector
pnpm add vite-plugin-i18n-detector -D
```

## Example
## 在线示例
[Demo](https://hemengke1997.github.io/vite-plugin-i18n-detector/)

### vite.config.ts
```ts
import path from 'path'
import { defineConfig } from 'vite'
import { i18nDetector } from 'vite-plugin-i18n-detector'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
i18nDetector({
localesPaths: [path.join(__dirname, './src/locale')],
pathMatcher: '{locale}/{namespaces}.{ext}',
enabledParsers: ['json', 'json5'],
}),
],
})

```

### i18next example

```tsx
import ReactDOM from 'react-dom/client'
import i18next from 'i18next'
import { initReactI18next } from 'react-i18next'
import { setupI18n } from 'vite-plugin-i18n-detector/client' // If you use i18next
import App from './App'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)

// The following is an example
const fallbackLng = 'en'
const lookupTarget = 'lang'

i18next
.use(initReactI18next)
.init({
fallbackLng,
resources: {},
})

const { loadResource, onLanguageChanged } = setupI18n({
language: i18next.language,
onInit(langs) {
if (!langs.includes(i18next.language)) {
i18next.changeLanguage(fallbackLng)
}
},
onLocaleChange: () => {
root.render(
<App />
)
},
fallbackLng,
setQuery: {
lookupTarget,
}
})

const _changeLanguage = i18next.changeLanguage
i18next.changeLanguage = async (lang: string | undefined, ...args) => {
let currentLng = i18next.language
// If language did't change, return
if (currentLng === lang) return undefined as any
currentLng = lang || currentLng
await loadResource(lang)
return _changeLanguage(lang, ...args)
}

i18next.on('languageChanged', (lang) => {
onLanguageChanged(lang)
})
```

### App.tsx

```tsx
import { useTranslation } from 'react-i18next'

function App() {
const { t, i18n } = useTranslation()

return <div onClick={() => i18n.changeLanguage('zh')}>{t('namespace.key')}</div>
}
```
## 代码示例
[playground](./playground/spa/)

## vscode国际化配置

### .vscode => settings.json
``` json
Expand All @@ -117,6 +36,6 @@ function App() {
```


## ⚠️ Warning
## ⚠️ 提示

Currently, we only support `.json(5)` file
目前仅支持 `json(5)` 资源文件
2 changes: 1 addition & 1 deletion __test__/csr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('crs spa', () => {

test('should have title', async () => {
const title = await page.title()
expect(title).toBe('SPA')
expect(title).toBe('vite-plugin-i18n-detector-demo')
})

test('should have lang', async () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.10",
"packageManager": "pnpm@7.30.0",
"description": "vite plugin for auto lazy-detect i18n resources",
"description": "vite plugin for lazyload i18n resources. 懒加载国际化资源的 vite 插件",
"author": "hemengke <https://github.com/hemengke1997>",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -48,6 +48,7 @@
"scripts": {
"dev": "tsup --watch",
"build": "tsup",
"build:pages": "pnpm run build && cd playground/spa && pnpm run build",
"test": "vitest",
"preinstall": "npx only-allow pnpm",
"lint": "eslint .",
Expand Down

0 comments on commit d8cf0d8

Please sign in to comment.