okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** 2* The computer's regional format, for the Monitor's cost line (#3628): which region and, where the system names it, which currency the 3* person uses. Read once, without PowerShell: 4* Windows the regional format (Settings > Time & language > Region > Regional format) from the registry: LocaleName ("en-US"); when it 5* names no region, the country or region setting (Geo\Name). 6* macOS `defaults read -g AppleLocale` ("en_US", or "de_DE@currency=CHF" when the person set another currency). 7* Linux LC_ALL, LC_MONETARY or LANG ("de_DE.UTF-8"), and the locale's own currency when `locale -k int_curr_symbol` answers. 8* Nothing here is a guess: no region means no answer, and the page then falls back to its own language list. 9*/ 10import { execFileSync } from 'node:child_process' ; 11 12const NONE = Object . freeze ({ locale :null , region :null , currency :null , source :null }); 13 14/** "en_US.UTF-8" / "de_DE@euro" / "zh-Hans-CN" -> { locale: "en-US", region: "US" } (null parts when the tag has none). */ 15export function parseLocaleTag ( text ) { 16const tag = String ( text ?? '' ). trim (). replace ( / [ .@ ] . * $ / , '' ). replace ( / _ / g , '-' ); 17if ( ! tag || / ^ ( c | posix ) $ / i . test ( tag )) return { locale :null , region :null }; 18const parts = tag . split ( '-' ); 19const region = parts . slice ( 1 ). find ( p => / ^ [ A - Za - z ]{ 2 } $ / . test ( p )) ?? null ; 20const language = / ^ [ A - Za - z ]{ 2 , 3 } $ / . test ( parts [ 0 ]) ?parts [ 0 ]. toLowerCase () :null ; 21if ( ! language ) return { locale :null , region :null }; 22return { locale :region ?` ${ language } - ${ region . toUpperCase ()} ` :language , region :region ?region . toUpperCase () :null }; 23} 24 25export function parseRegValue ( out , name ) { 26const m = new RegExp ( `^\\s* ${ name } \\s+REG_SZ\\s+(.*?)\\s*$` , 'mi' ). exec ( String ( out ?? '' )); 27return m ?m [ 1 ] :null ; 28} 29 30const currencyCode = value => ( / ^ [ A - Z ]{ 3 } $ / . test ( String ( value ?? '' ). trim (). toUpperCase ()) ?String ( value ). trim (). toUpperCase () :null ); 31 32export function detectRegion ({ platform= process . platform , env= process . env , run= ( cmd , args ) => execFileSync ( cmd , args , { encoding :'utf8' , timeout :4000 , windowsHide :true , stdio :[ 'ignore' , 'pipe' , 'ignore' ] }) } = {}) { 33try { 34if ( platform === 'win32' ) { 35const intl = 'HKCU\\Control Panel\\International' ; 36const named = parseLocaleTag ( parseRegValue ( run ( 'reg' , [ 'query' , intl , '/v' , 'LocaleName' ]), 'LocaleName' )); 37if ( named . region ) return { ...named , currency :null , source :'windows' }; 38let geo = null ; 39try { geo = parseRegValue ( run ( 'reg' , [ 'query' , ` ${ intl } \\Geo` , '/v' , 'Name' ]), 'Name' ); } catch { /* no country setting */ } 40const region = / ^ [ A - Za - z ]{ 2 } $ / . test ( geo ?? '' ) ?geo . toUpperCase () :null ; 41return region ?{ locale :named . locale ?` ${ named . locale } - ${ region } ` :null , region, currency :null , source :'windows' } :{ ...NONE }; 42} 43if ( platform === 'darwin' ) { 44const raw = String ( run ( 'defaults' , [ 'read' , '-g' , 'AppleLocale' ])). trim (); 45const named = parseLocaleTag ( raw ); 46const currency = currencyCode ( / @currency= ([ A - Za - z ]{ 3 }) / . exec ( raw )?.[ 1 ]); 47return named . region || currency ?{ ...named , currency, source :'macos' } :{ ...NONE }; 48} 49const named = parseLocaleTag ( env . LC_ALL || env . LC_MONETARY || env . LANG ); 50let currency = null ; 51try { currency = currencyCode ( / int_curr_symbol=" ? ([ A - Za - z ]{ 3 }) / . exec ( run ( 'locale' , [ '-k' , 'int_curr_symbol' ]))?.[ 1 ]); } catch { /* no locale command */ } 52return named . region || currency ?{ ...named , currency, source :'linux' } :{ ...NONE }; 53} catch { 54return { ...NONE }; 55} 56} 57 58let cached = null ; 59/** The answer is cached: the regional format does not change while the host runs. */ 60export function systemLocale ( options ) { 61if ( options ) return detectRegion ( options ); 62// A test or a demo can name the regional format (the screenshots of the Monitor); a packaged app never reads it. 63if ( process . env . STRATA_TEST_LOCALE && process . env . STRATA_PACKAGED !== '1' ) { const named = parseLocaleTag ( process . env . STRATA_TEST_LOCALE ); return { ...named , currency :null , source :'test' }; } 64cached ??= detectRegion (); 65return cached ; 66}