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 { readFile } from 'node:fs/promises' ; 4import { build } from 'esbuild' ; 5 6// French (#3970): the catalogs are complete (the generic language tests cover keys and placeholders); these tests cover 7// what is specific to French: spacing before ? ! : ;, apostrophes, short labels staying short, and the picker entry. 8const names = [[ 'en' , 'fr' ], [ 'fix-en' , 'fix-fr' ], [ 'tune-en' , 'tune-fr' ], [ 'tools-en' , 'tools-fr' ]]; 9const read = async name => JSON . parse ( await readFile ( `src/locales/ ${ name } .json` , 'utf8' )); 10const compiled = 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, localeTag, currentLanguage, LANGUAGES , validLanguage, loadCatalog} = await import ( `data:text/javascript;base64, ${ Buffer . from ( compiled . 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 ( 'French is in the language picker under its own name' , () => { 15const entry = LANGUAGES . find ( item => item . value === 'fr' ); 16assert . equal ( entry ?. label , 'Français' ); 17assert . ok ( validLanguage ( 'fr' )); 18assert . equal ( LANGUAGES . length , 9 ); 19setTranslationLanguage ( 'fr' ); 20assert . equal ( currentLanguage (), 'fr' ); 21assert . equal ( localeTag (), 'fr' ); 22assert . equal ( tr ( 'Settings' ), 'Paramètres' ); 23assert . equal ( tr ( 'Cancel' ), 'Annuler' ); 24assert . equal ( tr ( 'Open {0}' , [ 'Modèles' ]). includes ( '{' ), false ); 25assert . equal ( tr ( 'unrecognized user content Ω' ), 'unrecognized user content Ω' ); 26setTranslationLanguage ( 'en' ); 27}); 28 29test ( 'French text keeps the French typography: a no-break space before ? ! : ; and a typographic apostrophe' , async () => { 30const bad = []; 31for ( const [ englishName , frenchName ] of names ) { 32const english = await read ( englishName ), french = await read ( frenchName ); 33for ( const [ id , text ] of Object . entries ( french )) { 34if ( text === english [ id ]) continue ; // kept as is: names, file names, class names 35if ( / [ A - Za - zÀ - ÿ \)\]\d % ] [ ?!;: ] / . test ( text ) && ! /https ? : | \d:\d / . test ( text )) bad . push ( ` ${ id } : ordinary space before punctuation: ${ text } ` ); 36if ( / [ A - Za - zÀ - ÿ \)\]\d % ][ ?!; ] / . test ( text )) bad . push ( ` ${ id } : no space before ? ! or ;: ${ text } ` ); 37if ( / [ A - Za - zÀ - ÿ ] ' [ A - Za - zÀ - ÿ{ ] / . test ( text )) bad . push ( ` ${ id } : straight apostrophe: ${ text } ` ); 38} 39} 40assert . deepEqual ( bad , []); 41}); 42 43test ( 'short labels stay short in French (buttons, tabs, chips)' , async () => { 44const english = await read ( 'en' ), french = await read ( 'fr' ); 45const long = []; 46for ( const [ id , text ] of Object . entries ( english )) { 47if ( text . length > 10 || french [ id ] === text || / \{\d + \} / . test ( text )) continue ; 48// a label of up to 10 letters may take up to 3 times the room, and never more than 36 characters 49if ( french [ id ]. length > Math . max ( 18 , text . length * 3.2 ) || french [ id ]. length > 36 ) long . push ( ` ${ id } : ${ text } -> ${ french [ id ]} ( ${ french [ id ]. length } )` ); 50} 51assert . deepEqual ( long , []); 52}); 53 54test ( 'no French string is still English by accident: at most 5% of the main catalog is left unchanged' , async () => { 55const english = await read ( 'en' ), french = await read ( 'fr' ); 56const same = Object . keys ( english ). filter ( id => english [ id ] === french [ id ]); 57assert . ok ( same . length < Object . keys ( english ). length * 0.05 , ` ${ same . length } unchanged` ); 58// every unchanged string has no real words in it, or is a name that is spelled the same in French 59const words = same . filter ( id => english [ id ]. split ( / \s +/ ). length >= 4 && / \s ( the | your | you | is | are | this | that | with | from | and ) \s / i . test ( english [ id ]) && ! /^ [ -/ \\ ] / . test ( english [ id ])); 60assert . deepEqual ( words . map ( id => ` ${ id } : ${ english [ id ]} ` ), []); 61});