snapshot of https://github.com/dream-num/univer.git (dev, 2026-09-16; blobs > 131072B omitted)
Some checks failed
📦 Build / build (push) Has been cancelled
🧪 CI Test / test shard (1/4) (push) Has been cancelled
🧪 CI Test / test shard (2/4) (push) Has been cancelled
🧪 CI Test / test shard (3/4) (push) Has been cancelled
🧪 CI Test / test shard (4/4) (push) Has been cancelled
🧪 CI Test / test (push) Has been cancelled
🧪 CI Test / eslint (push) Has been cancelled
🧪 CI Test / typecheck (push) Has been cancelled
🔫 Sync to Pro / dispatch-sync (push) Has been cancelled
🔮 Sync Mirror / sync (push) Has been cancelled
Some checks failed
📦 Build / build (push) Has been cancelled
🧪 CI Test / test shard (1/4) (push) Has been cancelled
🧪 CI Test / test shard (2/4) (push) Has been cancelled
🧪 CI Test / test shard (3/4) (push) Has been cancelled
🧪 CI Test / test shard (4/4) (push) Has been cancelled
🧪 CI Test / test (push) Has been cancelled
🧪 CI Test / eslint (push) Has been cancelled
🧪 CI Test / typecheck (push) Has been cancelled
🔫 Sync to Pro / dispatch-sync (push) Has been cancelled
🔮 Sync Mirror / sync (push) Has been cancelled
This commit is contained in:
61
common/shared/locale/index.ts
Normal file
61
common/shared/locale/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { existsSync, readdirSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const LOCALE_FILE_NAME_PATTERN = /^[a-z]{2}-[A-Z]{2}\.ts$/;
|
||||
|
||||
export interface IDiscoverUniverUiLocalesOptions {
|
||||
rootDir?: string;
|
||||
uiPackageDir?: string;
|
||||
}
|
||||
|
||||
function getCandidateUiPackageDirs(rootDir: string): string[] {
|
||||
const dirs: string[] = [];
|
||||
let current = path.resolve(rootDir);
|
||||
|
||||
while (true) {
|
||||
dirs.push(
|
||||
path.join(current, 'node_modules/@univerjs/ui'),
|
||||
path.join(current, 'packages/ui'),
|
||||
path.join(current, 'submodules/univer/packages/ui')
|
||||
);
|
||||
|
||||
const parent = path.dirname(current);
|
||||
if (parent === current) {
|
||||
break;
|
||||
}
|
||||
|
||||
current = parent;
|
||||
}
|
||||
|
||||
return dirs;
|
||||
}
|
||||
|
||||
function resolveUniverUiPackageDir(options: IDiscoverUniverUiLocalesOptions): string {
|
||||
if (options.uiPackageDir) {
|
||||
return path.resolve(options.uiPackageDir);
|
||||
}
|
||||
|
||||
const rootDir = options.rootDir ?? process.cwd();
|
||||
const uiPackageDir = getCandidateUiPackageDirs(rootDir).find((candidate) => existsSync(path.join(candidate, 'src/locale')));
|
||||
|
||||
if (!uiPackageDir) {
|
||||
throw new Error(`Cannot find @univerjs/ui src/locale from ${path.resolve(rootDir)}`);
|
||||
}
|
||||
|
||||
return uiPackageDir;
|
||||
}
|
||||
|
||||
export function discoverUniverUiLocales(options: IDiscoverUniverUiLocalesOptions = {}): string[] {
|
||||
const uiPackageDir = resolveUniverUiPackageDir(options);
|
||||
const localeDir = path.join(uiPackageDir, 'src/locale');
|
||||
|
||||
if (!existsSync(localeDir)) {
|
||||
throw new Error(`Cannot find @univerjs/ui locale directory: ${localeDir}`);
|
||||
}
|
||||
|
||||
return readdirSync(localeDir, { withFileTypes: true })
|
||||
.filter((entry) => entry.isFile() && LOCALE_FILE_NAME_PATTERN.test(entry.name))
|
||||
.map((entry) => path.basename(entry.name, '.ts'))
|
||||
.sort((left, right) => left.localeCompare(right));
|
||||
}
|
||||
Reference in New Issue
Block a user