Merge pull request #391 from TW199501/refactor/i18n-rename-safet
refactor(i18n): rename safeT to t, drop export alias
This commit is contained in:
@@ -169,7 +169,7 @@ async function initI18n() {
|
|||||||
await loadTranslations('en');
|
await loadTranslations('en');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark loaded BEFORE translatePage so safeT resolves properly
|
// Mark loaded BEFORE translatePage so t() resolves properly
|
||||||
translationsLoaded = true;
|
translationsLoaded = true;
|
||||||
|
|
||||||
// Translate the page
|
// Translate the page
|
||||||
@@ -191,9 +191,7 @@ function translatePage() {
|
|||||||
* @param {Element|Document} root - The root element to search within
|
* @param {Element|Document} root - The root element to search within
|
||||||
*/
|
*/
|
||||||
function translateElement(root) {
|
function translateElement(root) {
|
||||||
// Use safeT instead of bare t() to avoid issues when other scripts
|
const resolve = t;
|
||||||
// (e.g. admin.js) shadow the global t() function.
|
|
||||||
const resolve = safeT;
|
|
||||||
const el = root || document;
|
const el = root || document;
|
||||||
el.querySelectorAll('[data-i18n]').forEach((element) => {
|
el.querySelectorAll('[data-i18n]').forEach((element) => {
|
||||||
const key = element.getAttribute('data-i18n');
|
const key = element.getAttribute('data-i18n');
|
||||||
@@ -242,14 +240,12 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||||||
window.dispatchEvent(new Event('translationsLoaded'));
|
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} key
|
||||||
* @param {string | Record<string, any>} [paramsOrFallback] - interpolation params object, or a string fallback used when the key is missing
|
* @param {string | Record<string, any>} [paramsOrFallback] - interpolation params object, or a string fallback used when the key is missing
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function safeT(key, paramsOrFallback = {}) {
|
function t(key, paramsOrFallback = {}) {
|
||||||
const fallback = typeof paramsOrFallback === 'string' ? paramsOrFallback : null;
|
const fallback = typeof paramsOrFallback === 'string' ? paramsOrFallback : null;
|
||||||
const params = typeof paramsOrFallback === 'object' ? paramsOrFallback : {};
|
const params = typeof paramsOrFallback === 'object' ? paramsOrFallback : {};
|
||||||
|
|
||||||
@@ -271,7 +267,7 @@ function safeT(key, paramsOrFallback = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const i18n = {
|
export const i18n = {
|
||||||
t: safeT,
|
t,
|
||||||
setLocale,
|
setLocale,
|
||||||
getCurrentLocale,
|
getCurrentLocale,
|
||||||
getSupportedLocales,
|
getSupportedLocales,
|
||||||
|
|||||||
Reference in New Issue
Block a user