1- import { Catalog } from "../catalog.js"
21import { FallbackLocales } from "@lingui/conf"
32import type { AllCatalogsType , CatalogType , MessageType } from "../types.js"
43import { getFallbackListForLocale } from "./getFallbackListForLocale.js"
@@ -10,14 +9,23 @@ export type TranslationMissingEvent = {
109
1110export type MissingBehavior = "resolved" | "catalog"
1211
12+ export function isMissingBehavior ( value : string ) : value is MissingBehavior {
13+ return value === "resolved" || value === "catalog"
14+ }
15+
1316export type GetTranslationsOptions = {
1417 sourceLocale : string
1518 fallbackLocales : FallbackLocales
1619 missingBehavior ?: MissingBehavior
1720}
1821
22+ type CatalogTranslationsReader = {
23+ readAll ( locales : string [ ] ) : Promise < AllCatalogsType >
24+ readTemplate ( ) : Promise < CatalogType | undefined >
25+ }
26+
1927export async function getTranslationsForCatalog (
20- catalog : Catalog ,
28+ catalog : CatalogTranslationsReader ,
2129 locale : string ,
2230 options : GetTranslationsOptions ,
2331) {
@@ -27,22 +35,24 @@ export async function getTranslationsForCatalog(
2735 ...getFallbackListForLocale ( options . fallbackLocales , locale ) ,
2836 ] )
2937
30- const [ catalogs , template ] = await Promise . all ( [
38+ const [ rawCatalogs , rawTemplate ] = await Promise . all ( [
3139 catalog . readAll ( Array . from ( locales ) ) ,
3240 catalog . readTemplate ( ) ,
3341 ] )
3442
43+ const catalogs = withoutObsolete ( rawCatalogs )
44+ const template = withoutObsoleteCatalog ( rawTemplate )
3545 const sourceLocaleCatalog = catalogs [ options . sourceLocale ] || { }
3646
3747 const input = { ...template , ...sourceLocaleCatalog , ...catalogs [ locale ] }
3848
3949 const missing : TranslationMissingEvent [ ] = [ ]
4050
41- const messages = Object . keys ( input ) . reduce < { [ id : string ] : string } > (
42- ( acc , key ) => {
51+ const messages = Object . entries ( input ) . reduce < { [ id : string ] : string } > (
52+ ( acc , [ key , msg ] ) => {
4353 acc [ key ] = getTranslation (
4454 catalogs ,
45- input [ key ] ! ,
55+ msg ,
4656 locale ,
4757 key ,
4858 ( event ) => {
@@ -61,12 +71,41 @@ export async function getTranslationsForCatalog(
6171 }
6272}
6373
74+ function isActiveMessage (
75+ message : MessageType | undefined ,
76+ ) : message is MessageType {
77+ return Boolean ( message && ! message . obsolete )
78+ }
79+
80+ function withoutObsolete ( catalogs : AllCatalogsType ) : AllCatalogsType {
81+ return Object . fromEntries (
82+ Object . entries ( catalogs ) . map ( ( [ locale , catalog ] ) => [
83+ locale ,
84+ withoutObsoleteCatalog ( catalog ) ,
85+ ] ) ,
86+ )
87+ }
88+
89+ function withoutObsoleteCatalog ( catalog : CatalogType | undefined ) : CatalogType {
90+ const activeCatalog : CatalogType = { }
91+
92+ Object . entries ( catalog ?? { } ) . forEach ( ( [ id , message ] ) => {
93+ if ( isActiveMessage ( message ) ) {
94+ activeCatalog [ id ] = message
95+ }
96+ } )
97+
98+ return activeCatalog
99+ }
100+
64101function sourceLocaleFallback ( catalog : CatalogType | undefined , key : string ) {
65- if ( ! catalog ?. [ key ] ) {
102+ const message = catalog ?. [ key ]
103+
104+ if ( ! isActiveMessage ( message ) ) {
66105 return undefined
67106 }
68107
69- return catalog [ key ] . translation || catalog [ key ] . message
108+ return message . translation || message . message
70109}
71110
72111function getTranslation (
@@ -81,7 +120,13 @@ function getTranslation(
81120
82121 const getCatalogTranslation = ( _locale : string ) => {
83122 const localeCatalog = catalogs [ _locale ]
84- return localeCatalog ?. [ key ] ?. translation
123+ const message = localeCatalog ?. [ key ]
124+
125+ if ( ! isActiveMessage ( message ) ) {
126+ return undefined
127+ }
128+
129+ return message . translation
85130 }
86131
87132 const getMultipleFallbacks = ( _locale : string ) => {
0 commit comments