@@ -2,36 +2,22 @@ import { formatLocale } from 'd3-format';
22
33import type { FormatLocaleDefinition , FormatLocaleObject } from 'd3-format' ;
44
5- import enUS from 'd3-format/locale/en-US.json' ;
6- import enGB from 'd3-format/locale/en-GB.json' ;
7- import zhCN from 'd3-format/locale/zh-CN.json' ;
8- import esES from 'd3-format/locale/es-ES.json' ;
9- import esMX from 'd3-format/locale/es-MX.json' ;
10- import deDE from 'd3-format/locale/de-DE.json' ;
11- import jaJP from 'd3-format/locale/ja-JP.json' ;
12- import frFR from 'd3-format/locale/fr-FR.json' ;
13- import ptBR from 'd3-format/locale/pt-BR.json' ;
14- import koKR from 'd3-format/locale/ko-KR.json' ;
15- import itIT from 'd3-format/locale/it-IT.json' ;
16- import nlNL from 'd3-format/locale/nl-NL.json' ;
17- import ruRU from 'd3-format/locale/ru-RU.json' ;
18-
195// Pre-built d3 locale definitions for the most popular locales.
206// Used as a fallback when Intl is unavailable (e.g. some edge runtimes).
21- export const formatD3NumericLocale : Record < string , FormatLocaleDefinition > = {
22- 'en-US' : enUS as unknown as FormatLocaleDefinition ,
23- 'en-GB' : enGB as unknown as FormatLocaleDefinition ,
24- 'zh-CN' : zhCN as unknown as FormatLocaleDefinition ,
25- 'es-ES' : esES as unknown as FormatLocaleDefinition ,
26- 'es-MX' : esMX as unknown as FormatLocaleDefinition ,
27- 'de-DE' : deDE as unknown as FormatLocaleDefinition ,
28- 'ja-JP' : jaJP as unknown as FormatLocaleDefinition ,
29- 'fr-FR' : frFR as unknown as FormatLocaleDefinition ,
30- 'pt-BR' : ptBR as unknown as FormatLocaleDefinition ,
31- 'ko-KR' : koKR as unknown as FormatLocaleDefinition ,
32- 'it-IT' : itIT as unknown as FormatLocaleDefinition ,
33- 'nl-NL' : nlNL as unknown as FormatLocaleDefinition ,
34- 'ru-RU' : ruRU as unknown as FormatLocaleDefinition ,
7+ export const formatD3NumericLocale : Record < string , Omit < FormatLocaleDefinition , 'currency' > > = {
8+ 'en-US' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
9+ 'en-GB' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
10+ 'zh-CN' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
11+ 'es-ES' : { decimal : ',' , thousands : '.' , grouping : [ 3 ] } ,
12+ 'es-MX' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
13+ 'de-DE' : { decimal : ',' , thousands : '.' , grouping : [ 3 ] } ,
14+ 'ja-JP' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
15+ 'fr-FR' : { decimal : ',' , thousands : '\u00a0' , grouping : [ 3 ] , percent : '\u202f%' } ,
16+ 'pt-BR' : { decimal : ',' , thousands : '.' , grouping : [ 3 ] } ,
17+ 'ko-KR' : { decimal : '.' , thousands : ',' , grouping : [ 3 ] } ,
18+ 'it-IT' : { decimal : ',' , thousands : '.' , grouping : [ 3 ] } ,
19+ 'nl-NL' : { decimal : ',' , thousands : '.' , grouping : [ 3 ] } ,
20+ 'ru-RU' : { decimal : ',' , thousands : '\u00a0' , grouping : [ 3 ] } ,
3521} ;
3622
3723const currencySymbols : Record < string , string > = {
@@ -45,7 +31,7 @@ const currencySymbols: Record<string, string> = {
4531 RUB : '₽' ,
4632} ;
4733
48- function getCurrencySymbol ( locale : string | undefined , currencyCode : string ) : [ string , string ] {
34+ function getCurrencyOverride ( locale : string | undefined , currencyCode : string ) : [ string , string ] {
4935 try {
5036 const cf = new Intl . NumberFormat ( locale , { style : 'currency' , currency : currencyCode } ) ;
5137 const currencyParts = cf . formatToParts ( 1 ) ;
@@ -88,7 +74,7 @@ function getD3NumericLocaleFromIntl(locale: string, currencyCode = 'USD'): Forma
8874 decimal : find ( 'decimal' ) || '.' ,
8975 thousands : find ( 'group' ) || ',' ,
9076 grouping : deriveGrouping ( locale ) ,
91- currency : getCurrencySymbol ( locale , currencyCode ) ,
77+ currency : getCurrencyOverride ( locale , currencyCode ) ,
9278 } ;
9379}
9480
@@ -103,14 +89,17 @@ export function getD3NumericLocale(locale: string, currencyCode = 'USD'): Format
10389 let definition : FormatLocaleDefinition ;
10490
10591 if ( formatD3NumericLocale [ locale ] ) {
106- definition = { ...formatD3NumericLocale [ locale ] , currency : getCurrencySymbol ( locale , currencyCode ) } ;
92+ definition = { ...formatD3NumericLocale [ locale ] , currency : getCurrencyOverride ( locale , currencyCode ) } ;
10793 } else {
10894 try {
10995 definition = getD3NumericLocaleFromIntl ( locale , currencyCode ) ;
11096 } catch ( e : unknown ) {
11197 console . warn ( 'Failed to generate d3 local via Intl, failing back to en-US' , e ) ;
11298
113- definition = formatD3NumericLocale [ 'en-US' ] ;
99+ definition = {
100+ ...formatD3NumericLocale [ 'en-US' ] ,
101+ currency : getCurrencyOverride ( locale , currencyCode )
102+ } ;
114103 }
115104 }
116105
0 commit comments