From ff4c41bc2d7389afbe113d7a362c9495d94d0c3d Mon Sep 17 00:00:00 2001 From: Artur Moreira Date: Fri, 27 Mar 2026 10:59:04 +0000 Subject: [PATCH] fix(frontend): add ui-i18n module and locale-aware copy for institutional theme The institutional header/footer already imported ui-i18n.ts but the file was missing from the repository. Add Portuguese/English UI strings, wire config.locale through homepage sections, category archives, article cards, and related articles, and use Intl.RelativeTimeFormat for relative dates. Refs: BIT-586 Made-with: Cursor --- .../components/article/RelatedArticles.astro | 5 +- frontend/src/components/home/LatestGrid.astro | 12 ++- frontend/src/lib/format.ts | 26 ++++-- frontend/src/lib/ui-i18n.ts | 93 +++++++++++++++++++ frontend/src/pages/article/[slug].astro | 2 +- frontend/src/pages/category/[slug].astro | 20 ++-- frontend/src/pages/index.astro | 5 +- .../themes/institutional/ArticleCard.astro | 9 +- .../institutional/CategorySection.astro | 11 ++- .../themes/institutional/HeroSection.astro | 11 ++- frontend/src/themes/sports/ArticleCard.astro | 5 +- .../src/themes/sports/CategorySection.astro | 7 +- frontend/src/themes/sports/HeroSection.astro | 7 +- 13 files changed, 168 insertions(+), 45 deletions(-) create mode 100644 frontend/src/lib/ui-i18n.ts diff --git a/frontend/src/components/article/RelatedArticles.astro b/frontend/src/components/article/RelatedArticles.astro index 60c7e38..452d173 100644 --- a/frontend/src/components/article/RelatedArticles.astro +++ b/frontend/src/components/article/RelatedArticles.astro @@ -5,8 +5,9 @@ import { getThemeComponents } from '../../lib/theme.js'; interface Props { theme: string; articles: ArticleListItem[]; + locale?: string; } -const { theme, articles } = Astro.props; +const { theme, articles, locale = 'en-US' } = Astro.props; const { ArticleCard } = getThemeComponents(theme); --- {articles.length > 0 && ( @@ -14,7 +15,7 @@ const { ArticleCard } = getThemeComponents(theme); diff --git a/frontend/src/components/home/LatestGrid.astro b/frontend/src/components/home/LatestGrid.astro index 51868b2..202eab4 100644 --- a/frontend/src/components/home/LatestGrid.astro +++ b/frontend/src/components/home/LatestGrid.astro @@ -1,29 +1,33 @@ --- import type { ArticleListItem } from '../../types/index.js'; import { getThemeComponents } from '../../lib/theme.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; interface Props { theme: string; articles: ArticleListItem[]; title?: string; show_more_href?: string; + locale?: string; } -const { theme, articles, title = 'Latest News', show_more_href } = Astro.props; +const { theme, articles, title, show_more_href, locale = 'en-US' } = Astro.props; const { ArticleCard } = getThemeComponents(theme); +const ui = getUiStrings(locale); +const heading = title ?? ui.latest_news; ---
-

{title}

+

{heading}

{show_more_href && ( - View all + {ui.view_all} )}
{articles.map(article => ( - + ))}
diff --git a/frontend/src/lib/format.ts b/frontend/src/lib/format.ts index b367a5e..8f8b7dd 100644 --- a/frontend/src/lib/format.ts +++ b/frontend/src/lib/format.ts @@ -17,18 +17,26 @@ export function formatDateShort(iso: string, locale = 'en-US'): string { }).format(d); } -export function relativeTime(iso: string): string { - const now = Date.now(); +export function relativeTime(iso: string, locale = 'en-US'): string { const parsed = new Date(iso); if (isNaN(parsed.getTime())) return iso; - const then = parsed.getTime(); - const diff = (now - then) / 1000; + const now = Date.now(); + const diffSec = (now - parsed.getTime()) / 1000; + const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); - if (diff < 60) return 'Just now'; - if (diff < 3600) return `${Math.floor(diff / 60)}m ago`; - if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`; - if (diff < 604800) return `${Math.floor(diff / 86400)}d ago`; - return formatDateShort(iso); + if (diffSec < 60) { + return rtf.format(-Math.max(0, Math.floor(diffSec)), 'second'); + } + if (diffSec < 3600) { + return rtf.format(-Math.floor(diffSec / 60), 'minute'); + } + if (diffSec < 86400) { + return rtf.format(-Math.floor(diffSec / 3600), 'hour'); + } + if (diffSec < 604800) { + return rtf.format(-Math.floor(diffSec / 86400), 'day'); + } + return formatDateShort(iso, locale); } export function truncate(str: string, max: number): string { diff --git a/frontend/src/lib/ui-i18n.ts b/frontend/src/lib/ui-i18n.ts new file mode 100644 index 0000000..3f198e5 --- /dev/null +++ b/frontend/src/lib/ui-i18n.ts @@ -0,0 +1,93 @@ +/** + * Static UI copy keyed by domain `locale` (BCP 47). + * Extend as more locales ship; unknown locales fall back to English. + */ +export type UiStrings = { + skip_to_content: string; + nav_home: string; + nav_main: string; + nav_mobile: string; + toggle_theme: string; + open_menu: string; + close_menu: string; + view_all: string; + latest_news: string; + breadcrumb_home: string; + articles_many: string; + article_one: string; + no_articles_category: string; + prev_page: string; + next_page: string; + min_read: string; + featured: string; + privacy: string; + terms: string; + sitemap: string; + sections_categories: string; + info: string; + all_rights_reserved: string; + /** Localised “page X of Y” for archive headers. */ + pageOf: (page: number, total: number) => string; +}; + +const en: UiStrings = { + skip_to_content: 'Skip to content', + nav_home: 'Home', + nav_main: 'Main navigation', + nav_mobile: 'Mobile navigation', + toggle_theme: 'Toggle dark and light mode', + open_menu: 'Open menu', + close_menu: 'Close menu', + view_all: 'View all', + latest_news: 'Latest news', + breadcrumb_home: 'Home', + articles_many: 'articles', + article_one: 'article', + no_articles_category: 'No articles found in this category yet.', + prev_page: 'Previous page', + next_page: 'Next page', + min_read: 'min read', + featured: 'Featured', + privacy: 'Privacy policy', + terms: 'Terms', + sitemap: 'Sitemap', + sections_categories: 'Sections', + info: 'Information', + all_rights_reserved: 'All rights reserved.', + pageOf: (page, total) => `Page ${page} of ${total}`, +}; + +const pt: UiStrings = { + skip_to_content: 'Saltar para o conteúdo', + nav_home: 'Início', + nav_main: 'Navegação principal', + nav_mobile: 'Navegação (telemóvel)', + toggle_theme: 'Alternar modo claro e escuro', + open_menu: 'Abrir menu', + close_menu: 'Fechar menu', + view_all: 'Ver tudo', + latest_news: 'Últimas notícias', + breadcrumb_home: 'Início', + articles_many: 'artigos', + article_one: 'artigo', + no_articles_category: 'Ainda não há artigos nesta secção.', + prev_page: 'Página anterior', + next_page: 'Página seguinte', + min_read: 'min de leitura', + featured: 'Destaque', + privacy: 'Política de privacidade', + terms: 'Termos', + sitemap: 'Mapa do site', + sections_categories: 'Secções', + info: 'Informação', + all_rights_reserved: 'Todos os direitos reservados.', + pageOf: (page, total) => `Página ${page} de ${total}`, +}; + +export function getUiStrings(locale: string): UiStrings { + const base = locale.split('-')[0]?.toLowerCase() ?? 'en'; + if (base === 'pt') { + return pt; + } + return en; +} diff --git a/frontend/src/pages/article/[slug].astro b/frontend/src/pages/article/[slug].astro index d40a1a0..c72b058 100644 --- a/frontend/src/pages/article/[slug].astro +++ b/frontend/src/pages/article/[slug].astro @@ -139,7 +139,7 @@ const breadcrumb_schema = { - + diff --git a/frontend/src/pages/category/[slug].astro b/frontend/src/pages/category/[slug].astro index 3c5c188..55e68e8 100644 --- a/frontend/src/pages/category/[slug].astro +++ b/frontend/src/pages/category/[slug].astro @@ -3,6 +3,7 @@ import Base from '../../layouts/Base.astro'; import { getDomainConfig } from '../../lib/domain.js'; import { getThemeComponents } from '../../lib/theme.js'; import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../../lib/bridge.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; const { slug } = Astro.params; if (!slug) return Astro.redirect('/'); @@ -40,6 +41,9 @@ try { const cat_config = config.categories.find(c => c.slug === slug); const display_name = cat_config?.label ?? category.name; +const ui = getUiStrings(config.locale); +const article_count_label = + paginated.total === 1 ? `1 ${ui.article_one}` : `${paginated.total} ${ui.articles_many}`; const total_pages = paginated.total_pages; const has_prev = page > 1; @@ -51,7 +55,7 @@ const breadcrumb_schema = { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: [ - { '@type': 'ListItem', position: 1, name: 'Home', item: Astro.url.origin }, + { '@type': 'ListItem', position: 1, name: ui.breadcrumb_home, item: Astro.url.origin }, { '@type': 'ListItem', position: 2, name: display_name, item: `${Astro.url.origin}/category/${slug}` }, ], }; @@ -66,7 +70,7 @@ const breadcrumb_schema = {
@@ -78,20 +82,20 @@ const breadcrumb_schema = {

{category.description}

)}

- {paginated.total} article{paginated.total !== 1 ? 's' : ''} - {total_pages > 1 && ` — page ${page} of ${total_pages}`} + {article_count_label} + {total_pages > 1 && ` — ${ui.pageOf(page, total_pages)}`}

{paginated.items.map(article => ( - + ))}
{paginated.items.length === 0 && ( -

No articles found in this category yet.

+

{ui.no_articles_category}

)} @@ -101,7 +105,7 @@ const breadcrumb_schema = { ← )} @@ -122,7 +126,7 @@ const breadcrumb_schema = { → )} diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro index 0ca1581..204f421 100644 --- a/frontend/src/pages/index.astro +++ b/frontend/src/pages/index.astro @@ -51,7 +51,7 @@ const website_schema = { schema={website_schema} > - + {config.ads_enabled && (
@@ -62,14 +62,15 @@ const website_schema = { {config.categories.filter(cat => homepage.by_category[cat.slug]?.length).map(cat => ( ))} diff --git a/frontend/src/themes/institutional/ArticleCard.astro b/frontend/src/themes/institutional/ArticleCard.astro index f92580f..4626405 100644 --- a/frontend/src/themes/institutional/ArticleCard.astro +++ b/frontend/src/themes/institutional/ArticleCard.astro @@ -1,14 +1,17 @@ --- import type { ArticleListItem } from '../../types/index.js'; import { relativeTime, truncate } from '../../lib/format.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; interface Props { article: ArticleListItem; size?: 'sm' | 'md' | 'lg'; show_excerpt?: boolean; + locale?: string; } -const { article, size = 'md', show_excerpt = true } = Astro.props; +const { article, size = 'md', show_excerpt = true, locale = 'en-US' } = Astro.props; +const ui = getUiStrings(locale); const is_large = size === 'lg'; const is_small = size === 'sm'; --- @@ -28,7 +31,7 @@ const is_small = size === 'sm'; @@ -77,7 +80,7 @@ const secondary = rest.slice(0, 4); {article.title}
diff --git a/frontend/src/themes/sports/ArticleCard.astro b/frontend/src/themes/sports/ArticleCard.astro index 5425d2c..1c62c1f 100644 --- a/frontend/src/themes/sports/ArticleCard.astro +++ b/frontend/src/themes/sports/ArticleCard.astro @@ -6,9 +6,10 @@ interface Props { article: ArticleListItem; size?: 'sm' | 'md' | 'lg'; show_excerpt?: boolean; + locale?: string; } -const { article, size = 'md', show_excerpt = true } = Astro.props; +const { article, size = 'md', show_excerpt = true, locale = 'en-US' } = Astro.props; const is_large = size === 'lg'; const is_small = size === 'sm'; --- @@ -56,7 +57,7 @@ const is_small = size === 'sm'; {article.author.name} {article.reading_time} min read diff --git a/frontend/src/themes/sports/CategorySection.astro b/frontend/src/themes/sports/CategorySection.astro index f83827f..65dfc1a 100644 --- a/frontend/src/themes/sports/CategorySection.astro +++ b/frontend/src/themes/sports/CategorySection.astro @@ -5,9 +5,10 @@ import ArticleCard from './ArticleCard.astro'; interface Props { category: CategoryConfig; articles: ArticleListItem[]; + locale?: string; } -const { category, articles } = Astro.props; +const { category, articles, locale = 'en-US' } = Astro.props; --- {articles.length > 0 && (
@@ -20,12 +21,12 @@ const { category, articles } = Astro.props;
- +
{articles.slice(1, 4).map(article => ( - + ))}
diff --git a/frontend/src/themes/sports/HeroSection.astro b/frontend/src/themes/sports/HeroSection.astro index ee3cede..26c8819 100644 --- a/frontend/src/themes/sports/HeroSection.astro +++ b/frontend/src/themes/sports/HeroSection.astro @@ -4,8 +4,9 @@ import { relativeTime } from '../../lib/format.js'; interface Props { articles: ArticleListItem[]; + locale?: string; } -const { articles } = Astro.props; +const { articles, locale = 'en-US' } = Astro.props; const [main, ...secondary] = articles; --- {main && ( @@ -45,7 +46,7 @@ const [main, ...secondary] = articles;
{main.author.name} - + {main.reading_time} min
@@ -78,7 +79,7 @@ const [main, ...secondary] = articles; {article.title}
- + {article.reading_time} min