okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
3.3 KiB27 linesraw
1// One-time migration retained for reference. Do not run it on localized source.
2throw new Error("Localization migration already applied. Add explicit tr() calls for new interface copy; preserve user content.");
3import ts from 'typescript';
4import {readdir,readFile,writeFile} from 'node:fs/promises';
5import {join} from 'node:path';
6const attrs=new Set(['title','label','hint','description','placeholder','alt','ariaLabel','aria-label','aria-description','emptyLabel','action']);
7const printer=ts.createPrinter({newLine:ts.NewLineKind.LineFeed});
8const f=ts.factory;
9function text(value){const lines=value.replace(/\r/g,'').split('\n');return lines.map((line,i)=>{line=line.replace(/\t/g,' ');if(i)line=line.replace(/^ +/,'');if(i<lines.length-1)line=line.replace(/ +$/,'');return line;}).filter(Boolean).join(' ');}
10function excluded(node){for(let p=node.parent;p;p=p.parent){if(ts.isJsxElement(p)){const name=p.openingElement.tagName.getText();if(['pre','code','textarea','Markdown','MarkdownContent','Code','Syntax','Mermaid'].includes(name))return true;}}return false;}
11function translate(expression){
12 if(ts.isCallExpression(expression)&&expression.expression.getText?.()==='tr')return expression;
13 if(ts.isTemplateExpression(expression)){let key=expression.head.text;const values=expression.templateSpans.map((span,i)=>{key+=`{${i}}${span.literal.text}`;return span.expression;});return f.createCallExpression(f.createIdentifier('tr'),undefined,[f.createStringLiteral(key),f.createArrayLiteralExpression(values)]);}
14 return f.createCallExpression(f.createIdentifier('tr'),undefined,[expression]);
15}
16async function scan(dir){for(const entry of await readdir(dir,{withFileTypes:true})){const path=join(dir,entry.name);if(entry.isDirectory())await scan(path);else if(path.endsWith('.tsx')&&!path.endsWith('/main.tsx')){
17 const source=await readFile(path,'utf8'),sf=ts.createSourceFile(path,source,ts.ScriptTarget.Latest,true,ts.ScriptKind.TSX);let changed=false;
18 const result=ts.transform(sf,[context=>root=>{const visit=node=>{
19  if(ts.isJsxText(node)&&!excluded(node)&&/[A-Za-z]/.test(node.text)){changed=true;return f.createJsxExpression(undefined,translate(f.createStringLiteral(text(node.text))));}
20  if(ts.isJsxAttribute(node)&&attrs.has(node.name.getText())&&node.initializer&&ts.isStringLiteral(node.initializer)){changed=true;return f.updateJsxAttribute(node,node.name,f.createJsxExpression(undefined,translate(node.initializer)));}
21  if(ts.isJsxExpression(node)&&node.expression){const p=node.parent;const isTextAttribute=ts.isJsxAttribute(p)&&attrs.has(p.name.getText());const isChild=ts.isJsxElement(p)||ts.isJsxFragment(p);const visited=ts.visitEachChild(node,visit,context);if((isTextAttribute||isChild)&&!excluded(node)&&! /(?:message\.content|message\.reasoning|msg\.content|session\.title|\.path\b|\.code\b|\.markdown\b|\.src\b)/.test(node.expression.getText())){changed=true;return f.updateJsxExpression(visited,translate(visited.expression));}return visited;}
22  return ts.visitEachChild(node,visit,context);
23 };return ts.visitNode(root,visit);}]);
24 if(changed){let output=printer.printFile(result.transformed[0]);if(!/import\s*\{[^}]*\btr\b[^}]*\}\s*from/.test(source)){const relative=path.startsWith('src/lib/')?'./i18n':'../lib/i18n';output=`import { tr } from '${relative}';\n`+output;}await writeFile(path,output);}
25 result.dispose();
26 }} }
27await scan('src');