The minimalistic document archiving platform.
feat(i18n): auto generate i18n type in dev mode (#254)
Corentin Thomasset6287aaa9735f
diff --git a/.github/workflows/ci-apps-papra-client.yaml b/.github/workflows/ci-apps-papra-client.yamlindex 98f737d7..36f7ae75 100644--- a/.github/workflows/ci-apps-papra-client.yaml+++ b/.github/workflows/ci-apps-papra-client.yaml@@ -38,11 +38,12 @@ jobs:- name: Run unit testrun: pnpm test- - name: Build the app- run: pnpm build-- # Ensure locales types are up to date+ # Ensure locales types are up to date, must be run before building the app- name: Check locales typesrun: |pnpm script:generate-i18n-types- git diff --exit-code -- src/modules/i18n/locales.types.ts > /dev/null || (echo "Locales types are outdated, please run 'pnpm script:generate-i18n-types' and commit the changes." && exit 1)\ No newline at end of file+ git diff --exit-code -- src/modules/i18n/locales.types.ts > /dev/null || (echo "Locales types are outdated, please run 'pnpm script:generate-i18n-types' and commit the changes." && exit 1)++ - name: Build the app+ run: pnpm build+diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 96471330..07ab4eac 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -60,7 +60,7 @@ pnpm script:generate-i18n-types```- This command will update the file [`locales.types.ts`](./apps/papra-client/src/modules/i18n/locale.types.ts) with the new/removed keys.-- Will developing you can use `pnpm script:generate-i18n-types:watch` to automatically update the types when you touch the [`en.yml`](./apps/papra-client/src/locales/en.yml) file.+- When developing in papra-client (using `pnpm dev`), the i18n types definition will automatically update when you touch the [`en.yml`](./apps/papra-client/src/locales/en.yml) file, so no need to run the command above.## Development Setupdiff --git a/apps/papra-client/package.json b/apps/papra-client/package.jsonindex 2eaafe5f..38b6124c 100644--- a/apps/papra-client/package.json+++ b/apps/papra-client/package.json@@ -25,8 +25,7 @@"test:e2e": "playwright test","typecheck": "tsc --noEmit","script:get-missing-i18n-keys": "tsx src/scripts/get-missing-i18n-keys.script.ts",- "script:generate-i18n-types": "tsx src/scripts/generate-i18n-types.script.ts",- "script:generate-i18n-types:watch": "tsx watch --include src/locales/en.yml src/scripts/generate-i18n-types.script.ts"+ "script:generate-i18n-types": "tsx src/scripts/generate-i18n-types.script.ts"},"dependencies": {"@corentinth/chisels": "^1.0.2",diff --git a/apps/papra-client/src/modules/i18n/locales.types.ts b/apps/papra-client/src/modules/i18n/locales.types.tsindex 602b7409..2d41c8a7 100644--- a/apps/papra-client/src/modules/i18n/locales.types.ts+++ b/apps/papra-client/src/modules/i18n/locales.types.ts@@ -1,2 +1,200 @@-// Dynamically generated file. Use "pnpm script:generate-i18n-types" to update.-export type LocaleKeys = 'auth.request-password-reset.title' | 'auth.request-password-reset.description' | 'auth.request-password-reset.requested' | 'auth.request-password-reset.back-to-login' | 'auth.request-password-reset.form.email.label' | 'auth.request-password-reset.form.email.placeholder' | 'auth.request-password-reset.form.email.required' | 'auth.request-password-reset.form.email.invalid' | 'auth.request-password-reset.form.submit' | 'auth.reset-password.title' | 'auth.reset-password.description' | 'auth.reset-password.reset' | 'auth.reset-password.back-to-login' | 'auth.reset-password.form.new-password.label' | 'auth.reset-password.form.new-password.placeholder' | 'auth.reset-password.form.new-password.required' | 'auth.reset-password.form.new-password.min-length' | 'auth.reset-password.form.new-password.max-length' | 'auth.reset-password.form.submit' | 'auth.email-provider.open' | 'auth.login.title' | 'auth.login.description' | 'auth.login.login-with-provider' | 'auth.login.no-account' | 'auth.login.register' | 'auth.login.form.email.label' | 'auth.login.form.email.placeholder' | 'auth.login.form.email.required' | 'auth.login.form.email.invalid' | 'auth.login.form.password.label' | 'auth.login.form.password.placeholder' | 'auth.login.form.password.required' | 'auth.login.form.remember-me.label' | 'auth.login.form.forgot-password.label' | 'auth.login.form.submit' | 'auth.register.title' | 'auth.register.description' | 'auth.register.register-with-email' | 'auth.register.register-with-provider' | 'auth.register.providers.google' | 'auth.register.providers.github' | 'auth.register.have-account' | 'auth.register.login' | 'auth.register.registration-disabled.title' | 'auth.register.registration-disabled.description' | 'auth.register.form.email.label' | 'auth.register.form.email.placeholder' | 'auth.register.form.email.required' | 'auth.register.form.email.invalid' | 'auth.register.form.password.label' | 'auth.register.form.password.placeholder' | 'auth.register.form.password.required' | 'auth.register.form.password.min-length' | 'auth.register.form.password.max-length' | 'auth.register.form.name.label' | 'auth.register.form.name.placeholder' | 'auth.register.form.name.required' | 'auth.register.form.name.max-length' | 'auth.register.form.submit' | 'auth.email-validation-required.title' | 'auth.email-validation-required.description' | 'auth.legal-links.description' | 'auth.legal-links.terms' | 'auth.legal-links.privacy' | 'tags.no-tags.title' | 'tags.no-tags.description' | 'tags.no-tags.create-tag' | 'layout.menu.home' | 'layout.menu.documents' | 'layout.menu.tags' | 'layout.menu.tagging-rules' | 'layout.menu.integrations' | 'layout.menu.deleted-documents' | 'layout.menu.organization-settings' | 'layout.menu.api-keys' | 'layout.menu.settings' | 'layout.menu.account' | 'tagging-rules.field.name' | 'tagging-rules.field.content' | 'tagging-rules.operator.equals' | 'tagging-rules.operator.not-equals' | 'tagging-rules.operator.contains' | 'tagging-rules.operator.not-contains' | 'tagging-rules.operator.starts-with' | 'tagging-rules.operator.ends-with' | 'tagging-rules.list.title' | 'tagging-rules.list.description' | 'tagging-rules.list.demo-warning' | 'tagging-rules.list.no-tagging-rules.title' | 'tagging-rules.list.no-tagging-rules.description' | 'tagging-rules.list.no-tagging-rules.create-tagging-rule' | 'tagging-rules.list.card.no-conditions' | 'tagging-rules.list.card.one-condition' | 'tagging-rules.list.card.conditions' | 'tagging-rules.list.card.delete' | 'tagging-rules.list.card.edit' | 'tagging-rules.create.title' | 'tagging-rules.create.success' | 'tagging-rules.create.error' | 'tagging-rules.create.submit' | 'tagging-rules.form.name.label' | 'tagging-rules.form.name.placeholder' | 'tagging-rules.form.name.min-length' | 'tagging-rules.form.name.max-length' | 'tagging-rules.form.description.label' | 'tagging-rules.form.description.placeholder' | 'tagging-rules.form.description.max-length' | 'tagging-rules.form.conditions.label' | 'tagging-rules.form.conditions.description' | 'tagging-rules.form.conditions.add-condition' | 'tagging-rules.form.conditions.no-conditions.title' | 'tagging-rules.form.conditions.no-conditions.description' | 'tagging-rules.form.conditions.no-conditions.confirm' | 'tagging-rules.form.conditions.no-conditions.cancel' | 'tagging-rules.form.conditions.field.label' | 'tagging-rules.form.conditions.operator.label' | 'tagging-rules.form.conditions.value.label' | 'tagging-rules.form.conditions.value.placeholder' | 'tagging-rules.form.conditions.value.min-length' | 'tagging-rules.form.tags.label' | 'tagging-rules.form.tags.description' | 'tagging-rules.form.tags.min-length' | 'tagging-rules.form.tags.add-tag' | 'tagging-rules.form.submit' | 'tagging-rules.update.title' | 'tagging-rules.update.success' | 'tagging-rules.update.error' | 'tagging-rules.update.submit' | 'tagging-rules.update.cancel' | 'demo.popup.description' | 'demo.popup.discord' | 'demo.popup.discord-link-label' | 'demo.popup.reset' | 'demo.popup.hide' | 'trash.delete-all.button' | 'trash.delete-all.confirm.title' | 'trash.delete-all.confirm.description' | 'trash.delete-all.confirm.label' | 'trash.delete-all.confirm.cancel' | 'trash.delete.button' | 'trash.delete.confirm.title' | 'trash.delete.confirm.description' | 'trash.delete.confirm.label' | 'trash.delete.confirm.cancel' | 'trash.deleted.success.title' | 'trash.deleted.success.description' | 'import-documents.title.error' | 'import-documents.title.success' | 'import-documents.title.pending' | 'import-documents.title.none' | 'import-documents.no-import-in-progress' | 'api-errors.document.already_exists' | 'api-errors.document.file_too_big' | 'api-errors.intake_email.limit_reached' | 'api-errors.user.max_organization_count_reached' | 'api-errors.default' | 'api-keys.permissions.documents.title' | 'api-keys.permissions.documents.documents:create' | 'api-keys.permissions.documents.documents:read' | 'api-keys.permissions.documents.documents:update' | 'api-keys.permissions.documents.documents:delete' | 'api-keys.permissions.tags.title' | 'api-keys.permissions.tags.tags:create' | 'api-keys.permissions.tags.tags:read' | 'api-keys.permissions.tags.tags:update' | 'api-keys.permissions.tags.tags:delete' | 'api-keys.create.title' | 'api-keys.create.description' | 'api-keys.create.success' | 'api-keys.create.back' | 'api-keys.create.form.name.label' | 'api-keys.create.form.name.placeholder' | 'api-keys.create.form.name.required' | 'api-keys.create.form.permissions.label' | 'api-keys.create.form.permissions.description' | 'api-keys.create.form.permissions.required' | 'api-keys.create.form.submit' | 'api-keys.create.created.title' | 'api-keys.create.created.description' | 'api-keys.list.title' | 'api-keys.list.description' | 'api-keys.list.delete' | 'api-keys.list.create' | 'api-keys.list.empty.title' | 'api-keys.list.empty.description' | 'api-keys.list.card.last-used' | 'api-keys.list.card.never' | 'api-keys.list.card.created' | 'api-keys.list.card.delete' | 'api-keys.delete.success' | 'api-keys.delete.confirm.title' | 'api-keys.delete.confirm.message' | 'api-keys.delete.confirm.confirm-button' | 'api-keys.delete.confirm.cancel-button';+// Do not manually edit this file.+// This file is dynamically generated when the dev server runs (or using the `pnpm script:generate-i18n-types` command).+// Keys are extracted from the en.yml file.+// Source code : src/plugins/i18n-types/i18n-types.services.ts++export type LocaleKeys =+ | 'auth.request-password-reset.title'+ | 'auth.request-password-reset.description'+ | 'auth.request-password-reset.requested'+ | 'auth.request-password-reset.back-to-login'+ | 'auth.request-password-reset.form.email.label'+ | 'auth.request-password-reset.form.email.placeholder'+ | 'auth.request-password-reset.form.email.required'+ | 'auth.request-password-reset.form.email.invalid'+ | 'auth.request-password-reset.form.submit'+ | 'auth.reset-password.title'+ | 'auth.reset-password.description'+ | 'auth.reset-password.reset'+ | 'auth.reset-password.back-to-login'+ | 'auth.reset-password.form.new-password.label'+ | 'auth.reset-password.form.new-password.placeholder'+ | 'auth.reset-password.form.new-password.required'+ | 'auth.reset-password.form.new-password.min-length'+ | 'auth.reset-password.form.new-password.max-length'+ | 'auth.reset-password.form.submit'+ | 'auth.email-provider.open'+ | 'auth.login.title'+ | 'auth.login.description'+ | 'auth.login.login-with-provider'+ | 'auth.login.no-account'+ | 'auth.login.register'+ | 'auth.login.form.email.label'+ | 'auth.login.form.email.placeholder'+ | 'auth.login.form.email.required'+ | 'auth.login.form.email.invalid'+ | 'auth.login.form.password.label'+ | 'auth.login.form.password.placeholder'+ | 'auth.login.form.password.required'+ | 'auth.login.form.remember-me.label'+ | 'auth.login.form.forgot-password.label'+ | 'auth.login.form.submit'+ | 'auth.register.title'+ | 'auth.register.description'+ | 'auth.register.register-with-email'+ | 'auth.register.register-with-provider'+ | 'auth.register.providers.google'+ | 'auth.register.providers.github'+ | 'auth.register.have-account'+ | 'auth.register.login'+ | 'auth.register.registration-disabled.title'+ | 'auth.register.registration-disabled.description'+ | 'auth.register.form.email.label'+ | 'auth.register.form.email.placeholder'+ | 'auth.register.form.email.required'+ | 'auth.register.form.email.invalid'+ | 'auth.register.form.password.label'+ | 'auth.register.form.password.placeholder'+ | 'auth.register.form.password.required'+ | 'auth.register.form.password.min-length'+ | 'auth.register.form.password.max-length'+ | 'auth.register.form.name.label'+ | 'auth.register.form.name.placeholder'+ | 'auth.register.form.name.required'+ | 'auth.register.form.name.max-length'+ | 'auth.register.form.submit'+ | 'auth.email-validation-required.title'+ | 'auth.email-validation-required.description'+ | 'auth.legal-links.description'+ | 'auth.legal-links.terms'+ | 'auth.legal-links.privacy'+ | 'tags.no-tags.title'+ | 'tags.no-tags.description'+ | 'tags.no-tags.create-tag'+ | 'layout.menu.home'+ | 'layout.menu.documents'+ | 'layout.menu.tags'+ | 'layout.menu.tagging-rules'+ | 'layout.menu.integrations'+ | 'layout.menu.deleted-documents'+ | 'layout.menu.organization-settings'+ | 'layout.menu.api-keys'+ | 'layout.menu.settings'+ | 'layout.menu.account'+ | 'tagging-rules.field.name'+ | 'tagging-rules.field.content'+ | 'tagging-rules.operator.equals'+ | 'tagging-rules.operator.not-equals'+ | 'tagging-rules.operator.contains'+ | 'tagging-rules.operator.not-contains'+ | 'tagging-rules.operator.starts-with'+ | 'tagging-rules.operator.ends-with'+ | 'tagging-rules.list.title'+ | 'tagging-rules.list.description'+ | 'tagging-rules.list.demo-warning'+ | 'tagging-rules.list.no-tagging-rules.title'+ | 'tagging-rules.list.no-tagging-rules.description'+ | 'tagging-rules.list.no-tagging-rules.create-tagging-rule'+ | 'tagging-rules.list.card.no-conditions'+ | 'tagging-rules.list.card.one-condition'+ | 'tagging-rules.list.card.conditions'+ | 'tagging-rules.list.card.delete'+ | 'tagging-rules.list.card.edit'+ | 'tagging-rules.create.title'+ | 'tagging-rules.create.success'+ | 'tagging-rules.create.error'+ | 'tagging-rules.create.submit'+ | 'tagging-rules.form.name.label'+ | 'tagging-rules.form.name.placeholder'+ | 'tagging-rules.form.name.min-length'+ | 'tagging-rules.form.name.max-length'+ | 'tagging-rules.form.description.label'+ | 'tagging-rules.form.description.placeholder'+ | 'tagging-rules.form.description.max-length'+ | 'tagging-rules.form.conditions.label'+ | 'tagging-rules.form.conditions.description'+ | 'tagging-rules.form.conditions.add-condition'+ | 'tagging-rules.form.conditions.no-conditions.title'+ | 'tagging-rules.form.conditions.no-conditions.description'+ | 'tagging-rules.form.conditions.no-conditions.confirm'+ | 'tagging-rules.form.conditions.no-conditions.cancel'+ | 'tagging-rules.form.conditions.field.label'+ | 'tagging-rules.form.conditions.operator.label'+ | 'tagging-rules.form.conditions.value.label'+ | 'tagging-rules.form.conditions.value.placeholder'+ | 'tagging-rules.form.conditions.value.min-length'+ | 'tagging-rules.form.tags.label'+ | 'tagging-rules.form.tags.description'+ | 'tagging-rules.form.tags.min-length'+ | 'tagging-rules.form.tags.add-tag'+ | 'tagging-rules.form.submit'+ | 'tagging-rules.update.title'+ | 'tagging-rules.update.success'+ | 'tagging-rules.update.error'+ | 'tagging-rules.update.submit'+ | 'tagging-rules.update.cancel'+ | 'demo.popup.description'+ | 'demo.popup.discord'+ | 'demo.popup.discord-link-label'+ | 'demo.popup.reset'+ | 'demo.popup.hide'+ | 'trash.delete-all.button'+ | 'trash.delete-all.confirm.title'+ | 'trash.delete-all.confirm.description'+ | 'trash.delete-all.confirm.label'+ | 'trash.delete-all.confirm.cancel'+ | 'trash.delete.button'+ | 'trash.delete.confirm.title'+ | 'trash.delete.confirm.description'+ | 'trash.delete.confirm.label'+ | 'trash.delete.confirm.cancel'+ | 'trash.deleted.success.title'+ | 'trash.deleted.success.description'+ | 'import-documents.title.error'+ | 'import-documents.title.success'+ | 'import-documents.title.pending'+ | 'import-documents.title.none'+ | 'import-documents.no-import-in-progress'+ | 'api-errors.document.already_exists'+ | 'api-errors.document.file_too_big'+ | 'api-errors.intake_email.limit_reached'+ | 'api-errors.user.max_organization_count_reached'+ | 'api-errors.default'+ | 'api-keys.permissions.documents.title'+ | 'api-keys.permissions.documents.documents:create'+ | 'api-keys.permissions.documents.documents:read'+ | 'api-keys.permissions.documents.documents:update'+ | 'api-keys.permissions.documents.documents:delete'+ | 'api-keys.permissions.tags.title'+ | 'api-keys.permissions.tags.tags:create'+ | 'api-keys.permissions.tags.tags:read'+ | 'api-keys.permissions.tags.tags:update'+ | 'api-keys.permissions.tags.tags:delete'+ | 'api-keys.create.title'+ | 'api-keys.create.description'+ | 'api-keys.create.success'+ | 'api-keys.create.back'+ | 'api-keys.create.form.name.label'+ | 'api-keys.create.form.name.placeholder'+ | 'api-keys.create.form.name.required'+ | 'api-keys.create.form.permissions.label'+ | 'api-keys.create.form.permissions.description'+ | 'api-keys.create.form.permissions.required'+ | 'api-keys.create.form.submit'+ | 'api-keys.create.created.title'+ | 'api-keys.create.created.description'+ | 'api-keys.list.title'+ | 'api-keys.list.description'+ | 'api-keys.list.delete'+ | 'api-keys.list.create'+ | 'api-keys.list.empty.title'+ | 'api-keys.list.empty.description'+ | 'api-keys.list.card.last-used'+ | 'api-keys.list.card.never'+ | 'api-keys.list.card.created'+ | 'api-keys.list.card.delete'+ | 'api-keys.delete.success'+ | 'api-keys.delete.confirm.title'+ | 'api-keys.delete.confirm.message'+ | 'api-keys.delete.confirm.confirm-button'+ | 'api-keys.delete.confirm.cancel-button';diff --git a/apps/papra-client/src/plugins/i18n-types/i18n-types.plugin.ts b/apps/papra-client/src/plugins/i18n-types/i18n-types.plugin.tsnew file mode 100644index 00000000..43680810--- /dev/null+++ b/apps/papra-client/src/plugins/i18n-types/i18n-types.plugin.ts@@ -0,0 +1,23 @@+import type { Plugin } from 'vite';+import path from 'node:path';+import { cwd as getCwd } from 'node:process';+import { generateI18nTypes } from './i18n-types.services';++const cwd = getCwd();++export function i18nTypesPlugin(): Plugin {+ return {+ name: 'vite-plugin-i18n-types',+ configureServer(server) {+ server.watcher.add(path.join(cwd, 'src/locales/en.yml'));+ server.watcher.on('change', (changedPath) => {+ if (changedPath.endsWith('en.yml')) {+ generateI18nTypes();+ }+ });+ },+ buildStart() {+ generateI18nTypes();+ },+ };+}diff --git a/apps/papra-client/src/plugins/i18n-types/i18n-types.services.ts b/apps/papra-client/src/plugins/i18n-types/i18n-types.services.tsnew file mode 100644index 00000000..2709f13a--- /dev/null+++ b/apps/papra-client/src/plugins/i18n-types/i18n-types.services.ts@@ -0,0 +1,30 @@+/* eslint-disable no-console */+import { readFile, writeFile } from 'node:fs/promises';+import path from 'node:path';+import { cwd as getCwd } from 'node:process';+import { flattenYaml } from '../yaml-flattened/yaml-flattened.models';++export async function generateI18nTypes({ cwd = getCwd() }: { cwd?: string } = {}) {+ try {+ const yamlPath = path.join(cwd, 'src/locales/en.yml');+ const outputPath = path.join(cwd, 'src/modules/i18n/locales.types.ts');++ const enLocales = await readFile(yamlPath, 'utf-8');+ const parsedLocales = flattenYaml({ code: enLocales });+ const localKeys = Object.keys(parsedLocales);++ const localesTypeDefinition = `+// Do not manually edit this file.+// This file is dynamically generated when the dev server runs (or using the \`pnpm script:generate-i18n-types\` command).+// Keys are extracted from the en.yml file.+// Source code : ${path.relative(cwd, __filename)}++export type LocaleKeys =\n${localKeys.map(key => ` | '${key}'`).join('\n')};+`.trimStart();++ await writeFile(outputPath, localesTypeDefinition);+ console.log('✅ Successfully generated i18n types');+ } catch (error) {+ console.error('❌ Error generating i18n types:', error);+ }+}diff --git a/apps/papra-client/src/scripts/generate-i18n-types.script.ts b/apps/papra-client/src/scripts/generate-i18n-types.script.tsindex 3cff2500..9a686e63 100644--- a/apps/papra-client/src/scripts/generate-i18n-types.script.ts+++ b/apps/papra-client/src/scripts/generate-i18n-types.script.ts@@ -1,15 +1,3 @@-import { readFile, writeFile } from 'node:fs/promises';-import { flattenYaml } from '@/plugins/yaml-flattened/yaml-flattened.models';+import { generateI18nTypes } from '@/plugins/i18n-types/i18n-types.services';-const enLocales = await readFile('src/locales/en.yml', 'utf-8');-const parsedLocales = flattenYaml({ code: enLocales });--const localKeys = Object.keys(parsedLocales);-const localesTypeDefinition = `-// Dynamically generated file. Use "pnpm script:generate-i18n-types" to update.-export type LocaleKeys = ${localKeys.map(key => `'${key}'`).join(' | ')};-`.trimStart();--await writeFile('src/modules/i18n/locales.types.ts', localesTypeDefinition);--console.log('Successfully generated i18n types');+await generateI18nTypes();diff --git a/apps/papra-client/vite.config.ts b/apps/papra-client/vite.config.tsindex cd695150..17df6153 100644--- a/apps/papra-client/vite.config.ts+++ b/apps/papra-client/vite.config.ts@@ -3,6 +3,7 @@ import unoCssPlugin from 'unocss/vite';import { defineConfig } from 'vite';import solidPlugin from 'vite-plugin-solid';import { version } from './package.json';+import { i18nTypesPlugin } from './src/plugins/i18n-types/i18n-types.plugin';import { yamlFlattenPlugin } from './src/plugins/yaml-flattened/yaml-flattened.plugin';export default defineConfig({@@ -10,6 +11,7 @@ export default defineConfig({yamlFlattenPlugin(),unoCssPlugin(),solidPlugin(),+ i18nTypesPlugin(),],define: {'import.meta.env.VITE_PAPRA_VERSION': JSON.stringify(version),