Skip to content

Commit

Permalink
fix: typo
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Jan 18, 2024
1 parent 7f015d9 commit 5fcfb3f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
name: Create Release
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
# steps:
# - uses: actions/checkout@v3
# with:
# fetch-depth: 0

- uses: actions/setup-node@v3
with:
node-version: 16.x
# - uses: actions/setup-node@v3
# with:
# node-version: 16.x

- run: npx changelogithub
env:
GITHUB_TOKEN: ${{ secrets.ghb_token }}
# - run: npx changelogithub
# env:
# GITHUB_TOKEN: ${{ secrets.ghb_token }}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node_version: [16, 18, 20]
node_version: [18]
include:
# Active LTS + other OS
- os: macos-latest
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "vite-plugin-i18n-detector",
"version": "2.2.0",
"packageManager": "pnpm@8.12.1",
"description": "vite plugin for lazyload i18n resources. 懒加载国际化资源的 vite 插件",
"type": "module",
"keywords": [
Expand Down Expand Up @@ -50,15 +49,15 @@
"scripts": {
"dev": "simple-git-hooks && tsup --watch",
"build": "rm -rf dist && tsup",
"build:pages": "pnpm run build && cd playground/spa && pnpm run build",
"build:pages": "npm run build && cd playground/spa && npm run build",
"test": "run-s test:unit test:serve test:build",
"test:unit": "cross-env DEBUG=vite-plugin-i18n* vitest run",
"test:serve": "cross-env DEBUG=vite-plugin-i18n* vitest run -c vitest.config.e2e.ts",
"test:build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
"lint": "eslint .",
"up": "taze -I -w -r",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"bump": "bumpp package.json -c -p -t --all -x \"pnpm run changelog\""
"bump": "bumpp package.json -c -p -t --all -x \"npm run changelog\""
},
"peerDependencies": {
"vite": ">=4.0.0"
Expand Down Expand Up @@ -108,6 +107,6 @@
"node": ">=16.0.0"
},
"simple-git-hooks": {
"commit-msg": "pnpm exec commitlint -e"
"commit-msg": "npx commitlint -e"
}
}
2 changes: 1 addition & 1 deletion playground/spa/__tests__/spa.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('e2e', () => {
})

test('should fallback to fallbackLng when language is not found', async () => {
await untilBrowserLogAfter(() => page.goto(`${viteTestUrl}/?lang=not-exist`), /.*Current language is 'not-exist'*/)
await untilBrowserLogAfter(() => page.goto(`${viteTestUrl}/?lang=not-exist`), /.*Current language 'not-exist' not found*/)
})
})

Expand Down
2 changes: 1 addition & 1 deletion playground/vscode-setting/__tests__/vscode-setting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('e2e', () => {
})

test('should fallback to fallbackLng when language is not found', async () => {
await untilBrowserLogAfter(() => page.goto(`${viteTestUrl}/?lang=not-exist`), /.*Current language is 'not-exist'*/)
await untilBrowserLogAfter(() => page.goto(`${viteTestUrl}/?lang=not-exist`), /.*Current language 'not-exist' not found*/)
})
})

Expand Down
36 changes: 19 additions & 17 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import resources from 'virtual:i18n-helper'
import { name as PKGNAME } from '../../package.json'

export interface I18nSetupOptions {
/**
* Current language
*/
language: string
/**
* If no resource of current language, fallback to `fallbackLng`
*/
fallbackLng: string
/**
* triggered when resource is loaded first time
* Triggered when resource is loaded first time
*/
onResourceLoaded: (langHelper: Record<string, string>, currentLang: string) => Promise<void> | void
/**
* triggered when i18n is inited
* Triggered when i18n is inited
*/
onInited: (langs: String[], currentLang: string) => Promise<void> | void
onInited: (langs: string[], currentLang: string) => Promise<void> | void
/**
* cache user language on
* Cache user language on
*/
cache?: {
/**
Expand Down Expand Up @@ -46,20 +52,21 @@ function setupI18n(options: I18nSetupOptions) {
const { setCache = true } = options || {}

if (!lang) {
console.warn(`[${PKGNAME}]: Language is undefined, fallback to '${fallbackLng}'`)
console.warn(`[${PKGNAME}]: 'language' undefined, fallback to '${fallbackLng}'`)
lang = fallbackLng
}

if (!(lang in resources)) {
console.warn(
`[${PKGNAME}]: Current language is '${lang}', but it is not defined in locales, fallback to '${fallbackLng}'`,
`[${PKGNAME}]: Current language '${lang}' not found in locale resources, fallback to '${fallbackLng}'`,
)
lang = fallbackLng
}

const lazyload: (() => Promise<{ default: Record<string, string> | undefined }>) | undefined = resources[lang]

if (!lazyload) {
console.error(`[${PKGNAME}]: No locales resources found, please ensure 'localesPaths' and locale files exist`)
console.error(`[${PKGNAME}]: No locale resources found. Please check config`)
return
}

Expand Down Expand Up @@ -96,16 +103,7 @@ function setupI18n(options: I18nSetupOptions) {
}
}

/**
* NOTE:
* If you need to specify the language setting for headers, such as the `fetch` API, set it here.
* The following is an example for axios.
*
* axios.defaults.headers.common['Accept-Language'] = lang
*/

setHtmlTag(lang)

setQuerystring(lang)
}

Expand All @@ -118,7 +116,11 @@ function setupI18n(options: I18nSetupOptions) {
}

_init().then(async () => {
await onInited?.(langs, lng)
try {
await onInited?.(langs, lng)
} catch (e) {
console.error(`[${PKGNAME}]: onInited error`, e)
}
})

return {
Expand Down

0 comments on commit 5fcfb3f

Please sign in to comment.