@@ -92,9 +92,11 @@ export function createTInstance(locale: string): Promise<TFunction> {
9292}
9393export { getLocale } ;
9494
95- export const stripLocaleFromId = ( id : string ) : string => {
96- const slashIdx = id . indexOf ( "/" ) ;
97- return slashIdx === - 1 ? id : id . slice ( slashIdx + 1 ) ;
95+ export const splitLocaleId = ( id : string ) : { locale : string ; slug : string } => {
96+ const i = id . indexOf ( "/" ) ;
97+ return i === - 1
98+ ? { locale : id , slug : id }
99+ : { locale : id . slice ( 0 , i ) , slug : id . slice ( i + 1 ) } ;
98100} ;
99101
100102const collectionMapCache = new Map < string , Promise < Map < string , unknown > > > ( ) ;
@@ -112,43 +114,37 @@ const getCollectionMap = <C extends CollectionKey>(
112114 return promise as Promise < Map < string , CollectionEntry < C > > > ;
113115} ;
114116
115- export const getLocalizedCollection = async < C extends CollectionKey > (
117+ export const applyLocale = async < C extends CollectionKey > (
118+ entries : CollectionEntry < C > [ ] ,
116119 name : C ,
117120 locale : string ,
118121) : Promise < CollectionEntry < C > [ ] > => {
122+ if ( locale === defaultLocale ) return entries ;
119123 const map = await getCollectionMap ( name ) ;
120- const defaults = [ ...map . values ( ) ] . filter ( ( e ) =>
121- e . id . startsWith ( `${ defaultLocale } /` ) ,
122- ) ;
123- if ( locale === defaultLocale ) return defaults ;
124- return defaults . map (
124+ return entries . map (
125125 ( e ) =>
126- ( map . get ( `${ locale } /${ stripLocaleFromId ( e . id ) } ` ) as
126+ ( map . get ( `${ locale } /${ splitLocaleId ( e . id ) . slug } ` ) as
127127 | CollectionEntry < C >
128128 | undefined ) ?? e ,
129129 ) ;
130130} ;
131131
132- export const getLocalizedEntry = async < C extends CollectionKey > (
132+ export const getLocalizedCollection = async < C extends CollectionKey > (
133133 name : C ,
134134 locale : string ,
135- slug : string ,
136- ) : Promise < CollectionEntry < C > | undefined > => {
135+ ) : Promise < CollectionEntry < C > [ ] > => {
137136 const map = await getCollectionMap ( name ) ;
138- return map . get ( `${ locale } /${ slug } ` ) ?? map . get ( `${ defaultLocale } /${ slug } ` ) ;
137+ const defaults = [ ...map . values ( ) ] . filter ( ( e ) =>
138+ e . id . startsWith ( `${ defaultLocale } /` ) ,
139+ ) ;
140+ return applyLocale ( defaults , name , locale ) ;
139141} ;
140142
141- export const applyLocale = async < C extends CollectionKey > (
142- entries : CollectionEntry < C > [ ] ,
143+ export const getLocalizedEntry = async < C extends CollectionKey > (
143144 name : C ,
144145 locale : string ,
145- ) : Promise < CollectionEntry < C > [ ] > => {
146- if ( locale === defaultLocale ) return entries ;
146+ slug : string ,
147+ ) : Promise < CollectionEntry < C > | undefined > => {
147148 const map = await getCollectionMap ( name ) ;
148- return entries . map (
149- ( e ) =>
150- ( map . get ( `${ locale } /${ stripLocaleFromId ( e . id ) } ` ) as
151- | CollectionEntry < C >
152- | undefined ) ?? e ,
153- ) ;
149+ return map . get ( `${ locale } /${ slug } ` ) ?? map . get ( `${ defaultLocale } /${ slug } ` ) ;
154150} ;
0 commit comments