Glob all i18n files

This commit is contained in:
Jesse Hines 2026-06-30 19:50:20 -04:00
parent d105532b6a
commit 55d1f2d08a
No known key found for this signature in database
3 changed files with 34 additions and 6 deletions

View file

@ -1,6 +1,8 @@
import esbuild from "esbuild";
import process from "process";
import { builtinModules } from "node:module";
import path from "path";
import fs from "fs";
const banner =
`/*
@ -17,6 +19,32 @@ const context = await esbuild.context({
},
entryPoints: ["src/main.ts"],
bundle: true,
plugins: [{
name: "import-glob",
setup: (build) => {
build.onResolve({ filter: /^glob:.*$/ }, async (args) => {
return {
path: args.path.replace(/^glob:/, ''),
namespace: 'import-glob',
pluginData: { resolveDir: args.resolveDir },
};
});
build.onLoad({ filter: /.*/, namespace: 'import-glob' }, async (args) => {
const files = fs.globSync(args.path, {cwd: args.pluginData.resolveDir})
.map((f) => f.split(path.sep).join('/'))
.sort();
return {
contents: [
...files.map((module, i) => `import m${i} from './${module}'`),
"export default {",
...files.map((f, i) => `${JSON.stringify(path.basename(f, path.extname(f)))}: m${i},`),
"}",
].join("\n"),
resolveDir: args.pluginData.resolveDir,
};
});
},
}],
external: [
"obsidian",
"electron",

5
src/glob.d.ts vendored Normal file
View file

@ -0,0 +1,5 @@
declare module 'glob:*' {
const modules: Record<string, any>;
export default modules;
}

View file

@ -1,11 +1,6 @@
import i18next from 'i18next';
import { getLanguage } from 'obsidian';
// esbuild doesn't have an easy way to include all files in a dir, so we'll list them manually
import en from './locales/en.json';
const locales = {
en,
}
import locales from 'glob:./locales/*.json';
export async function initializeI18n() {
return await i18next.init({