@maz-ui/translations 4.7.6 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{globalState,locale}from"../states.js";function getMessage(obj,path){return path.split(`.`).reduce((current,key)=>current?.[key],obj)}function setMessage(obj,path,value){let keys=path.split(`.`);let lastKey=keys.pop();let target=keys.reduce((current,key)=>((!current[key]||typeof current[key]!=`object`)&&(current[key]={}),current[key]),obj);target[lastKey]=value}function isFlattenedObject(obj){return!obj||typeof obj!=`object`?!1:Object.keys(obj).some(key=>key.includes(`.`))}function flattenToNested(flatObj){if(!isFlattenedObject(flatObj))return flatObj;let nested={};for(let[key,value]of Object.entries(flatObj))key.includes(`.`)?setMessage(nested,key,value):nested[key]=value;return nested}function mergeMessages(target,source){let normalizedSource=flattenToNested(source);let result={...flattenToNested(target)};for(let key in normalizedSource)normalizedSource[key]&&typeof normalizedSource[key]==`object`?result[key]=mergeMessages(result[key]||{},normalizedSource[key]):result[key]=normalizedSource[key];return result}function interpolate(message,variables){return variables?message.replace(/\{(\w+)\}/g,(match,key)=>variables[key]===void 0?match:String(variables[key])):message}function t(key,variables,fallbackLocale){let message=getMessage(globalState.messages[locale.value],key);if(!message&&fallbackLocale&&locale.value!==fallbackLocale&&(globalState.loadedLocales.has(fallbackLocale)||loadLocale(fallbackLocale).catch(error=>console.error(`[@maz-ui/translations] Failed to load fallback locale: "${fallbackLocale}"`,error)),message=getMessage(globalState.messages[fallbackLocale],key)),!message&&fallbackLocale!==`en`&&locale.value!==`en`){if(!globalState.loadedLocales.has(`en`))return loadLocale(`en`).catch(error=>console.error(`[@maz-ui/translations] Failed to load en locale: "en"`,error)),key;message=getMessage(globalState.messages.en,key)}return message?interpolate(String(message),variables):(console.warn(`[@maz-ui/translations] Translation not found for key: "${key}"`),key)}var defaultLocalesLoaders={"./locales/en.ts":()=>import(`../locales/en.js`).then(m=>({default:m.default})),"./locales/de.ts":()=>import(`../locales/de.js`).then(m=>({default:m.default})),"./locales/es.ts":()=>import(`../locales/es.js`).then(m=>({default:m.default})),"./locales/fr.ts":()=>import(`../locales/fr.js`).then(m=>({default:m.default})),"./locales/it.ts":()=>import(`../locales/it.js`).then(m=>({default:m.default})),"./locales/ja.ts":()=>import(`../locales/ja.js`).then(m=>({default:m.default})),"./locales/pt.ts":()=>import(`../locales/pt.js`).then(m=>({default:m.default})),"./locales/zh-CN.ts":()=>import(`../locales/zh-CN.js`).then(m=>({default:m.default}))};var defaultMessagesCache=new Map;async function loadDefaultMessages(locale){if(defaultMessagesCache.has(locale))return defaultMessagesCache.get(locale);let loader=defaultLocalesLoaders[`./locales/${locale}.ts`];if(!loader)return{};try{let messages=(await loader()).default;return defaultMessagesCache.set(locale,messages),messages}catch(error){return console.error(`Failed to load default messages for locale "${locale}":`,error),{}}}function getAvailableLocales(){let locales=new Set;for(let[path,loader]of Object.entries(defaultLocalesLoaders))loader&&typeof loader==`function`&&locales.add(path.replace(`./locales/`,``).replace(`.ts`,``));for(let path of Object.keys(globalState.userMessages))locales.add(path);return
|
|
1
|
+
import{globalState,locale}from"../states.js";function getMessage(obj,path){return path.split(`.`).reduce((current,key)=>current?.[key],obj)}function setMessage(obj,path,value){let keys=path.split(`.`);let lastKey=keys.pop();let target=keys.reduce((current,key)=>((!current[key]||typeof current[key]!=`object`)&&(current[key]={}),current[key]),obj);target[lastKey]=value}function isFlattenedObject(obj){return!obj||typeof obj!=`object`?!1:Object.keys(obj).some(key=>key.includes(`.`))}function flattenToNested(flatObj){if(!isFlattenedObject(flatObj))return flatObj;let nested={};for(let[key,value]of Object.entries(flatObj))key.includes(`.`)?setMessage(nested,key,value):nested[key]=value;return nested}function mergeMessages(target,source){let normalizedSource=flattenToNested(source);let result={...flattenToNested(target)};for(let key in normalizedSource)normalizedSource[key]&&typeof normalizedSource[key]==`object`?result[key]=mergeMessages(result[key]||{},normalizedSource[key]):result[key]=normalizedSource[key];return result}function interpolate(message,variables){return variables?message.replace(/\{(\w+)\}/g,(match,key)=>variables[key]===void 0?match:String(variables[key])):message}function t(key,variables,fallbackLocale){let message=getMessage(globalState.messages[locale.value],key);if(!message&&fallbackLocale&&locale.value!==fallbackLocale&&(globalState.loadedLocales.has(fallbackLocale)||loadLocale(fallbackLocale).catch(error=>console.error(`[@maz-ui/translations] Failed to load fallback locale: "${fallbackLocale}"`,error)),message=getMessage(globalState.messages[fallbackLocale],key)),!message&&fallbackLocale!==`en`&&locale.value!==`en`){if(!globalState.loadedLocales.has(`en`))return loadLocale(`en`).catch(error=>console.error(`[@maz-ui/translations] Failed to load en locale: "en"`,error)),key;message=getMessage(globalState.messages.en,key)}return message?interpolate(String(message),variables):(console.warn(`[@maz-ui/translations] Translation not found for key: "${key}"`),key)}var defaultLocalesLoaders={"./locales/en.ts":()=>import(`../locales/en.js`).then(m=>({default:m.default})),"./locales/de.ts":()=>import(`../locales/de.js`).then(m=>({default:m.default})),"./locales/es.ts":()=>import(`../locales/es.js`).then(m=>({default:m.default})),"./locales/fr.ts":()=>import(`../locales/fr.js`).then(m=>({default:m.default})),"./locales/it.ts":()=>import(`../locales/it.js`).then(m=>({default:m.default})),"./locales/ja.ts":()=>import(`../locales/ja.js`).then(m=>({default:m.default})),"./locales/pt.ts":()=>import(`../locales/pt.js`).then(m=>({default:m.default})),"./locales/zh-CN.ts":()=>import(`../locales/zh-CN.js`).then(m=>({default:m.default}))};var defaultMessagesCache=new Map;async function loadDefaultMessages(locale){if(defaultMessagesCache.has(locale))return defaultMessagesCache.get(locale);let loader=defaultLocalesLoaders[`./locales/${locale}.ts`];if(!loader)return{};try{let messages=(await loader()).default;return defaultMessagesCache.set(locale,messages),messages}catch(error){return console.error(`Failed to load default messages for locale "${locale}":`,error),{}}}function getAvailableLocales(){let locales=new Set;for(let[path,loader]of Object.entries(defaultLocalesLoaders))loader&&typeof loader==`function`&&locales.add(path.replace(`./locales/`,``).replace(`.ts`,``));for(let path of Object.keys(globalState.userMessages))locales.add(path);return[...locales]}function loadLocale(targetLocale){if(globalState.loadedLocales.has(targetLocale))return Promise.resolve();if(globalState.loadingPromises.has(targetLocale))return globalState.loadingPromises.get(targetLocale);let loadingPromise=(async()=>{try{let localeDefaultMessages=await loadDefaultMessages(targetLocale);let localeUserMessages={};let userLoader=globalState.userMessages[targetLocale];if(userLoader)if(typeof userLoader==`function`)try{let userMessages=await userLoader();localeUserMessages=`default`in userMessages?userMessages.default:userMessages}catch(error){console.error(`Failed to load user translations for locale "${targetLocale}":`,error),localeUserMessages={}}else localeUserMessages=userLoader;globalState.messages[targetLocale]=mergeMessages(localeDefaultMessages,localeUserMessages),globalState.loadedLocales.add(targetLocale)}catch(error){console.error(`Failed to load translations for locale "${targetLocale}":`,error),globalState.messages[targetLocale]={},globalState.loadedLocales.add(targetLocale)}finally{globalState.loadingPromises.delete(targetLocale)}})();return globalState.loadingPromises.set(targetLocale,loadingPromise),loadingPromise}function getMessages(){return globalState.messages}function isLocaleLoaded(localeToCheck){return globalState.loadedLocales.has(localeToCheck)}function isLocaleLoading(localeToCheck){return globalState.loadingPromises.has(localeToCheck)}function setLocaleMessage(targetLocale,messages){globalState.messages[targetLocale]||(globalState.messages[targetLocale]={}),globalState.messages[targetLocale]=mergeMessages(globalState.messages[targetLocale],messages),globalState.loadedLocales.add(targetLocale)}async function setLocale(newLocale){globalState.loadedLocales.has(newLocale)||await loadLocale(newLocale),locale.value=newLocale}function getLoadedLocales(){return Object.keys(globalState.messages)}export{isLocaleLoading as a,setLocale as c,interpolate as d,mergeMessages as f,isLocaleLoaded as i,setLocaleMessage as l,getLoadedLocales as n,loadDefaultMessages as o,t as p,getMessages as r,loadLocale as s,getAvailableLocales as t,getMessage as u};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{globalState,locale}from"./states.js";import{injectTranslations}from"./utils/inject.js";import{a as isLocaleLoading,c as setLocale,d as interpolate,f as mergeMessages,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales,u as getMessage}from"./chunks/locales.
|
|
1
|
+
import{globalState,locale}from"./states.js";import{injectTranslations}from"./utils/inject.js";import{a as isLocaleLoading,c as setLocale,d as interpolate,f as mergeMessages,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales,u as getMessage}from"./chunks/locales.DAcmIbE5.js";import{createMazUiTranslations}from"./utils/instance.js";import{MazUiTranslations}from"./plugin.js";import zh_CN_default from"./locales/zh-CN.js";import pt_default from"./locales/pt.js";import ja_default from"./locales/ja.js";import it_default from"./locales/it.js";import fr_default from"./locales/fr.js";import es_default from"./locales/es.js";import en_default from"./locales/en.js";import de_default from"./locales/de.js";import{useTranslations}from"./composables/useTranslations.js";import"./locales/index.js";import"./utils/index.js";export{MazUiTranslations,createMazUiTranslations,de_default as de,en_default as en,es_default as es,fr_default as fr,getAvailableLocales,getLoadedLocales,getMessage,getMessages,globalState,injectTranslations,interpolate,isLocaleLoaded,isLocaleLoading,it_default as it,ja_default as ja,loadDefaultMessages,loadLocale,locale,mergeMessages,pt_default as pt,setLocale,setLocaleMessage,t,useTranslations,zh_CN_default as zhCN};
|
package/dist/utils/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{injectTranslations}from"./inject.js";import{a as isLocaleLoading,c as setLocale,d as interpolate,f as mergeMessages,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales,u as getMessage}from"../chunks/locales.
|
|
1
|
+
import{injectTranslations}from"./inject.js";import{a as isLocaleLoading,c as setLocale,d as interpolate,f as mergeMessages,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales,u as getMessage}from"../chunks/locales.DAcmIbE5.js";import{createMazUiTranslations}from"./instance.js";export{createMazUiTranslations,getAvailableLocales,getLoadedLocales,getMessage,getMessages,injectTranslations,interpolate,isLocaleLoaded,isLocaleLoading,loadDefaultMessages,loadLocale,mergeMessages,setLocale,setLocaleMessage,t};
|
package/dist/utils/instance.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{globalState,locale}from"../states.js";import{a as isLocaleLoading,c as setLocale,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales}from"../chunks/locales.
|
|
1
|
+
import{globalState,locale}from"../states.js";import{a as isLocaleLoading,c as setLocale,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,p as t,r as getMessages,s as loadLocale,t as getAvailableLocales}from"../chunks/locales.DAcmIbE5.js";function createMazUiTranslations(options={}){let{locale:initialLocale=`en`,fallbackLocale=`en`,preloadFallback=!0,messages={en:{}}}=options;locale.value=initialLocale;for(let[loc,msgs]of Object.entries(messages))globalState.userMessages[loc]=msgs,msgs&&typeof msgs==`object`&&(globalState.messages[loc]={...globalState.messages[loc],...msgs});return setTimeout(()=>{loadLocale(initialLocale).catch(error=>console.error(`[@maz-ui/translations] Failed to load locale: "${initialLocale}"`,error)),preloadFallback&&fallbackLocale!==initialLocale&&loadLocale(fallbackLocale).catch(error=>console.error(`[@maz-ui/translations] Failed to load fallback locale: "${fallbackLocale}"`,error))},0),{locale,t:(key,variables)=>t(key,variables,fallbackLocale),setLocale,isLocaleLoaded,isLocaleLoading,setLocaleMessage,getMessages,getLoadedLocales,getAvailableLocales}}export{createMazUiTranslations};
|
package/dist/utils/locales.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as isLocaleLoading,c as setLocale,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,r as getMessages,s as loadLocale,t as getAvailableLocales}from"../chunks/locales.
|
|
1
|
+
import{a as isLocaleLoading,c as setLocale,i as isLocaleLoaded,l as setLocaleMessage,n as getLoadedLocales,o as loadDefaultMessages,r as getMessages,s as loadLocale,t as getAvailableLocales}from"../chunks/locales.DAcmIbE5.js";export{getAvailableLocales,getLoadedLocales,getMessages,isLocaleLoaded,isLocaleLoading,loadDefaultMessages,loadLocale,setLocale,setLocaleMessage};
|
package/dist/utils/messages.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{d as interpolate,f as mergeMessages,p as t,u as getMessage}from"../chunks/locales.
|
|
1
|
+
import{d as interpolate,f as mergeMessages,p as t,u as getMessage}from"../chunks/locales.DAcmIbE5.js";export{getMessage,interpolate,mergeMessages,t};
|