Skip to content

Commit

Permalink
feat: support flat keyStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 19, 2024
1 parent 567d6da commit c402676
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 26 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-i18n-ally",
"version": "5.0.1",
"description": "vite plugin load i18n resources lazily. 懒加载国际化资源的 vite 插件",
"description": "vite plugin load i18n resources lazily. 懒加载国际化资源,适配vscode-i18n-ally",
"type": "module",
"keywords": [
"vite plugin",
Expand Down Expand Up @@ -55,16 +55,13 @@
"vite": ">=5.0.0"
},
"dependencies": {
"clone-deep": "^4.0.1",
"debug": "^4.3.7",
"fast-glob": "^3.3.2",
"find-up": "^7.0.0",
"importx": "^0.4.4",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"language-tags": "^1.0.9",
"string.prototype.trimend": "^1.0.8",
"uniq": "^1.0.1"
"language-tags": "^1.0.9"
},
"devDependencies": {
"@commitlint/cli": "^19.5.0",
Expand All @@ -76,14 +73,17 @@
"@types/debug": "^4.1.12",
"@types/js-yaml": "^4.0.9",
"@types/language-tags": "^1.0.4",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.5.5",
"@types/react": "^18.3.7",
"bumpp": "^9.5.2",
"clone-deep": "^4.0.1",
"conventional-changelog-cli": "^5.0.0",
"cross-env": "^7.0.3",
"eslint": "^9.10.0",
"i18next": "23.5.1",
"jsdom": "^22.1.0",
"lodash-es": "^4.17.21",
"npm-run-all": "^4.1.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
9 changes: 0 additions & 9 deletions playground/remix-ssr/app/components/not-found/index.tsx

This file was deleted.

10 changes: 8 additions & 2 deletions playground/remix-ssr/app/routes/$lang+/$.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import NotFound from '@/components/not-found'
import { useEffect } from 'react'

export default NotFound
export default function NotFound() {
useEffect(() => {
console.log('not found')
}, [])

return null
}
10 changes: 9 additions & 1 deletion playground/remix-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import { remixFlatRoutes } from 'vite-plugin-remix-flat-routes'
installGlobals()

export default defineConfig((env) => {
const ignoredRouteFiles = ['**/components/**', '**/hooks/**', '**/images/**', '**/utils/**', '**/*.css']
const ignoredRouteFiles = [
'**/components/**',
'**/hooks/**',
'**/images/**',
'**/utils/**',
'**/*.css',
'**/meta.ts',
'**/types.ts',
]
return preset(
{
env,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/node/locale-detector/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import cloneDeep from 'clone-deep'
import fg from 'fast-glob'
import tags from 'language-tags'
import { trimEnd, uniq } from 'lodash-es'
import path from 'node:path'
import trimEnd from 'string.prototype.trimend'
import uniq from 'uniq'
import { normalizePath } from 'vite'
import { DefaultEnabledParsers } from '../parsers'
import { Parser } from '../parsers/Parser'
import { ParsePathMatcher } from '../path-matcher'
import { type I18nAllyOptions } from '../types'
import { PKGNAME, VIRTUAL } from '../utils/constant'
import { I18nAlly, VIRTUAL } from '../utils/constant'
import { debug } from '../utils/debugger'
import { unflatten } from '../utils/flat'
import { logger } from '../utils/logger'

export type Config = Omit<Required<I18nAllyOptions>, 'useVscodeI18nAllyConfig'>
Expand Down Expand Up @@ -241,7 +241,7 @@ export class LocaleDetector {
}
}
if (!this.files.length) {
throw new Error(`[${PKGNAME}]: No locale files detected. Please check your config.`)
throw new Error(`[${I18nAlly}]: No locale files detected. Please check your config.`)
}
}

Expand Down Expand Up @@ -274,14 +274,17 @@ export class LocaleDetector {

const data = await parser.load(filepath)

// support `i18n-ally.keyStyle` `nesetd` and `flat`
const value = unflatten(data)

const deepDirpath = path.dirname(filepath)

this._files[filepath] = {
filepath,
deepDirpath,
dirpath,
locale,
value: data,
value,
namespace,
matcher,
}
Expand Down
3 changes: 2 additions & 1 deletion src/node/parsers/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class Parser {
try {
const res = await this.parse(raw, filepath)
return res
} catch {
} catch (e) {
console.error(e)
return {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/node/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { name as PKGNAME } from '../../../package.json'
export { name as I18nAlly } from '../../../package.json'

export const VIRTUAL = 'virtual:i18n-ally'

Expand Down
4 changes: 2 additions & 2 deletions src/node/utils/debugger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createDebug from 'debug'
import { PKGNAME } from './constant'
import { I18nAlly } from './constant'

export const debug = createDebug(PKGNAME)
export const debug = createDebug(I18nAlly)
42 changes: 42 additions & 0 deletions src/node/utils/flat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { get, isObject, set } from 'lodash-es'

export const ROOT_KEY = '__i18n_ally_root__'

export function flatten(data: any) {
const output: any = {}

function step(obj: any, prev?: string) {
Object.keys(obj).forEach((key) => {
const value = obj[key]
const isarray = Array.isArray(value)
const type = Object.prototype.toString.call(value)
const isobject = type === '[object Object]' || type === '[object Array]'

const newKey = key === ROOT_KEY ? prev || '' : prev ? `${prev}.${key}` : key

if (!isarray && isobject && Object.keys(value).length) return step(value, newKey)

output[newKey] = value
})
}

step(data)

return output
}

export function unflatten(data: any) {
const output: any = {}

Object.keys(data || {})
.sort((a, b) => b.length - a.length)
.forEach((key) => {
const original = key ? get(output, key) : output

if (isObject(original)) set(output, key ? `${key}.${ROOT_KEY}` : ROOT_KEY, data[key])
else if (original === undefined) set(output, key, data[key])
else throw new Error(`Duplicated key ${key} found`)
})

return output
}

0 comments on commit c402676

Please sign in to comment.