Complete Phase 1 implementation of YTDescGen: - Project setup: Vite 5 + React 18 + TypeScript strict + Tailwind CSS dark theme - Config layer: 7 video types, 10 genres, 7 platforms, rig/social fields - i18n: EN/VI/JA with separate UI and template namespaces + validation script - Core engine: title-builder, description-builder, tag-generator (pure TS, zero React) - 42 unit tests covering all engine functions - Zustand stores: editor + settings with localStorage persistence - UI: 6 primitives, 11 editor components, 4 output components, 3 layout components - Pages: Editor (form + quick preview) and Output (title/desc/tags + copy) - CI/CD: GitHub Actions for lint/typecheck/test/build + GitHub Pages deploy Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import enTemplates from "../../src/i18n/locales/en/templates.json";
|
|
import viTemplates from "../../src/i18n/locales/vi/templates.json";
|
|
import jaTemplates from "../../src/i18n/locales/ja/templates.json";
|
|
import type { TranslationFn } from "../../src/engine/types";
|
|
|
|
type NestedRecord = Record<string, unknown>;
|
|
|
|
const templatesByLang: Record<string, NestedRecord> = {
|
|
en: enTemplates,
|
|
vi: viTemplates,
|
|
ja: jaTemplates,
|
|
};
|
|
|
|
function getNestedValue(obj: NestedRecord, path: string): unknown {
|
|
const keys = path.split(".");
|
|
let current: unknown = obj;
|
|
for (const key of keys) {
|
|
if (current === null || current === undefined || typeof current !== "object") {
|
|
return undefined;
|
|
}
|
|
current = (current as NestedRecord)[key];
|
|
}
|
|
return current;
|
|
}
|
|
|
|
function interpolate(template: string, vars: Record<string, string>): string {
|
|
return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) => vars[key] ?? "");
|
|
}
|
|
|
|
export function createMockT(lang = "en"): TranslationFn {
|
|
const templates = templatesByLang[lang] ?? templatesByLang["en"]!;
|
|
|
|
return (key: string, options?: Record<string, string>): string => {
|
|
const value = getNestedValue(templates, key);
|
|
if (typeof value !== "string") {
|
|
return key;
|
|
}
|
|
return options ? interpolate(value, options) : value;
|
|
};
|
|
}
|