Skip to content

Commit 54ae846

Browse files
committed
feat(translation): expose sourceLang in article responses regardless of translation match
Previously TranslationResult only included translationMeta (and thus sourceLang) when a translation was applied. When the requested locale had no translation entry, the backend served the source content as fallback but omitted any indication of what language that source was — leaving clients unable to display accurate language metadata for fallback content. getTranslationAndAvailableLanguages now also returns sourceLang (read from any translation entry, since they all share the same source). translateArticle surfaces it as a top-level field on TranslationResult, populated in both the matched and no-match branches. The three article controllers (note/post/page) pass it through to the response.
1 parent 4458766 commit 54ae846

6 files changed

Lines changed: 19 additions & 3 deletions

File tree

apps/core/src/modules/ai/ai-translation/ai-translation.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ export class AiTranslationService
12751275
options?: { ignoreVisibility?: boolean },
12761276
): Promise<{
12771277
availableTranslations: string[]
1278+
sourceLang: string | null
12781279
translation: AITranslationModel | null
12791280
}> {
12801281
const article = await this.databaseService.findGlobalById(articleId)
@@ -1295,9 +1296,11 @@ export class AiTranslationService
12951296
.select('hash lang sourceLang sourceModified created')
12961297

12971298
if (!translations.length) {
1298-
return { availableTranslations: [], translation: null }
1299+
return { availableTranslations: [], sourceLang: null, translation: null }
12991300
}
13001301

1302+
const sourceLang = translations[0]?.sourceLang ?? null
1303+
13011304
const snapshot = this.buildSnapshotFromDocument(articleId, document)
13021305
const validLangs: string[] = []
13031306
const staleLangs: string[] = []
@@ -1341,6 +1344,7 @@ export class AiTranslationService
13411344

13421345
return {
13431346
availableTranslations: validLangs,
1347+
sourceLang,
13441348
translation: matchedTranslation,
13451349
}
13461350
}

apps/core/src/modules/note/note.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class NoteController {
126126
contentFormat: translationResult.contentFormat,
127127
}),
128128
isTranslated: translationResult.isTranslated,
129+
sourceLang: translationResult.sourceLang,
129130
translationMeta: translationResult.translationMeta,
130131
availableTranslations: translationResult.availableTranslations,
131132
hasInsightsInLocale,
@@ -606,6 +607,7 @@ export class NoteController {
606607
contentFormat: translationResult.contentFormat,
607608
}),
608609
isTranslated: translationResult.isTranslated,
610+
sourceLang: translationResult.sourceLang,
609611
translationMeta: translationResult.translationMeta,
610612
availableTranslations: translationResult.availableTranslations,
611613
hasInsightsInLocale,

apps/core/src/modules/page/page.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class PageController {
194194
contentFormat: translationResult.contentFormat,
195195
}),
196196
isTranslated: translationResult.isTranslated,
197+
sourceLang: translationResult.sourceLang,
197198
translationMeta: translationResult.translationMeta,
198199
availableTranslations: translationResult.availableTranslations,
199200
},

apps/core/src/modules/post/post.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ export class PostController {
374374
contentFormat: translationResult.contentFormat,
375375
}),
376376
isTranslated: translationResult.isTranslated,
377+
sourceLang: translationResult.sourceLang,
377378
translationMeta: translationResult.translationMeta,
378379
availableTranslations: translationResult.availableTranslations,
379380
liked,

apps/core/src/processors/helper/helper.translation.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface TranslationResult {
2222
content?: string
2323
contentFormat?: string
2424
isTranslated: boolean
25+
sourceLang?: string
2526
translationMeta?: TranslationMeta
2627
availableTranslations?: string[]
2728
}
@@ -69,15 +70,20 @@ export class TranslationService {
6970
const normalizedTarget = normalizeLanguageCode(targetLang)
7071

7172
try {
72-
const { availableTranslations, translation } =
73+
const { availableTranslations, sourceLang, translation } =
7374
await this.aiTranslationService.getTranslationAndAvailableLanguages(
7475
articleId,
7576
normalizedTarget || undefined,
7677
allowHidden ? { ignoreVisibility: true } : undefined,
7778
)
7879

7980
if (!normalizedTarget || !translation) {
80-
return { ...originalData, isTranslated: false, availableTranslations }
81+
return {
82+
...originalData,
83+
isTranslated: false,
84+
sourceLang: sourceLang ?? undefined,
85+
availableTranslations,
86+
}
8187
}
8288

8389
return {
@@ -89,6 +95,7 @@ export class TranslationService {
8995
content: translation.content,
9096
contentFormat: translation.contentFormat,
9197
isTranslated: true,
98+
sourceLang: translation.sourceLang,
9299
translationMeta: {
93100
sourceLang: translation.sourceLang,
94101
targetLang: translation.lang,

apps/core/test/src/processors/helper/helper.translation.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ describe('TranslationService', () => {
145145
summary: 'Translated Summary',
146146
tags: ['translated-tag'],
147147
isTranslated: true,
148+
sourceLang: 'zh',
148149
translationMeta: {
149150
sourceLang: 'zh',
150151
targetLang: 'en',

0 commit comments

Comments
 (0)