Pārlūkot izejas kodu

fix: robust deep merge for admin translations to avoid root key collision

unknown 21 stundas atpakaļ
vecāks
revīzija
cf9522e438
1 mainītis faili ar 29 papildinājumiem un 12 dzēšanām
  1. 29 12
      src/i18n.ts

+ 29 - 12
src/i18n.ts

@@ -18,33 +18,50 @@ const i18n = createI18n({
   }
 });
 
-// Helper for deep merging locale objects
+// Robust deep merge for locale objects
 const deepMerge = (target: any, source: any) => {
-  for (const key in source) {
-    if (source[key] instanceof Object && key in target) {
-      deepMerge(target[key], source[key]);
+  if (!source) return target;
+  
+  Object.keys(source).forEach(key => {
+    const sVal = source[key];
+    const tVal = target[key];
+    
+    if (sVal !== null && typeof sVal === 'object' && !Array.isArray(sVal)) {
+      if (!tVal || typeof tVal !== 'object') {
+        target[key] = {};
+      }
+      deepMerge(target[key], sVal);
     } else {
-      target[key] = source[key];
+      target[key] = sVal;
     }
-  }
+  });
+  return target;
 };
 
 export const loadAdminTranslations = async (lang?: string) => {
   const targetLang = lang || (i18n.global as any).locale.value;
+  console.log(`[i18n] Loading admin translations for: ${targetLang}`);
   try {
-    // Dynamic import to keep admin translations in a separate chunk
-    const messages = await import(`./locales/${targetLang}.admin.json`);
+    const module = await import(`./locales/${targetLang}.admin.json`);
+    let newMessages = module.default || module;
     
-    // Use manual deep merge instead of i18n.global.mergeLocaleMessage
-    // because mergeLocaleMessage might be shallow at the root level in some versions/configs
+    // Ensure we are working with the admin sub-key if it exists at root,
+    // otherwise wrap it if it seems to be just the content
+    if (!newMessages.admin && (newMessages.tabs || newMessages.actions || newMessages.managementCenter)) {
+       newMessages = { admin: newMessages };
+    }
+
     const currentMessages = i18n.global.getLocaleMessage(targetLang);
-    const newMessages = messages.default;
     
+    // Perform merge directly on the current messages object
     deepMerge(currentMessages, newMessages);
+    
+    // Re-set to trigger reactivity
     i18n.global.setLocaleMessage(targetLang, currentMessages);
     
+    console.log(`[i18n] Admin translations successfully merged for: ${targetLang}`);
   } catch (error) {
-    console.error(`Failed to load admin translations for ${targetLang}`, error);
+    console.error(`[i18n] Failed to load admin translations for ${targetLang}`, error);
   }
 };