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
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
export function formatDate(iso: string, locale = 'en-US'): string {
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return iso;
|
|
return new Intl.DateTimeFormat(locale, {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
}).format(d);
|
|
}
|
|
|
|
export function formatDateShort(iso: string, locale = 'en-US'): string {
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return iso;
|
|
return new Intl.DateTimeFormat(locale, {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
}).format(d);
|
|
}
|
|
|
|
export function relativeTime(iso: string, locale = 'en-US'): string {
|
|
const parsed = new Date(iso);
|
|
if (isNaN(parsed.getTime())) return iso;
|
|
const now = Date.now();
|
|
const diffSec = (now - parsed.getTime()) / 1000;
|
|
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });
|
|
|
|
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 {
|
|
if (str.length <= max) return str;
|
|
return str.slice(0, max).trimEnd() + '…';
|
|
}
|
|
|
|
export function slugToTitle(slug: string): string {
|
|
return slug.replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
|
|
}
|