1- import type { defaultLocalize as defaultLocalizeType , Enum as EnumType } from '@enum-plus' ;
1+ import type { defaultLocalize as defaultLocalizeType , EnumItemClass , Enum as EnumType } from '@enum-plus' ;
2+ import type { EnumValue , StandardEnumItemInit } from '@enum-plus/types' ;
23
34export const localeEN = {
45 'weekDay.name' : 'Week Days' ,
@@ -48,10 +49,12 @@ export let locales: typeof localeEN | typeof localeCN | typeof noLocale = noLoca
4849export let lang : 'en-US' | 'zh-CN' | undefined = undefined ;
4950
5051export function getLocales ( language : typeof lang ) {
51- return language === 'zh-CN' ? getLocales . localeCN : language ? getLocales . localeEN : noLocale ;
52+ const { localeCN, localeEN, noLocale } = getLocales ;
53+ return language === 'zh-CN' ? localeCN : language ? localeEN : noLocale ;
5254}
5355getLocales . localeCN = localeCN ;
5456getLocales . localeEN = localeEN ;
57+ getLocales . noLocale = noLocale ;
5558
5659type getLocalesType = typeof getLocales ;
5760export function setLang (
@@ -60,9 +63,10 @@ export function setLang(
6063 getLocales : getLocalesType ,
6164 defaultLocalize : typeof defaultLocalizeType
6265) {
66+ const { genSillyLocalizer } = setLang ;
6367 lang = value ;
6468 locales = getLocales ( value ) ;
65- Enum . localize = value ? setLang . genSillyLocalizer ( value , getLocales , defaultLocalize ) : defaultLocalize ;
69+ Enum . localize = value ? genSillyLocalizer ( value , getLocales ) : defaultLocalize ;
6670}
6771setLang . genSillyLocalizer = genSillyLocalizer ;
6872
@@ -81,6 +85,15 @@ export const StandardWeekConfig = {
8185 Friday : { value : 5 , label : noLocale . Friday , status : 'success' } ,
8286 Saturday : { value : 6 , label : noLocale . Saturday , status : 'error' } ,
8387} as const ;
88+ export const FuncLabelStandardWeekConfig = {
89+ Sunday : { value : 0 , label : labelLocalizer } ,
90+ Monday : { value : 1 , label : labelLocalizer } ,
91+ Tuesday : { value : 2 , label : labelLocalizer } ,
92+ Wednesday : { value : 3 , label : labelLocalizer } ,
93+ Thursday : { value : 4 , label : labelLocalizer } ,
94+ Friday : { value : 5 , label : labelLocalizer } ,
95+ Saturday : { value : 6 , label : labelLocalizer } ,
96+ } as const ;
8497export const ShortLabelStandardWeekConfig = {
8598 Sunday : { value : 0 , label : 'Sunday' } ,
8699 Monday : { value : 1 , label : 'Monday' } ,
@@ -180,41 +193,38 @@ export const WeekMetaOnlyConfig = Object.keys(StandardWeekConfig).reduce(
180193 { } as { [ key in TKey ] : Omit < TConfig [ key ] , 'value' | 'label' > }
181194) ;
182195
183- export function genSillyLocalizer (
184- language : typeof lang ,
185- getLocales : getLocalesType ,
186- defaultLocalize : typeof defaultLocalizeType
187- ) {
196+ export function genSillyLocalizer ( language : typeof lang , getLocales : getLocalesType ) {
188197 // should use function here to avoid closure. this is important for the e2e test cases.
189198 function sillyLocalize (
190- content : ( typeof StandardWeekConfig ) [ keyof typeof StandardWeekConfig ] [ 'label' ] | NonNullable < string > | undefined
191- ) : typeof content {
192- const locales = sillyLocalize . locales ;
199+ // eslint-disable-next-line @typescript-eslint/ban-types
200+ content : ( typeof noLocale ) [ keyof typeof noLocale ] | ( string & { } ) | undefined
201+ ) : string | undefined {
202+ const { locales } = sillyLocalize ;
193203 switch ( content ) {
194204 case 'weekDay.name' :
195- return locales [ 'weekDay.name' ] as typeof content ;
205+ return locales [ 'weekDay.name' ] ;
196206 case 'weekday.Sunday' :
197- return locales . Sunday as typeof content ;
207+ return locales . Sunday ;
198208 case 'weekday.Monday' :
199- return locales . Monday as typeof content ;
209+ return locales . Monday ;
200210 case 'weekday.Tuesday' :
201- return locales . Tuesday as typeof content ;
211+ return locales . Tuesday ;
202212 case 'weekday.Wednesday' :
203- return locales . Wednesday as typeof content ;
213+ return locales . Wednesday ;
204214 case 'weekday.Thursday' :
205- return locales . Thursday as typeof content ;
215+ return locales . Thursday ;
206216 case 'weekday.Friday' :
207- return locales . Friday as typeof content ;
217+ return locales . Friday ;
208218 case 'weekday.Saturday' :
209- return locales . Saturday as typeof content ;
210- case 'boolean.youes ' :
211- return locales . Yes as typeof content ;
219+ return locales . Saturday ;
220+ case 'boolean.Yes ' :
221+ return locales . Yes ;
212222 case 'boolean.No' :
213- return locales . No as typeof content ;
223+ return locales . No ;
214224 case 'date.FirstDay' :
215- return locales . FirstDay as typeof content ;
225+ return locales . FirstDay ;
216226 case 'date.LastDay' :
217- return locales . LastDay as typeof content ;
227+ return locales . LastDay ;
218228 default :
219229 return content ;
220230 }
@@ -223,6 +233,16 @@ export function genSillyLocalizer(
223233 return sillyLocalize ;
224234}
225235
236+ // eslint-disable-next-line @typescript-eslint/ban-types
237+ export function labelLocalizer ( item : EnumItemClass < StandardEnumItemInit < EnumValue > > ) {
238+ const { genSillyLocalizer, getLocales, noLocale } = labelLocalizer ;
239+ const localizer = genSillyLocalizer ( lang , getLocales ) ;
240+ return localizer ( noLocale [ item . key as keyof typeof noLocale ] ) ;
241+ }
242+ labelLocalizer . genSillyLocalizer = genSillyLocalizer ;
243+ labelLocalizer . getLocales = getLocales ;
244+ labelLocalizer . noLocale = noLocale ;
245+
226246export function localizeConfigData (
227247 config : typeof StandardWeekConfig ,
228248 locales : typeof localeEN | typeof localeCN | typeof noLocale
@@ -239,7 +259,8 @@ export function localizeConfigData(
239259 defaultLocalize ?: typeof defaultLocalizeType
240260) {
241261 if ( typeof getLocales === 'function' && defaultLocalize ) {
242- const localizer = localizeConfigData . genSillyLocalizer ( lang , getLocales , defaultLocalize ) ;
262+ const { genSillyLocalizer } = localizeConfigData ;
263+ const localizer = genSillyLocalizer ( lang , getLocales ) ;
243264 return Object . keys ( config ) . reduce (
244265 ( acc , key ) => {
245266 // @ts -expect-error: because cannot assign to 'value' because it is a read-only property.
0 commit comments