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
304cc5f
commit d8cf0d8
Showing
6 changed files
with
183 additions
and
97 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
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 # 指定构建之后要推送哪个目录的代码 |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
playground-dist |
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,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 |
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