okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1631a9fde
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import ts from 'typescript' ; 4import { readdir , readFile } from 'node:fs/promises' ; 5import { join } from 'node:path' ; 6import { build } from 'esbuild' ; 7 8const languages = [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]; 9const read = async name => JSON . parse ( await readFile ( `src/locales/ ${ name } .json` , 'utf8' )); 10const built = await build ({ stdin :{ contents :"export * from './src/lib/i18n';" , resolveDir :process . cwd () }, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' } }); 11const { tr, setTranslationLanguage, loadCatalog} = await import ( `data:text/javascript;base64, ${ Buffer . from ( built . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 12for ( const l of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) await loadCatalog ( l ); // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr() 13 14test ( 'every tr("...") literal and every command hint has a key in the English catalog, so no language is stuck in English (#3479)' , async () => { 15const known = new Set (); 16for ( const name of [ 'en' , 'tune-en' , 'tools-en' , 'fix-en' , 'gh-en' , 'ui-en' , 'engine-en' , 'pick-en' , 'df2-en' , 'upd-en' , 'r3-en' , 'pic-en' , 'gui-en' , 'sav-en' ]) for ( const v of Object . values ( await read ( name ))) known . add ( String ( v )); 17const missing = []; 18const scan = async dir => { 19for ( const entry of await readdir ( dir , { withFileTypes :true })) { 20const path = join ( dir , entry . name ); 21if ( entry . isDirectory ()) { if ( entry . name !== 'locales' ) await scan ( path ); continue ; } 22if ( ! /\.tsx ? $ / . test ( path )) continue ; 23const source = await readFile ( path , 'utf8' ), sf = ts . createSourceFile ( path , source , ts . ScriptTarget . Latest , true , path . endsWith ( '.tsx' ) ?ts . ScriptKind . TSX :ts . ScriptKind . TS ); 24const visit = node => { 25if ( ts . isCallExpression ( node ) && node . expression . getText ( sf ) === 'tr' && node . arguments [ 0 ] && ( ts . isStringLiteral ( node . arguments [ 0 ]) || ts . isNoSubstitutionTemplateLiteral ( node . arguments [ 0 ]))) { 26const text = node . arguments [ 0 ]. text . trim (); 27if ( text && / [ a - zA - Z ]{ 2 } / . test ( text ) && ! known . has ( text )) missing . push ( ` ${ path } : ${ text } ` ); 28} 29if ( ts . isPropertyAssignment ( node ) && node . name . getText ( sf ) === 'hint' && ts . isStringLiteral ( node . initializer ) && ! path . endsWith ( 'chat-showcase.ts' )) { 30const text = node . initializer . text . trim (); 31if ( text && ! known . has ( text )) missing . push ( ` ${ path } : hint ${ text } ` ); 32} 33ts . forEachChild ( node , visit ); 34}; 35visit ( sf ); 36} 37}; 38await scan ( 'src' ); 39assert . deepEqual ( missing , []); 40}); 41 42test ( 'the fix catalogs have the same keys and placeholders in all nine languages' , async () => { 43const en = await read ( 'fix-en' ); 44for ( const language of languages ) { 45const catalog = await read ( `fix- ${ language } ` ); 46assert . deepEqual ( Object . keys ( catalog ), Object . keys ( en ), language ); 47for ( const [ id , text ] of Object . entries ( catalog )) assert . deepEqual (( text . match ( / \{\d + \} / g ) ?? []). sort (), ( en [ id ]. match ( / \{\d + \} / g ) ?? []). sort (), ` ${ language } ${ id } ` ); 48} 49}); 50 51test ( 'the /plan, /research and /new hints and the four "Open ..." hints are translated (#3479, #3825)' , () => { 52for ( const language of [ 'zh' , 'ru' ]) { 53setTranslationLanguage ( language ); 54for ( const hint of [ 'Have the assistant write a plan first; nothing runs until you approve it' , 'Search the web, read the pages and answer with sources' , 'Start a new conversation; keep this one' ]) 55assert . notEqual ( tr ( hint ), hint , ` ${ language } : ${ hint } ` ); 56for ( const label of [ 'Model settings' , 'Serving' , 'Models' , 'Skills' ]) { 57const hint = tr ( `Open ${ label } ` ); 58assert . notEqual ( hint , `Open ${ label } ` , ` ${ language } : ${ label } template` ); 59assert . ok ( ! hint . includes ( label ), ` ${ language } : the label is translated too: ${ hint } ` ); 60} 61} 62setTranslationLanguage ( 'zh' ); 63assert . equal ( tr ( 'Open Skills' ), '打开 技能' ); 64setTranslationLanguage ( 'en' ); 65assert . equal ( tr ( 'Open Skills' ), 'Open Skills' ); 66});