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) <noreply@anthropic.com>
This commit is contained in:
Eddie Yang
2026-05-24 21:17:58 +08:00
parent daf25b5830
commit 32ad2a4f0e
+4 -8
View File
@@ -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<string, any>} [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,