Modern Apps git

← Modern-Apps.git

news: carry per-article language (BCP-47)

Commit
13ef9e7255143bd8e4d5258ab46ddaa078725663
Author
Vayun Mathur <[email protected]>
When
2026-11-23 17:36 UTC (12 days ago)
Parents
f65b1c7
NewsSource gains language (six non-English defaults),
FeedArticle gains advisory language, and both the
server-feed parser and fallback sitemap path resolve
feed-stated -> catalog-default. Verified: news tests
green incl. language fixtures.

Diff

news/src/main/java/com/vayunmathur/news/data/NewsRepository.kt
diff --git a/news/src/main/java/com/vayunmathur/news/data/NewsRepository.kt b/news/src/main/java/com/vayunmathur/news/data/NewsRepository.ktindex e8cc9f3762..8cc736a892 100644--- a/news/src/main/java/com/vayunmathur/news/data/NewsRepository.kt+++ b/news/src/main/java/com/vayunmathur/news/data/NewsRepository.kt@@ -255,6 +255,7 @@ internal fun CachedArticle.toFeedArticle(): FeedArticle = FeedArticle(     url = url, sourceId = sourceId, title = title, publishedAtMillis = publishedAtMillis,     imageUrl = imageUrl,     keywords = keywords.split(",").map { it.trim() }.filter { it.isNotEmpty() },+    language = SourceCatalog.byId(sourceId)?.language, )  internal fun ExtractedArticle.toCached(json: Json): CachedBody = CachedBody(
news/src/main/java/com/vayunmathur/news/domain/FeedModels.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/FeedModels.kt b/news/src/main/java/com/vayunmathur/news/domain/FeedModels.ktindex 04e3e79855..0307058ac0 100644--- a/news/src/main/java/com/vayunmathur/news/domain/FeedModels.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/FeedModels.kt@@ -12,6 +12,8 @@ data class FeedArticle(     val keywords: List<String> = emptyList(),     /** Server-feed only: source meters this article. Not persisted (catalog badges cover it). */     val metered: Boolean = false,+    /** BCP-47 language (server `language`, or the source catalog default). Not persisted. */+    val language: String? = null, )  /** One block of an extracted article body, in reading order. */
news/src/main/java/com/vayunmathur/news/domain/ServerFeedParser.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/ServerFeedParser.kt b/news/src/main/java/com/vayunmathur/news/domain/ServerFeedParser.ktindex d97dcd1563..ebf423db1f 100644--- a/news/src/main/java/com/vayunmathur/news/domain/ServerFeedParser.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/ServerFeedParser.kt@@ -14,7 +14,7 @@ import kotlinx.serialization.json.jsonObject  * ```  * { "generated_at": ..., "articles": [  *     { "title": ..., "url": ..., "source": ..., "published_at": ...,- *       "image_url": ..., "metered": false } ] }+ *       "image_url": ..., "metered": false, "language": "en" } ] }  * ```  *  * Contract assumptions (flagged for reconciliation with the server agent):@@ -28,6 +28,9 @@ import kotlinx.serialization.json.jsonObject  * - `image_url` may be absent/null; `metered` may be absent (defaults  *   false). `metered` is carried on [FeedArticle] but not persisted:  *   the catalog already badges metered sources in the Sources screen.+ * - `language` is a BCP-47 tag stated per article by the server (its+ *   per-article refinement of the source default); when absent it+ *   defaults from [SourceCatalog] for the resolved source.  *  * Pure and non-throwing: malformed documents/articles yield an empty or  * partial list, which the repository treats as server-feed failure and@@ -51,13 +54,15 @@ object ServerFeedParser {         val url = obj.string("url")?.takeIf { it.startsWith("http") } ?: return null         val title = obj.string("title")?.takeIf { it.isNotBlank() } ?: return null         val sourceRaw = obj.string("source")?.takeIf { it.isNotBlank() } ?: return null+        val sourceId = resolveSourceId(sourceRaw)         return FeedArticle(             url = url,-            sourceId = resolveSourceId(sourceRaw),+            sourceId = sourceId,             title = title,             publishedAtMillis = parsePublishedAt(obj["published_at"]),             imageUrl = obj.string("image_url"),             metered = (obj["metered"] as? JsonPrimitive)?.contentOrNull?.toBooleanStrictOrNull() ?: false,+            language = obj.string("language") ?: SourceCatalog.byId(sourceId)?.language,         )     } 
news/src/main/java/com/vayunmathur/news/domain/SitemapParser.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/SitemapParser.kt b/news/src/main/java/com/vayunmathur/news/domain/SitemapParser.ktindex 8f2d4de023..2344ee5804 100644--- a/news/src/main/java/com/vayunmathur/news/domain/SitemapParser.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/SitemapParser.kt@@ -55,12 +55,16 @@ object SitemapParser {                 url = loc, sourceId = sourceId, title = title,                 publishedAtMillis = DateParsing.parseMillis(date) ?: 0L,                 imageUrl = image, keywords = keywords,+                language = languageOf(block, sourceId),             )         }         return out     }      private fun extractRss(doc: String, sourceId: String): List<FeedArticle> {+        // Channel-level <language> (before the first item) is the feed default.+        val head = doc.substringBefore("<item")+        val feedLanguage = tagText(head, "language") ?: tagText(head, "dc:language")         val out = mutableListOf<FeedArticle>()         forEachBlock(doc, "<item>", "</item>") { block ->             val link = tagText(block, "link") ?: return@forEachBlock@@ -68,7 +72,9 @@ object SitemapParser {             val date = tagText(block, "pubDate") ?: tagText(block, "dc:date")             val image = attributeAfter(block, "media:content", "url")                 ?: attributeAfter(block, "enclosure", "url")-            out += FeedArticle(link, sourceId, title, DateParsing.parseMillis(date) ?: 0L, image)+            val language = tagText(block, "dc:language") ?: tagText(block, "language")+                ?: feedLanguage ?: SourceCatalog.byId(sourceId)?.language+            out += FeedArticle(link, sourceId, title, DateParsing.parseMillis(date) ?: 0L, image, language = language)         }         return out     }@@ -79,11 +85,23 @@ object SitemapParser {             val link = attributeAfter(block, "link", "href") ?: return@forEachBlock             val title = tagText(block, "title") ?: link             val date = tagText(block, "published") ?: tagText(block, "updated")-            out += FeedArticle(decodeEntities(link), sourceId, title, DateParsing.parseMillis(date) ?: 0L)+            out += FeedArticle(+                decodeEntities(link), sourceId, title, DateParsing.parseMillis(date) ?: 0L,+                language = languageOf(block, sourceId),+            )         }         return out     } +    /**+     * Language stated by the feed for one entry (`news:language` /+     * `dc:language` / `language`), else the source catalog default.+     * Hreflang alternates are not used: they name other editions.+     */+    private fun languageOf(block: String, sourceId: String): String? =+        tagText(block, "news:language") ?: tagText(block, "dc:language") ?: tagText(block, "language")+            ?: SourceCatalog.byId(sourceId)?.language+     /**      * Visits each `<open>…</close>` block in order, in one forward pass.      * Unterminated trailing blocks are ignored.
news/src/main/java/com/vayunmathur/news/domain/SourceCatalog.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/SourceCatalog.kt b/news/src/main/java/com/vayunmathur/news/domain/SourceCatalog.ktindex 1959a080c1..e2faf3cd8d 100644--- a/news/src/main/java/com/vayunmathur/news/domain/SourceCatalog.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/SourceCatalog.kt@@ -16,6 +16,8 @@ data class NewsSource(     val feedUrl: String,     val kind: FeedKind,     val badge: String? = null,+    /** BCP-47 language of this source's feed (e.g. "en", "de"); default for its articles. */+    val language: String = "en", )  /** Built-in catalog; every URL here was fetched and parsed during development. */@@ -35,7 +37,7 @@ object SourceCatalog {         NewsSource("techcrunch", "TechCrunch", "https://techcrunch.com/news-sitemap.xml", FeedKind.SITEMAP),         NewsSource("propublica", "ProPublica", "https://www.propublica.org/news-sitemap.xml", FeedKind.SITEMAP),         NewsSource(-            "elpais", "El País", "https://elpais.com/sitemaps/news.xml", FeedKind.SITEMAP,+            "elpais", "El País", "https://elpais.com/sitemaps/news.xml", FeedKind.SITEMAP, language = "es",             badge = "Metered: full article text was present in the raw HTML during " +                 "verification; some articles may still truncate.",         ),@@ -49,10 +51,10 @@ object SourceCatalog {         ),         // Second verification round (2026-11-23, same bar as above: feed fetched         // with 200 and parsed; sampled article HTML held the full body).-        NewsSource("tagesschau", "Tagesschau", "https://www.tagesschau.de/newsticker.rdf", FeedKind.RSS),-        NewsSource("nos", "NOS", "https://feeds.nos.nl/nosnieuwsalgemeen", FeedKind.RSS),-        NewsSource("svt", "SVT Nyheter", "https://www.svt.se/nyheter/rss.xml", FeedKind.RSS),-        NewsSource("nrk", "NRK", "https://www.nrk.no/toppsaker.rss", FeedKind.RSS),+        NewsSource("tagesschau", "Tagesschau", "https://www.tagesschau.de/newsticker.rdf", FeedKind.RSS, language = "de"),+        NewsSource("nos", "NOS", "https://feeds.nos.nl/nosnieuwsalgemeen", FeedKind.RSS, language = "nl"),+        NewsSource("svt", "SVT Nyheter", "https://www.svt.se/nyheter/rss.xml", FeedKind.RSS, language = "sv"),+        NewsSource("nrk", "NRK", "https://www.nrk.no/toppsaker.rss", FeedKind.RSS, language = "nb"),         NewsSource("yle", "Yle News", "https://yle.fi/rss/news", FeedKind.RSS),         NewsSource("rte", "RTÉ News", "https://www.rte.ie/news/rss/news-headlines.xml", FeedKind.RSS),         NewsSource("cbc", "CBC News", "https://www.cbc.ca/webfeed/rss/rss-topstories", FeedKind.RSS),@@ -60,7 +62,7 @@ object SourceCatalog {         NewsSource("abc-au", "ABC Australia", "https://www.abc.net.au/news/feed/51120/rss.xml", FeedKind.RSS),         NewsSource("rnz", "RNZ", "https://www.rnz.co.nz/rss/national.xml", FeedKind.RSS),         NewsSource("cna", "CNA", "https://www.channelnewsasia.com/rssfeeds/8395986", FeedKind.RSS),-        NewsSource("ansa", "ANSA", "https://www.ansa.it/sito/ansait_rss.xml", FeedKind.RSS),+        NewsSource("ansa", "ANSA", "https://www.ansa.it/sito/ansait_rss.xml", FeedKind.RSS, language = "it"),         NewsSource("mee", "Middle East Eye", "https://www.middleeasteye.net/rss.xml", FeedKind.RSS),         NewsSource("semafor", "Semafor", "https://www.semafor.com/rss.xml", FeedKind.RSS),         NewsSource("restofworld", "Rest of World", "https://restofworld.org/feed/", FeedKind.RSS),
news/src/test/java/com/vayunmathur/news/domain/ServerFeedParserTest.kt
diff --git a/news/src/test/java/com/vayunmathur/news/domain/ServerFeedParserTest.kt b/news/src/test/java/com/vayunmathur/news/domain/ServerFeedParserTest.ktindex 13d2d4ad3a..e891e07a72 100644--- a/news/src/test/java/com/vayunmathur/news/domain/ServerFeedParserTest.kt+++ b/news/src/test/java/com/vayunmathur/news/domain/ServerFeedParserTest.kt@@ -8,17 +8,19 @@ class ServerFeedParserTest {     private val fixture = """         {"generated_at":"2026-11-23T12:00:00Z","articles":[           {"title":"Server Story","url":"https://example.com/a","source":"bbc",-           "published_at":"2026-11-23T10:00:00Z","image_url":"https://example.com/a.jpg","metered":true},+           "published_at":"2026-11-23T10:00:00Z","image_url":"https://example.com/a.jpg","metered":true,"language":"pt-BR"},           {"title":"Named Source","url":"https://example.com/b","source":"The Guardian",            "published_at":1795420000},           {"title":"No Image","url":"https://example.com/c","source":"npr",-           "published_at":"2026-11-23T08:00:00+00:00"}+           "published_at":"2026-11-23T08:00:00+00:00"},+          {"title":"German Story","url":"https://example.com/d","source":"tagesschau",+           "published_at":"2026-11-23T07:00:00Z"}         ]}     """.trimIndent()      @Test fun parsesContractFixture() {         val articles = ServerFeedParser.parse(fixture)-        assertEquals(3, articles.size)+        assertEquals(4, articles.size)         val first = articles[0]         assertEquals("bbc", first.sourceId)         assertEquals("https://example.com/a.jpg", first.imageUrl)@@ -27,6 +29,9 @@ class ServerFeedParserTest {         assertEquals("guardian", articles[1].sourceId, "exact catalog name must resolve to catalog id")         assertEquals(1_795_420_000_000L, articles[1].publishedAtMillis, "epoch seconds must become millis")         assertEquals(null, articles[2].imageUrl)+        assertEquals("pt-BR", first.language, "server-stated language wins")+        assertEquals("en", articles[1].language, "absent language defaults from the catalog")+        assertEquals("de", articles[3].language, "non-English catalog default")     }      @Test fun malformedInputIsEmptyNotThrowing() {