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
This commit is contained in:
1 parent
aff73455d0
commit
ff4c41bc2d
13 files changed
+168
-45
No files matched your search
@@ -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);
|
||||
<h2 class="section-title" id="related-title">Related Articles</h2>
|
||||
<div class="related-grid">
|
||||
{articles.map(article => (
|
||||
<ArticleCard article={article} size="md" show_excerpt={false} />
|
||||
<ArticleCard article={article} size="md" show_excerpt={false} locale={locale} />
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -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;
|
||||
---
|
||||
<section class="latest-section" aria-labelledby="latest-title">
|
||||
<div class="container">
|
||||
<div class="latest-header">
|
||||
<h2 class="section-title" id="latest-title">{title}</h2>
|
||||
<h2 class="section-title" id="latest-title">{heading}</h2>
|
||||
{show_more_href && (
|
||||
<a href={show_more_href} class="btn btn--ghost latest-more">View all</a>
|
||||
<a href={show_more_href} class="btn btn--ghost latest-more">{ui.view_all}</a>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div class="latest-grid">
|
||||
{articles.map(article => (
|
||||
<ArticleCard article={article} size="md" show_excerpt={true} />
|
||||
<ArticleCard article={article} size="md" show_excerpt={true} locale={locale} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -139,7 +139,7 @@ const breadcrumb_schema = {
|
||||
</div>
|
||||
|
||||
<!-- Related articles -->
|
||||
<RelatedArticles theme={config.theme} articles={related} />
|
||||
<RelatedArticles theme={config.theme} articles={related} locale={config.locale} />
|
||||
</div>
|
||||
</Base>
|
||||
|
||||
|
||||
@@ -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 = {
|
||||
<div class="container">
|
||||
<!-- Breadcrumb -->
|
||||
<nav class="breadcrumb" style="padding-top: 1.5rem" aria-label="Breadcrumb">
|
||||
<a href="/">Home</a>
|
||||
<a href="/">{ui.breadcrumb_home}</a>
|
||||
<span class="breadcrumb__sep" aria-hidden="true">/</span>
|
||||
<span aria-current="page">{display_name}</span>
|
||||
</nav>
|
||||
@@ -78,20 +82,20 @@ const breadcrumb_schema = {
|
||||
<p class="category-header__desc">{category.description}</p>
|
||||
)}
|
||||
<p class="category-header__count text-muted text-sm">
|
||||
{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)}`}
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<!-- Articles grid -->
|
||||
<div class="category-grid" aria-label={`${display_name} articles`}>
|
||||
{paginated.items.map(article => (
|
||||
<ArticleCard article={article} size="md" show_excerpt={true} />
|
||||
<ArticleCard article={article} size="md" show_excerpt={true} locale={config.locale} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
{paginated.items.length === 0 && (
|
||||
<p class="empty-state">No articles found in this category yet.</p>
|
||||
<p class="empty-state">{ui.no_articles_category}</p>
|
||||
)}
|
||||
|
||||
<!-- Pagination -->
|
||||
@@ -101,7 +105,7 @@ const breadcrumb_schema = {
|
||||
<a
|
||||
href={`/category/${slug}?page=${page - 1}`}
|
||||
class="pagination__item"
|
||||
aria-label="Previous page"
|
||||
aria-label={ui.prev_page}
|
||||
>←</a>
|
||||
)}
|
||||
|
||||
@@ -122,7 +126,7 @@ const breadcrumb_schema = {
|
||||
<a
|
||||
href={`/category/${slug}?page=${page + 1}`}
|
||||
class="pagination__item"
|
||||
aria-label="Next page"
|
||||
aria-label={ui.next_page}
|
||||
>→</a>
|
||||
)}
|
||||
</nav>
|
||||
|
||||
@@ -51,7 +51,7 @@ const website_schema = {
|
||||
schema={website_schema}
|
||||
>
|
||||
<TrendingBar articles={homepage.trending} />
|
||||
<HeroSection articles={homepage.hero} />
|
||||
<HeroSection articles={homepage.hero} locale={config.locale} />
|
||||
|
||||
{config.ads_enabled && (
|
||||
<div class="container" style="padding-block: 1rem">
|
||||
@@ -62,14 +62,15 @@ const website_schema = {
|
||||
<LatestGrid
|
||||
theme={config.theme}
|
||||
articles={homepage.latest}
|
||||
title="Latest News"
|
||||
show_more_href="/category/all"
|
||||
locale={config.locale}
|
||||
/>
|
||||
|
||||
{config.categories.filter(cat => homepage.by_category[cat.slug]?.length).map(cat => (
|
||||
<CategorySection
|
||||
category={cat}
|
||||
articles={homepage.by_category[cat.slug] ?? []}
|
||||
locale={config.locale}
|
||||
/>
|
||||
))}
|
||||
</Base>
|
||||
@@ -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';
|
||||
<div class="inst-card__thumb inst-card__thumb--empty" aria-hidden="true" />
|
||||
)}
|
||||
{article.is_featured && (
|
||||
<span class="inst-card__ribbon">Featured</span>
|
||||
<span class="inst-card__ribbon">{ui.featured}</span>
|
||||
)}
|
||||
</a>
|
||||
|
||||
@@ -44,7 +47,7 @@ const is_small = size === 'sm';
|
||||
</h3>
|
||||
|
||||
<time class="inst-card__date" datetime={article.published_at}>
|
||||
{relativeTime(article.published_at)}
|
||||
{relativeTime(article.published_at, locale)}
|
||||
</time>
|
||||
|
||||
{show_excerpt && !is_small && (
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
---
|
||||
import type { ArticleListItem, CategoryConfig } from '../../types/index.js';
|
||||
import { formatDateShort } from '../../lib/format.js';
|
||||
import { getUiStrings } from '../../lib/ui-i18n.js';
|
||||
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;
|
||||
const ui = getUiStrings(locale);
|
||||
---
|
||||
{articles.length > 0 && (
|
||||
<section class="inst-cat" aria-labelledby={`inst-cat-${category.slug}`}>
|
||||
@@ -16,13 +19,13 @@ const { category, articles } = Astro.props;
|
||||
<header class="inst-cat__head">
|
||||
<h2 class="inst-cat__title" id={`inst-cat-${category.slug}`}>{category.label}</h2>
|
||||
<a href={`/category/${category.slug}`} class="inst-cat__more">
|
||||
View all
|
||||
{ui.view_all}
|
||||
</a>
|
||||
</header>
|
||||
|
||||
<div class="inst-cat__layout">
|
||||
<div class="inst-cat__lead">
|
||||
<ArticleCard article={articles[0]!} size="lg" show_excerpt={true} />
|
||||
<ArticleCard article={articles[0]!} size="lg" show_excerpt={true} locale={locale} />
|
||||
</div>
|
||||
<ul class="inst-cat__list" role="list">
|
||||
{articles.slice(1, 5).map(article => (
|
||||
@@ -50,7 +53,7 @@ const { category, articles } = Astro.props;
|
||||
<a href={`/article/${article.slug}`}>{article.title}</a>
|
||||
</h3>
|
||||
<time class="inst-cat__row-date" datetime={article.published_at}>
|
||||
{formatDateShort(article.published_at)}
|
||||
{formatDateShort(article.published_at, locale)}
|
||||
</time>
|
||||
</div>
|
||||
</li>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
---
|
||||
import type { ArticleListItem } from '../../types/index.js';
|
||||
import { relativeTime } from '../../lib/format.js';
|
||||
import { getUiStrings } from '../../lib/ui-i18n.js';
|
||||
|
||||
interface Props {
|
||||
articles: ArticleListItem[];
|
||||
locale?: string;
|
||||
}
|
||||
const { articles } = Astro.props;
|
||||
const { articles, locale = 'en-US' } = Astro.props;
|
||||
const ui = getUiStrings(locale);
|
||||
const [main, ...rest] = articles;
|
||||
const secondary = rest.slice(0, 4);
|
||||
---
|
||||
@@ -43,9 +46,9 @@ const secondary = rest.slice(0, 4);
|
||||
<div class="inst-hero__featured-meta">
|
||||
<span>{main.author.name}</span>
|
||||
<span aria-hidden="true">·</span>
|
||||
<time datetime={main.published_at}>{relativeTime(main.published_at)}</time>
|
||||
<time datetime={main.published_at}>{relativeTime(main.published_at, locale)}</time>
|
||||
<span aria-hidden="true">·</span>
|
||||
<span>{main.reading_time} min read</span>
|
||||
<span>{main.reading_time} {ui.min_read}</span>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
@@ -77,7 +80,7 @@ const secondary = rest.slice(0, 4);
|
||||
<a href={`/article/${article.slug}`}>{article.title}</a>
|
||||
</h2>
|
||||
<time class="inst-hero__card-date" datetime={article.published_at}>
|
||||
{relativeTime(article.published_at)}
|
||||
{relativeTime(article.published_at, locale)}
|
||||
</time>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
@@ -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';
|
||||
<span class="article-card__author">{article.author.name}</span>
|
||||
<span class="article-card__sep" aria-hidden="true">·</span>
|
||||
<time datetime={article.published_at} class="article-card__time">
|
||||
{relativeTime(article.published_at)}
|
||||
{relativeTime(article.published_at, locale)}
|
||||
</time>
|
||||
<span class="article-card__sep" aria-hidden="true">·</span>
|
||||
<span class="article-card__read">{article.reading_time} min read</span>
|
||||
|
||||
@@ -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 && (
|
||||
<section class="cat-section" aria-labelledby={`cat-${category.slug}`}>
|
||||
@@ -20,12 +21,12 @@ const { category, articles } = Astro.props;
|
||||
<div class="cat-grid">
|
||||
<!-- Lead article -->
|
||||
<div class="cat-grid__lead">
|
||||
<ArticleCard article={articles[0]!} size="lg" show_excerpt={true} />
|
||||
<ArticleCard article={articles[0]!} size="lg" show_excerpt={true} locale={locale} />
|
||||
</div>
|
||||
<!-- Sidebar articles -->
|
||||
<div class="cat-grid__side">
|
||||
{articles.slice(1, 4).map(article => (
|
||||
<ArticleCard article={article} size="sm" show_excerpt={false} />
|
||||
<ArticleCard article={article} size="sm" show_excerpt={false} locale={locale} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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;
|
||||
<div class="hero-main__meta">
|
||||
<span>{main.author.name}</span>
|
||||
<span aria-hidden="true">·</span>
|
||||
<time datetime={main.published_at}>{relativeTime(main.published_at)}</time>
|
||||
<time datetime={main.published_at}>{relativeTime(main.published_at, locale)}</time>
|
||||
<span aria-hidden="true">·</span>
|
||||
<span>{main.reading_time} min</span>
|
||||
</div>
|
||||
@@ -78,7 +79,7 @@ const [main, ...secondary] = articles;
|
||||
<a href={`/article/${article.slug}`}>{article.title}</a>
|
||||
</h3>
|
||||
<div class="hero-secondary__meta text-xs text-muted">
|
||||
<time datetime={article.published_at}>{relativeTime(article.published_at)}</time>
|
||||
<time datetime={article.published_at}>{relativeTime(article.published_at, locale)}</time>
|
||||
<span aria-hidden="true">·</span>
|
||||
<span>{article.reading_time} min</span>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user