From 32ad2a4f0e8f5e2c1ea2403baf7f246f18484fe8 Mon Sep 17 00:00:00 2001 From: Eddie Yang Date: Sun, 24 May 2026 21:17:58 +0800 Subject: [PATCH] refactor(i18n): rename safeT to t, drop export alias Follow-up to PR #373 review feedback (thanks @EdouardVanbelle): with the local t() removed in that PR, the safeT defensive name no longer earns its purpose. There is no global t() left to shadow it, so the wrapper- style name just creates confusion against callers writing i18n.t(). - function safeT -> function t - export { t: safeT, ... } -> { t, ... } - Remove stale comments in translateElement and above the definition that explained the safeT/admin.js shadowing history No call-site changes - every external caller already uses i18n.t(...), which now points directly at the function of the same name. Co-Authored-By: Claude Opus 4.7 (1M context) --- static/js/core/i18n.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/static/js/core/i18n.js b/static/js/core/i18n.js index 108a838f..3822a4c4 100644 --- a/static/js/core/i18n.js +++ b/static/js/core/i18n.js @@ -169,7 +169,7 @@ async function initI18n() { await loadTranslations('en'); } - // Mark loaded BEFORE translatePage so safeT resolves properly + // Mark loaded BEFORE translatePage so t() resolves properly translationsLoaded = true; // Translate the page @@ -191,9 +191,7 @@ function translatePage() { * @param {Element|Document} root - The root element to search within */ function translateElement(root) { - // Use safeT instead of bare t() to avoid issues when other scripts - // (e.g. admin.js) shadow the global t() function. - const resolve = safeT; + const resolve = t; const el = root || document; el.querySelectorAll('[data-i18n]').forEach((element) => { const key = element.getAttribute('data-i18n'); @@ -242,14 +240,12 @@ document.addEventListener('DOMContentLoaded', async () => { window.dispatchEvent(new Event('translationsLoaded')); }); -// Self-contained t wrapper — does NOT call the global t() because other -// scripts (e.g. admin.js) may shadow it, which would cause infinite recursion. /** * @param {string} key * @param {string | Record} [paramsOrFallback] - interpolation params object, or a string fallback used when the key is missing * @returns {string} */ -function safeT(key, paramsOrFallback = {}) { +function t(key, paramsOrFallback = {}) { const fallback = typeof paramsOrFallback === 'string' ? paramsOrFallback : null; const params = typeof paramsOrFallback === 'object' ? paramsOrFallback : {}; @@ -271,7 +267,7 @@ function safeT(key, paramsOrFallback = {}) { } export const i18n = { - t: safeT, + t, setLocale, getCurrentLocale, getSupportedLocales,