Files
planet/frontend/public/earth/js/news-locale.js
linkong 3265d22af5
Some checks failed
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.71.1
2026-06-26 17:34:19 +08:00

193 lines
6.2 KiB
JavaScript

const DEFAULT_LOCALE = "zh-CN";
const REGION_LABELS = {
americas: "美洲",
europe: "欧洲",
"middle-east-africa": "中东与非洲",
"asia-pacific": "亚太",
global: "全球",
};
const FEED_LABELS = {
"Global Monitor / World": "区域监测",
"Global Monitor / Americas": "区域监测",
"Global Monitor / Europe": "区域监测",
"Global Monitor / MEA": "区域监测",
"Global Monitor / APAC": "区域监测",
};
const CATEGORY_LABELS = {
politics: "政治",
business: "商业",
ecommerce: "电商",
finance: "金融",
sports: "体育",
technology: "科技",
military: "军事",
disaster: "灾害",
energy: "能源",
society: "社会",
culture: "文化",
other: "其他",
};
const BREAKING_LEVEL_LABELS = {
watch: "关注",
breaking: "突发",
critical: "严重突发",
};
const BREAKING_SCOPE_LABELS = {
regional: "区域",
global: "全球",
};
const SOURCE_TYPE_LABELS = {
rss: "RSS",
atom: "Atom",
aggregated: "Aggregated",
manual: "手动添加",
reference: "Reference",
};
const LOCATION_SOURCE_LABELS = {
region_anchor: "区域锚点",
ai_inferred_target: "AI 推断位置",
headline_location_hint: "标题位置线索",
headline_country_hint: "标题国家线索",
};
const ENRICHMENT_STATUS_LABELS = {
pending: "待增强",
queued: "增强排队中",
attempted: "增强中",
success: "已汉化",
content_only: "已汉化",
location_only: "位置已增强",
unavailable: "AI 未配置",
provider_error: "增强失败",
parse_error: "增强解析失败",
no_result: "暂无增强结果",
};
const TITLE_PLACEHOLDERS = {
queued: "新闻汉化排队中",
attempted: "新闻汉化中",
provider_error: "新闻汉化失败,正在重试",
parse_error: "新闻解析失败,正在重试",
unavailable: "等待 AI 配置",
no_result: "新闻汉化待重试",
location_only: "新闻汉化待重试",
};
const SUMMARY_PLACEHOLDERS = {
queued: "中文概要正在生成,请稍后刷新。",
attempted: "中文概要正在生成,请稍后刷新。",
provider_error: "中文概要生成失败,系统会重新提交增强任务。",
parse_error: "中文概要解析失败,系统会重新提交增强任务。",
unavailable: "AI 服务配置完成后将生成中文概要。",
no_result: "中文概要暂未生成,系统会继续重试。",
location_only: "已完成位置增强,中文概要将继续重试。",
};
function normalizeText(value) {
return String(value ?? "").replace(/\s+/g, " ").trim();
}
function getLocalization(item, locale = DEFAULT_LOCALE) {
const localizations = item?.localizations;
const localized = localizations && typeof localizations === "object"
? localizations[locale]
: null;
return localized && typeof localized === "object" ? localized : {};
}
export function getNewsDisplayTitle(item, locale = DEFAULT_LOCALE) {
const localized = getLocalization(item, locale).title;
if (localized || item?.display_title) {
return normalizeText(item?.display_title || localized);
}
if (item?.title) {
return normalizeText(item.title);
}
return normalizeText(
TITLE_PLACEHOLDERS[item?.enrichment_status]
|| "新闻汉化中",
);
}
export function getNewsDisplaySummary(item, locale = DEFAULT_LOCALE) {
const localized = getLocalization(item, locale).summary;
if (localized || item?.display_summary) {
return normalizeText(item?.display_summary || localized);
}
if (item?.summary) {
return normalizeText(item.summary);
}
return normalizeText(
SUMMARY_PLACEHOLDERS[item?.enrichment_status]
|| "中文概要生成中,请稍后刷新。",
);
}
export function isNewsContentReady(item, locale = DEFAULT_LOCALE) {
const localized = getLocalization(item, locale);
const title = normalizeText(item?.display_title || localized.title || item?.title);
const summary = normalizeText(item?.display_summary || localized.summary || item?.summary);
return Boolean(title && summary);
}
export function getNewsRegionLabel(region, fallback = "") {
return REGION_LABELS[region] || fallback || region || REGION_LABELS.global;
}
export function getNewsFeedLabel(feedName) {
return FEED_LABELS[feedName] || feedName || "聚合源";
}
export function getNewsCategoryLabel(category) {
return CATEGORY_LABELS[category] || category || CATEGORY_LABELS.other;
}
export function getNewsBreakingLabel(level, scope = "regional") {
const normalizedLevel = normalizeText(level).toLowerCase();
if (!normalizedLevel || normalizedLevel === "none") return "";
const levelLabel = BREAKING_LEVEL_LABELS[normalizedLevel] || level;
const scopeLabel = BREAKING_SCOPE_LABELS[normalizeText(scope).toLowerCase()] || BREAKING_SCOPE_LABELS.regional;
return `${scopeLabel}${levelLabel}`;
}
export function getNewsSourceTypeLabel(sourceType) {
const normalized = normalizeText(sourceType).toLowerCase();
return SOURCE_TYPE_LABELS[normalized] || sourceType || "RSS";
}
export function isRegionalMonitorFeed(feedName) {
return Object.prototype.hasOwnProperty.call(FEED_LABELS, feedName);
}
export function getNewsFetchChannelLabel(feedName, sourceType = "") {
const normalized = normalizeText(sourceType).toLowerCase();
if (isRegionalMonitorFeed(feedName) || normalized === "aggregated") return "区域监测";
if (normalized === "manual") return "手动添加";
if (normalized === "atom") return "单源 Atom";
if (normalized === "reference") return "配置保留";
return "单源 RSS";
}
export function getNewsLocationSourceLabel(source) {
return LOCATION_SOURCE_LABELS[source] || source || "位置来源";
}
export function getNewsEnrichmentStatusLabel(statusOrItem) {
const item = statusOrItem && typeof statusOrItem === "object" ? statusOrItem : null;
const status = item ? item.enrichment_status : statusOrItem;
const language = String(item?.content_language || "").toLowerCase();
if (language.startsWith("zh") && status !== "success" && status !== "content_only") {
if (status === "queued" || status === "attempted") return "英文补译中";
if (status === "provider_error" || status === "parse_error" || status === "no_result") return "中文原文";
return "中文原文";
}
return ENRICHMENT_STATUS_LABELS[status] || status || "增强状态";
}