okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** 2* The currency of the Monitor's cost line (#3628): the person's own, found from the computer's region, never typed and never a guess. 3* 4* Order: the system (the host reads the regional format of Windows, macOS or Linux: its region, or its currency when the system names one), 5* then the browser's language list, then the region of the app's language, and then no currency at all: the number is shown without a 6* symbol rather than in a currency the person does not use. A currency picked in Settings overrides all of this. 7*/ 8 9/** ISO 3166 region -> ISO 4217 currency. */ 10const PAIRS = `AD:EUR AE:AED AF:AFN AG:XCD AI:XCD AL:ALL AM:AMD AO:AOA AR:ARS AS:USD AT:EUR AU:AUD AW:AWG AX:EUR AZ:AZN 11BA:BAM BB:BBD BD:BDT BE:EUR BF:XOF BG:EUR BH:BHD BI:BIF BJ:XOF BL:EUR BM:BMD BN:BND BO:BOB BQ:USD BR:BRL BS:BSD BT:BTN BV:NOK BW:BWP BY:BYN BZ:BZD 12CA:CAD CC:AUD CD:CDF CF:XAF CG:XAF CH:CHF CI:XOF CK:NZD CL:CLP CM:XAF CN:CNY CO:COP CR:CRC CU:CUP CV:CVE CW:XCG CX:AUD CY:EUR CZ:CZK 13DE:EUR DJ:DJF DK:DKK DM:XCD DO:DOP DZ:DZD EC:USD EE:EUR EG:EGP EH:MAD ER:ERN ES:EUR ET:ETB FI:EUR FJ:FJD FK:FKP FM:USD FO:DKK FR:EUR 14GA:XAF GB:GBP GD:XCD GE:GEL GF:EUR GG:GBP GH:GHS GI:GIP GL:DKK GM:GMD GN:GNF GP:EUR GQ:XAF GR:EUR GS:GBP GT:GTQ GU:USD GW:XOF GY:GYD 15HK:HKD HM:AUD HN:HNL HR:EUR HT:HTG HU:HUF ID:IDR IE:EUR IL:ILS IM:GBP IN:INR IO:USD IQ:IQD IR:IRR IS:ISK IT:EUR 16JE:GBP JM:JMD JO:JOD JP:JPY KE:KES KG:KGS KH:KHR KI:AUD KM:KMF KN:XCD KP:KPW KR:KRW KW:KWD KY:KYD KZ:KZT 17LA:LAK LB:LBP LC:XCD LI:CHF LK:LKR LR:LRD LS:LSL LT:EUR LU:EUR LV:EUR LY:LYD 18MA:MAD MC:EUR MD:MDL ME:EUR MF:EUR MG:MGA MH:USD MK:MKD ML:XOF MM:MMK MN:MNT MO:MOP MP:USD MQ:EUR MR:MRU MS:XCD MT:EUR MU:MUR MV:MVR MW:MWK MX:MXN MY:MYR MZ:MZN 19NA:NAD NC:XPF NE:XOF NF:AUD NG:NGN NI:NIO NL:EUR NO:NOK NP:NPR NR:AUD NU:NZD NZ:NZD OM:OMR 20PA:PAB PE:PEN PF:XPF PG:PGK PH:PHP PK:PKR PL:PLN PM:EUR PR:USD PS:ILS PT:EUR PW:USD PY:PYG QA:QAR RE:EUR RO:RON RS:RSD RU:RUB RW:RWF 21SA:SAR SB:SBD SC:SCR SD:SDG SE:SEK SG:SGD SH:SHP SI:EUR SJ:NOK SK:EUR SL:SLE SM:EUR SN:XOF SO:SOS SR:SRD SS:SSP ST:STN SV:USD SX:XCG SY:SYP SZ:SZL 22TC:USD TD:XAF TF:EUR TG:XOF TH:THB TJ:TJS TK:NZD TL:USD TM:TMT TN:TND TO:TOP TR:TRY TT:TTD TV:AUD TW:TWD TZ:TZS 23UA:UAH UG:UGX UM:USD US:USD UY:UYU UZ:UZS VA:EUR VC:XCD VE:VES VG:USD VI:USD VN:VND VU:VUV WF:XPF WS:WST XK:EUR YE:YER YT:EUR ZA:ZAR ZM:ZMW ZW:ZWG` ; 24 25export const REGION_CURRENCY :Readonly < Record < string , string >> = Object . freeze ( Object . fromEntries ( PAIRS . split ( / \s+ / ). map ( pair => pair . split ( ':' ) as [ string , string ]))); 26 27/** The region of each app language that has one region of its own; English and Spanish-speaking countries are many, so only the first is a guess. */ 28export const LANGUAGE_REGION :Readonly < Record < string , string >> = Object . freeze ({ zh :'CN' , ja :'JP' , de :'DE' , es :'ES' , ru :'RU' , pt :'BR' , ko :'KR' }); 29 30export const currencyForRegion = ( region :string | null | undefined ) :string | null => ( region ?REGION_CURRENCY [ region . toUpperCase ()] ?? null :null ); 31 32/** The region in a language tag ("en-US", "de_DE.UTF-8", "zh-Hans-CN"), or null when the tag names none. */ 33export function regionOfLocale ( tag :string | null | undefined ) :string | null { 34if ( ! tag ) return null ; 35const clean = String ( tag ). replace ( / [.@].*$ / , '' ). replace ( / _ / g , '-' ); 36try { 37const region = new Intl . Locale ( clean ). region ; 38return region ?? null ; 39} catch { 40const found = / -([A-Za-z]{2})(?:-|$) / . exec ( clean ); 41return found ?found [ 1 ]. toUpperCase () :null ; 42} 43} 44 45/** What the host read from the system (GET /api/locale). */ 46export interface SystemLocale { locale :string | null ; region :string | null ; currency :string | null ; source :string | null } 47 48export interface ResolvedCurrency { currency :string | null ; locale :string ; source :'system' | 'browser' | 'language' | 'none' } 49 50/** 51* The currency to show money in, and the locale to write the number in. 52* `override` is the currency the person chose in Settings (empty: automatic). 53*/ 54export function resolveCurrency ({ system, browserLanguages= [], language, override= '' } :{ system ?:SystemLocale | null ; browserLanguages ?:readonly string []; language :string ; override ?:string }) :ResolvedCurrency { 55const appTag = language === 'zh' ?'zh-CN' :language ; 56const locale = system ?. locale ?? browserLanguages . find ( tag => regionOfLocale ( tag )) ?? browserLanguages [ 0 ] ?? appTag ; 57if ( / ^[A-Za-z]{3}$ / . test ( override )) return { currency :override . toUpperCase (), locale, source :'system' }; 58const fromSystem = system ?. currency ?? currencyForRegion ( system ?. region ); 59if ( fromSystem ) return { currency :fromSystem , locale, source :'system' }; 60for ( const tag of browserLanguages ) { 61const found = currencyForRegion ( regionOfLocale ( tag )); 62if ( found ) return { currency :found , locale, source :'browser' }; 63} 64const fromLanguage = currencyForRegion ( LANGUAGE_REGION [ language ]); 65if ( fromLanguage ) return { currency :fromLanguage , locale, source :'language' }; 66return { currency :null , locale, source :'none' }; 67} 68 69/** How many decimals this currency normally has (2 for dollars and euros, 0 for yen); 2 when there is no currency. */ 70export function minorDigits ( currency :string | null , locale :string ) :number { 71if ( ! currency ) return 2 ; 72try { return new Intl . NumberFormat ( locale , { style :'currency' , currency}). resolvedOptions (). maximumFractionDigits ?? 2 ; } catch { return 2 ; } 73} 74 75/** 76* An amount of money, written for the person's locale with the currency's usual digits ("$0.12", "$1.23", "1,23 €", "¥3"). 77* An amount below the smallest normal unit (one cent; one yen) is not shown as 0.00 or as a long run of decimals but as 78* "less than $0.01" (`lessThan` writes the sentence in the person's language). With no currency the number is shown alone, 79* and never in a currency that was only guessed. 80*/ 81export function formatMoney ( amount :number , { currency, locale, lessThan= shown => `less than ${ shown } ` } :{ currency :string | null ; locale :string ; lessThan ?:( shown :string ) => string }) :string { 82if ( ! Number . isFinite ( amount ) || amount < 0 ) return '—' ; 83const digits = minorDigits ( currency , locale ), unit = 10 ** - digits ; 84const write = ( value :number ) :string => { 85try { 86if ( currency ) return new Intl . NumberFormat ( locale , { style :'currency' , currency}). format ( value ); 87} catch { /* an unknown currency code or locale: the number alone */ } 88try { 89return new Intl . NumberFormat ( locale , { minimumFractionDigits :digits , maximumFractionDigits :digits }). format ( value ); 90} catch { 91return value . toFixed ( digits ); 92} 93}; 94return amount > 0 && amount < unit ?lessThan ( write ( unit )) :write ( amount ); 95} 96 97/** "USD ($)": the code and its symbol in the person's locale, for the Settings line. */ 98export function currencyLabel ( currency :string , locale :string ) :string { 99try { 100const symbol = new Intl . NumberFormat ( locale , { style :'currency' , currency, currencyDisplay :'narrowSymbol' }). formatToParts ( 0 ). find ( part => part . type === 'currency' )?. value ; 101return symbol && symbol !== currency ?` ${ currency } ( ${ symbol } )` :currency ; 102} catch { 103return currency ; 104} 105} 106 107/** Every currency this web view knows, by name, for the override list. */ 108export function currencyChoices ( locale :string ) :{ value :string ; label :string }[] { 109let codes :string [] = []; 110try { codes = ( Intl as unknown as { supportedValuesOf ?:( key :string ) => string [] }). supportedValuesOf ?.( 'currency' ) ?? []; } catch { /* an older web view */ } 111let names :Intl . DisplayNames | null = null ; 112try { names = new Intl . DisplayNames ([ locale ], { type :'currency' }); } catch { /* no names */ } 113return codes . map ( code => ({ value :code , label :` ${ code } – ${ names ?. of ( code ) ?? code } ` })); 114}