Skip to content

Commit

Permalink
feat: suppot v0.5.0+ locale path (#19)
Browse files Browse the repository at this point in the history
* chore: update locale path

* chore: fix typo

* chore: fix in vite

* refactor: optimized logic

* test: update test
  • Loading branch information
hexf00 authored Nov 12, 2024
1 parent cb88b3d commit ef5d04f
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 204 deletions.
27 changes: 14 additions & 13 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
"build": "tsc && vite build"
},
"dependencies": {
"@univerjs/core": "0.4.0-alpha.1",
"@univerjs/design": "0.4.0-alpha.1",
"@univerjs/docs": "0.4.0-alpha.1",
"@univerjs/docs-ui": "0.4.0-alpha.1",
"@univerjs/engine-formula": "0.4.0-alpha.1",
"@univerjs/engine-numfmt": "0.4.0-alpha.1",
"@univerjs/engine-render": "0.4.0-alpha.1",
"@univerjs/sheets": "0.4.0-alpha.1",
"@univerjs/sheets-formula": "0.4.0-alpha.1",
"@univerjs/sheets-numfmt": "0.4.0-alpha.1",
"@univerjs/sheets-ui": "0.4.0-alpha.1",
"@univerjs/sheets-zen-editor": "0.4.0-alpha.1",
"@univerjs/ui": "0.4.0-alpha.1"
"@univerjs/core": "0.5.0-alpha.0",
"@univerjs/design": "0.5.0-alpha.0",
"@univerjs/docs": "0.5.0-alpha.0",
"@univerjs/docs-ui": "0.5.0-alpha.0",
"@univerjs/engine-formula": "0.5.0-alpha.0",
"@univerjs/engine-numfmt": "0.5.0-alpha.0",
"@univerjs/engine-render": "0.5.0-alpha.0",
"@univerjs/sheets": "0.5.0-alpha.0",
"@univerjs/sheets-formula": "0.5.0-alpha.0",
"@univerjs/sheets-formula-ui": "0.5.0-alpha.0",
"@univerjs/sheets-numfmt": "0.5.0-alpha.0",
"@univerjs/sheets-ui": "0.5.0-alpha.0",
"@univerjs/sheets-zen-editor": "0.5.0-alpha.0",
"@univerjs/ui": "0.5.0-alpha.0"
},
"devDependencies": {
"@types/react": "^18.3.11",
Expand Down
11 changes: 7 additions & 4 deletions examples/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'
import { UniverRenderEnginePlugin } from '@univerjs/engine-render'
import { UniverSheetsPlugin } from '@univerjs/sheets'
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula'
import { UniverSheetsFormulaUIPlugin } from '@univerjs/sheets-formula-ui'
import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt'
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui'
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor'
Expand All @@ -25,20 +26,22 @@ const univer = new Univer({
},
})

univer.registerPlugin(UniverDocsPlugin, {
hasScroll: false,
})
univer.registerPlugin(UniverDocsUIPlugin)
univer.registerPlugin(UniverRenderEnginePlugin)
univer.registerPlugin(UniverUIPlugin, {
container: 'app',
})
univer.registerPlugin(UniverDocsPlugin, {
hasScroll: false,
})
univer.registerPlugin(UniverDocsUIPlugin)

univer.registerPlugin(UniverSheetsPlugin)
univer.registerPlugin(UniverSheetsUIPlugin)

univer.registerPlugin(UniverSheetsNumfmtPlugin)
univer.registerPlugin(UniverFormulaEnginePlugin)
univer.registerPlugin(UniverSheetsFormulaPlugin)
univer.registerPlugin(UniverSheetsFormulaUIPlugin)
univer.registerPlugin(UniverSheetsZenEditorPlugin)

univer.createUnit(UniverInstanceType.UNIVER_SHEET, {})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ describe('export-virtual-locales-module', () => {
'node_modules/@univerjs/ui/lib/types/locale/en-US.d.ts': '/* en-US locale */',
'node_modules/@univerjs/ui/lib/types/locale/zh-CN.d.ts': '/* zh-CN locale */',
'node_modules/@univerjs/ui/lib/types/locale/ru-RU.d.ts': '/* ru-RU locale */',
'node_modules/@univerjs/design/lib/types/locale/vi-VN.d.ts': '/* vi-VN locale */',

'node_modules/@univerjs/ui/lib/es/locale/en-US.js': '/* en-US locale */',
'node_modules/@univerjs/ui/lib/es/locale/zh-CN.js': '/* zh-CN locale */',
'node_modules/@univerjs/ui/lib/es/locale/ru-RU.js': '/* ru-RU locale */',
'node_modules/@univerjs/ui/lib/locale/ru-RU.json': '/* ru-RU locale */',
'node_modules/@univerjs/design/lib/locale/vi-VN.json': '/* vi-VN locale */',

'node_modules/@univerjs/design/lib/index.css': '/* univerjs design css */',
})
})
Expand All @@ -20,9 +28,10 @@ describe('export-virtual-locales-module', () => {
const outputCode = exportVirtualLocalesModule()

expect(outputCode).toMatch(/import { Tools as _Tools } from '@univerjs\/core';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}enUS from '@univerjs\/ui\/lib\/locale\/en-US';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}ruRU from '@univerjs\/ui\/lib\/locale\/ru-RU';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}zhCN from '@univerjs\/ui\/lib\/locale\/zh-CN';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}enUS from '@univerjs\/ui\/lib\/es\/locale\/en-US.js';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}ruRU from '@univerjs\/ui\/lib\/es\/locale\/ru-RU.js';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}zhCN from '@univerjs\/ui\/lib\/es\/locale\/zh-CN.js';/)
expect(outputCode).toMatch(/import [a-zA-Z]{8}viVN from '@univerjs\/design\/lib\/locale\/vi-VN.json';/)

expect(outputCode).toMatch(/export const enUS = _Tools.deepMerge\([a-zA-Z]{8}enUS,\n\);/)
expect(outputCode).toMatch(/export const ruRU = _Tools.deepMerge\([a-zA-Z]{8}ruRU,\n\);/)
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/export-virtual-locales-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ export function exportVirtualLocalesModule() {

if (fs.existsSync(langPath)) {
const langVar = `${lang.replace('-', '')}`

const key = `${generateRandomString(8)}${langVar}`
importStatement += `import ${key} from '${packageName}/lib/locale/${lang}';\n`
languages[lang].add(key)
const langFiles = [
// if version is greater than 0.5.0, use lib/es/locale
`${packageName}/lib/es/locale/${lang}.js`,
`${packageName}/lib/locale/${lang}.json`,
]
const langFile = langFiles.find(file => fs.existsSync(path.resolve('node_modules', file)))
if (langFile) {
importStatement += `import ${key} from '${langFile}';\n`
languages[lang].add(key)
}
}
})
}
Expand Down
Loading

0 comments on commit ef5d04f

Please sign in to comment.