Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(i18n): sync email catalogs with any snake_case field

Hampus9ab3a532b343
diff --git a/packages/i18n/scripts/GenerateI18nTypes.ts b/packages/i18n/scripts/GenerateI18nTypes.ts
index c3f4ee5b5..a11f41da7 100644
--- a/packages/i18n/scripts/GenerateI18nTypes.ts
+++ b/packages/i18n/scripts/GenerateI18nTypes.ts
@@ -28,7 +28,8 @@ ${unionType};
export interface EmailTemplate {
subject: string;
- body: string;
+ intro: string;
+ [field: string]: string;
}
`;
}
diff --git a/packages/i18n/scripts/SyncStaticI18nCatalogs.ts b/packages/i18n/scripts/SyncStaticI18nCatalogs.ts
index 09697b3c6..d7a6d8df0 100644
--- a/packages/i18n/scripts/SyncStaticI18nCatalogs.ts
+++ b/packages/i18n/scripts/SyncStaticI18nCatalogs.ts
@@ -10,48 +10,12 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const REPO_ROOT = path.resolve(__dirname, '../../..');
-const STATIC_I18N_LOCALES = [
- 'ar',
- 'bg',
- 'cs',
- 'da',
- 'de',
- 'el',
- 'en-GB',
- 'es-419',
- 'es-ES',
- 'fi',
- 'fr',
- 'he',
- 'hi',
- 'hr',
- 'hu',
- 'id',
- 'it',
- 'ja',
- 'ko',
- 'lt',
- 'nl',
- 'no',
- 'pl',
- 'pt-BR',
- 'ro',
- 'ru',
- 'sv-SE',
- 'th',
- 'tr',
- 'uk',
- 'vi',
- 'zh-CN',
- 'zh-TW',
-] as const;
-
-type StaticLocale = (typeof STATIC_I18N_LOCALES)[number];
type FlatCatalog = Record<string, string>;
interface EmailTemplate {
subject: string;
- body: string;
+ intro: string;
+ [field: string]: string;
}
type EmailCatalog = Record<string, EmailTemplate>;
@@ -125,6 +89,17 @@ function sortKeys<T>(record: Record<string, T>): Record<string, T> {
return sorted;
}
+function sortCatalogForWeblate(config: StaticCatalogConfig, catalog: Catalog): Catalog {
+ if (config.kind !== 'email') {
+ return sortKeys(catalog as FlatCatalog);
+ }
+ const sorted: EmailCatalog = {};
+ for (const [key, template] of Object.entries(sortKeys(catalog as EmailCatalog))) {
+ sorted[key] = sortKeys(template) as EmailTemplate;
+ }
+ return sorted;
+}
+
function writeJson(filePath: string, value: unknown): void {
ensureDir(path.dirname(filePath));
fs.writeFileSync(filePath, `${JSON.stringify(value, null, '\t')}\n`, 'utf8');
@@ -145,17 +120,14 @@ function normalizeEmailCatalog(source: EmailCatalog, existing: unknown): EmailCa
const next: EmailCatalog = {};
for (const key of Object.keys(source).sort()) {
const sourceTemplate = source[key];
- const existingTemplate = existingRecord[key];
- next[key] = {
- subject:
- isRecord(existingTemplate) && typeof existingTemplate.subject === 'string'
- ? existingTemplate.subject
- : sourceTemplate.subject,
- body:
- isRecord(existingTemplate) && typeof existingTemplate.body === 'string'
- ? existingTemplate.body
- : sourceTemplate.body,
- };
+ const existingValue = existingRecord[key];
+ const existingTemplate = isRecord(existingValue) ? existingValue : {};
+ const template: Record<string, string> = {};
+ for (const field of Object.keys(sourceTemplate)) {
+ const value = existingTemplate[field];
+ template[field] = typeof value === 'string' ? value : sourceTemplate[field];
+ }
+ next[key] = template as EmailTemplate;
}
return next;
}
@@ -173,8 +145,13 @@ function validateCatalog(kind: CatalogKind, catalog: unknown, label: string): Ca
return catalog as FlatCatalog;
}
for (const [key, value] of Object.entries(catalog)) {
- if (!isRecord(value) || typeof value.subject !== 'string' || typeof value.body !== 'string') {
- throw new Error(`${label}.${key} must contain string subject and body fields`);
+ if (!isRecord(value) || typeof value.subject !== 'string' || typeof value.intro !== 'string') {
+ throw new Error(`${label}.${key} must contain string subject and intro fields`);
+ }
+ for (const [field, text] of Object.entries(value)) {
+ if (!/^[a-z][a-z0-9_]*$/.test(field) || typeof text !== 'string') {
+ throw new Error(`${label}.${key}.${field} must be a snake_case string field`);
+ }
}
}
return catalog as EmailCatalog;
@@ -196,7 +173,7 @@ async function extractCatalog(config: StaticCatalogConfig): Promise<void> {
for (const locale of STATIC_I18N_LOCALES) {
const next = normalizeCatalog(config, source, readJson<unknown>(jsonPath(config, locale)));
- writeJson(jsonPath(config, locale), sortKeys<Catalog[string]>(next));
+ writeJson(jsonPath(config, locale), sortCatalogForWeblate(config, next));
}
console.log(`extracted static ${config.name} catalogs`);
}
diff --git a/packages/i18n/src/runtime/MessageCatalogTypes.ts b/packages/i18n/src/runtime/MessageCatalogTypes.ts
index 3a2ebf817..be9a5ba5d 100644
--- a/packages/i18n/src/runtime/MessageCatalogTypes.ts
+++ b/packages/i18n/src/runtime/MessageCatalogTypes.ts
@@ -30,12 +30,12 @@ type NumericPlaceholderNameAfterOpen<T extends string> = T extends `${infer Name
: T extends `${infer Name}, selectordinal,${string}`
? ValidPlaceholderName<Trim<Name>>
: never;
-type TemplatePlaceholderNames<T extends string> = T extends `${string}{${infer AfterOpen}`
- ? PlaceholderNameAfterOpen<AfterOpen> | TemplatePlaceholderNames<AfterOpen>
- : never;
-type NumericTemplatePlaceholderNames<T extends string> = T extends `${string}{${infer AfterOpen}`
- ? NumericPlaceholderNameAfterOpen<AfterOpen> | NumericTemplatePlaceholderNames<AfterOpen>
- : never;
+type TemplatePlaceholderNames<T extends string, Found = never> = T extends `${string}{${infer AfterOpen}`
+ ? TemplatePlaceholderNames<AfterOpen, Found | PlaceholderNameAfterOpen<AfterOpen>>
+ : Found;
+type NumericTemplatePlaceholderNames<T extends string, Found = never> = T extends `${string}{${infer AfterOpen}`
+ ? NumericTemplatePlaceholderNames<AfterOpen, Found | NumericPlaceholderNameAfterOpen<AfterOpen>>
+ : Found;
export type MessageVariablesForTemplate<T extends string> = [TemplatePlaceholderNames<T>] extends [never]
? never
: {