Foro: nombres de categorías y foros según el locale (ES/EN)
Los nombres de categorías, foros, descripciones, clases e idiomas venían del seed solo en inglés. Ahora se muestran en el idioma de la página. Como es un conjunto fijo y conocido, se resuelve con un mapa de traducción en código (lib/forum-i18n.ts, inglés→español) que se aplica al vuelo, sin tocar el esquema (el inglés sigue siendo el valor guardado). Un texto no mapeado (p. ej. un foro nuevo creado desde el admin) se muestra tal cual. getForumIndex/getForum/getForumPath/getTopicPath aceptan el locale y localizan name/description/category (nunca el título de un tema, que es contenido de usuario). Las páginas pasan su locale. Verificado en producción: /es/forum en español (Noticias, Caballero de la Muerte, Foros por idioma) y /en/forum en inglés. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+22
-14
@@ -1,5 +1,6 @@
|
||||
import type { RowDataPacket } from 'mysql2'
|
||||
import { db, DB } from './db'
|
||||
import { localizeForum } from './forum-i18n'
|
||||
|
||||
// Foro estilo FusionCMS (ver sql/forum_fusion.sql). Esquema:
|
||||
// forum_categories(id, order, name)
|
||||
@@ -101,7 +102,7 @@ function avatarForClass(classId: number | undefined): string {
|
||||
const STAFF_GMLEVEL = Number(process.env.FORUM_MOD_GMLEVEL || 2)
|
||||
|
||||
/** Índice del foro: categorías ordenadas, cada una con sus foros y agregados. */
|
||||
export async function getForumIndex(): Promise<Category[]> {
|
||||
export async function getForumIndex(locale = 'es'): Promise<Category[]> {
|
||||
try {
|
||||
const [cats] = await db(DB.web).query<RowDataPacket[]>(
|
||||
'SELECT id, name FROM forum_categories ORDER BY `order`, name',
|
||||
@@ -132,8 +133,8 @@ export async function getForumIndex(): Promise<Category[]> {
|
||||
id: f.id,
|
||||
category: f.category,
|
||||
order: f.order,
|
||||
name: f.name,
|
||||
description: f.description,
|
||||
name: localizeForum(f.name, locale),
|
||||
description: localizeForum(f.description, locale),
|
||||
icon: f.icon,
|
||||
colortitle: f.colortitle,
|
||||
type: f.type,
|
||||
@@ -148,14 +149,14 @@ export async function getForumIndex(): Promise<Category[]> {
|
||||
byCat.get(row.category)!.push(row)
|
||||
}
|
||||
return cats
|
||||
.map((c) => ({ id: c.id, name: c.name, forums: byCat.get(c.id) ?? [] }))
|
||||
.map((c) => ({ id: c.id, name: localizeForum(c.name, locale), forums: byCat.get(c.id) ?? [] }))
|
||||
.filter((c) => c.forums.length > 0)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export async function getForum(id: number): Promise<ForumMeta | null> {
|
||||
export async function getForum(id: number, locale = 'es'): Promise<ForumMeta | null> {
|
||||
try {
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
'SELECT id, name, description, icon, colortitle, type FROM forum_forums WHERE id = ?',
|
||||
@@ -163,7 +164,14 @@ export async function getForum(id: number): Promise<ForumMeta | null> {
|
||||
)
|
||||
const r = rows[0]
|
||||
return r
|
||||
? { id: r.id, name: r.name, description: r.description, icon: r.icon, colortitle: r.colortitle, type: r.type }
|
||||
? {
|
||||
id: r.id,
|
||||
name: localizeForum(r.name, locale),
|
||||
description: localizeForum(r.description, locale),
|
||||
icon: r.icon,
|
||||
colortitle: r.colortitle,
|
||||
type: r.type,
|
||||
}
|
||||
: null
|
||||
} catch {
|
||||
return null
|
||||
@@ -180,7 +188,7 @@ export async function forumExists(id: number): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getForumPath(forumId: number): Promise<ForumPath | null> {
|
||||
export async function getForumPath(forumId: number, locale = 'es'): Promise<ForumPath | null> {
|
||||
try {
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
`SELECT c.id AS category_id, c.name AS category, f.id AS forum_id, f.name AS forum, f.description AS forum_description
|
||||
@@ -192,10 +200,10 @@ export async function getForumPath(forumId: number): Promise<ForumPath | null> {
|
||||
return r
|
||||
? {
|
||||
category_id: r.category_id,
|
||||
category: r.category,
|
||||
category: localizeForum(r.category, locale),
|
||||
forum_id: r.forum_id,
|
||||
forum: r.forum,
|
||||
forum_description: r.forum_description,
|
||||
forum: localizeForum(r.forum, locale),
|
||||
forum_description: localizeForum(r.forum_description, locale),
|
||||
}
|
||||
: null
|
||||
} catch {
|
||||
@@ -203,7 +211,7 @@ export async function getForumPath(forumId: number): Promise<ForumPath | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTopicPath(topicId: number): Promise<TopicPath | null> {
|
||||
export async function getTopicPath(topicId: number, locale = 'es'): Promise<TopicPath | null> {
|
||||
try {
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
`SELECT c.id AS category_id, c.name AS category, f.id AS forum_id, f.name AS forum,
|
||||
@@ -218,10 +226,10 @@ export async function getTopicPath(topicId: number): Promise<TopicPath | null> {
|
||||
return r
|
||||
? {
|
||||
category_id: r.category_id,
|
||||
category: r.category,
|
||||
category: localizeForum(r.category, locale),
|
||||
forum_id: r.forum_id,
|
||||
forum: r.forum,
|
||||
forum_description: r.forum_description,
|
||||
forum: localizeForum(r.forum, locale),
|
||||
forum_description: localizeForum(r.forum_description, locale),
|
||||
topic_id: r.topic_id,
|
||||
topic: r.topic,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user