okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d9f47922
main
1// The web page's translation mechanism (serve/web/i18n.js), run in Node with a small stub of the DOM and fetch. 2// node serve/test_web_i18n.js (serve/test_web_i18n.py runs it too and skips when node is missing) 3"use strict" ; 4const assert = require ( "node:assert/strict" ); 5const path = require ( "node:path" ); 6 7// a page element that holds data-i18n marks and keeps its attributes 8class El { 9constructor ( attrs , text = "" ) { this . attrs = { ...attrs }; this . textContent = text ; this . dataset = {}; } 10getAttribute ( k ) { return k in this . attrs ?this . attrs [ k ] :null ; } 11setAttribute ( k , v ) { this . attrs [ k ] = String ( v ); } 12} 13const marks = [ 14new El ({ "data-i18n" :"a.text" }, "Rewind to here" ), 15new El ({ "data-i18n-title" :"a.title" , title :"Light / dark" }), 16new El ({ "data-i18n-label" :"a.label" , "aria-label" :"Send" }), 17new El ({ "data-i18n-placeholder" :"a.hint" , placeholder :"Ask anything…" }), 18]; 19globalThis . document = { 20documentElement :{ lang :"en" }, 21querySelectorAll :( sel ) => marks . filter (( e ) => sel . slice ( 1 , - 1 ) in e . attrs ), 22}; 23const catalogs = { 24de :{ "a.text" :"Hierher zurückspulen" , "a.title" :"Hell / Dunkel" , "chat.hidden" :{ one :"{n} Nachricht ausgeblendet" , other :"{n} Nachrichten ausgeblendet" }}, 25ru :{ "chat.hidden" :{ one :"{n} сообщение скрыто" , few :"{n} сообщения скрыто" , many :"{n} сообщений скрыто" , other :"{n} сообщения скрыто" }}, 26ja :{ "chat.hidden" :{ other :"{n} 件のメッセージを非表示" }}, 27}; 28let fetched = []; 29globalThis . fetch = async ( url ) => { 30fetched . push ( url ); 31const m = / web\/locales\/ ([ \w - ] + ) \.json$ / . exec ( url ); 32if ( m && catalogs [ m [ 1 ]]) return { ok :true , json :async () => catalogs [ m [ 1 ]]}; 33if ( m && m [ 1 ] === "zh" ) return { ok :true , json :async () => [ "not" , "an" , "object" ]}; 34if ( m && m [ 1 ] === "es" ) throw new Error ( "offline" ); 35return { ok :false , json :async () => { throw new Error ( "404" ); }}; 36}; 37const i18n = require ( path . join ( __dirname , "web" , "i18n.js" )); 38const { tr} = i18n ; 39const plural = { one :"{n} message hidden" , other :"{n} messages hidden" }; 40 41( async () => { 42// English: the text in the code, with its {placeholders}; a key with no text shows the key rather than nothing 43assert . equal ( tr ( "x.y" , "Hello {name}" , { name :"Ada" }), "Hello Ada" ); 44assert . equal ( tr ( "x.y" , "Hello {name}" ), "Hello {name}" , "an unknown placeholder stays visible" ); 45assert . equal ( tr ( "chat.hidden" , plural , { n :1 }), "1 message hidden" ); 46assert . equal ( tr ( "chat.hidden" , plural , { n :3 }), "3 messages hidden" ); 47assert . equal ( tr ( "x.y" , undefined ), "x.y" ); 48assert . equal ( globalThis . tr , tr , "tr is global for app.js" ); 49i18n . apply (); 50assert . equal ( marks [ 0 ]. textContent , "Rewind to here" , "English leaves the marked text alone" ); 51 52// a catalog replaces the text; a key it does not have falls back to the English text in the code 53assert . equal ( await i18n . setLanguage ( "de" ), "de" ); 54assert . equal ( document . documentElement . lang , "de" ); 55assert . equal ( marks [ 0 ]. textContent , "Hierher zurückspulen" ); 56assert . equal ( marks [ 1 ]. attrs . title , "Hell / Dunkel" ); 57assert . equal ( marks [ 2 ]. attrs [ "aria-label" ], "Send" , "no German text for this key: English stays" ); 58assert . equal ( tr ( "chat.hidden" , plural , { n :1 }), "1 Nachricht ausgeblendet" ); 59assert . equal ( tr ( "chat.hidden" , plural , { n :2 }), "2 Nachrichten ausgeblendet" ); 60assert . equal ( tr ( "only.english" , "Only English" ), "Only English" ); 61 62// plural forms of the language: Russian has few and many, Japanese only other 63await i18n . setLanguage ( "ru" ); 64assert . deepEqual ([ 1 , 2 , 5 , 21 , 22 ]. map (( n ) => tr ( "chat.hidden" , plural , { n})), 65[ "1 сообщение скрыто" , "2 сообщения скрыто" , "5 сообщений скрыто" , "21 сообщение скрыто" , "22 сообщения скрыто" ]); 66await i18n . setLanguage ( "ja" ); 67assert . equal ( tr ( "chat.hidden" , plural , { n :1 }), "1 件のメッセージを非表示" ); 68 69// switching back restores the English text that was in the page 70await i18n . setLanguage ( "de" ); 71await i18n . setLanguage ( "en" ); 72assert . equal ( marks [ 0 ]. textContent , "Rewind to here" ); 73assert . equal ( marks [ 1 ]. attrs . title , "Light / dark" ); 74assert . equal ( marks [ 3 ]. attrs . placeholder , "Ask anything…" ); 75assert . equal ( document . documentElement . lang , "en" ); 76 77// a language that is not offered, a catalog that is not an object, one that cannot be fetched, a missing file: 78// all stay English, and never throw 79fetched = []; 80assert . equal ( await i18n . setLanguage ( "xx" ), "en" ); 81assert . deepEqual ( fetched , [], "an unknown code is not even requested" ); 82assert . equal ( await i18n . setLanguage ( "zh" ), "en" ); 83assert . equal ( await i18n . setLanguage ( "es" ), "en" ); 84assert . equal ( await i18n . setLanguage ( "fr" ), "en" , "French is not in LANGUAGES yet" ); 85assert . equal ( await i18n . setLanguage ( "ko" ), "en" , "the file does not exist in this test" ); 86assert . equal ( tr ( "chat.hidden" , plural , { n :3 }), "3 messages hidden" ); 87 88// the list: English first, every entry [code, own name] 89assert . equal ( i18n . LANGUAGES [ 0 ][ 0 ], "en" ); 90for ( const [ code , name ] of i18n . LANGUAGES ) assert . ok ( / ^ [ a - z ]{ 2 , 3 }( - [ A - Z ]{ 2 }) ? $ / . test ( code ) && name , code ); 91console . log ( "ok web i18n" ); 92})(). catch (( e ) => { console . error ( e ); process . exitCode = 1 ; });