Log languages a post is tagged with after translating (#10014)

Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
DS Boyce
2026-03-17 10:31:23 -07:00
committed by GitHub
parent be0d00de17
commit 1386a559b7
11 changed files with 102 additions and 81 deletions
+13 -14
View File
@@ -61,6 +61,14 @@ function getLocalizedLanguage(
}
}
export function getPostLanguageTags(post: AppBskyFeedDefs.PostView) {
return AppBskyFeedPost.isRecord(post.record) &&
hasProp(post.record, 'langs') &&
Array.isArray(post.record.langs)
? post.record.langs
: []
}
export function languageName(language: Language, appLang: string): string {
// if Intl.DisplayNames is unavailable on the target, display the English name
if (!Intl.DisplayNames) {
@@ -80,22 +88,14 @@ export function codeToLanguageName(lang2or3: string, appLang: string): string {
export function getPostLanguage(
post: AppBskyFeedDefs.PostView,
): string | undefined {
let candidates: string[] = []
let candidates: string[] = getPostLanguageTags(post)
let postText: string = ''
if (hasProp(post.record, 'text') && typeof post.record.text === 'string') {
postText = post.record.text
}
if (
AppBskyFeedPost.isRecord(post.record) &&
hasProp(post.record, 'langs') &&
Array.isArray(post.record.langs)
) {
candidates = post.record.langs
}
// if there's only one declared language, use that
if (candidates?.length === 1) {
if (candidates.length === 1) {
return candidates[0]
}
@@ -108,11 +108,10 @@ export function getPostLanguage(
let langsProbabilityMap = lande(postText)
// filter down using declared languages
if (candidates?.length) {
if (candidates.length) {
langsProbabilityMap = langsProbabilityMap.filter(
([lang, _probability]: [string, number]) => {
return candidates.includes(code3ToCode2(lang))
},
([lang, _probability]: [string, number]) =>
candidates.includes(code3ToCode2(lang)),
)
}