@stacksjs/i18n 0.70.23 → 0.70.26
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.
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/src/loader.ts +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var i=import.meta.require;function c(q,B){return r(B)(q)}function r(q){let B=q.split("-")[0].toLowerCase(),G=T[B];if(G)return G;return()=>"other"}function C(q,B="|"){let G=q.split(B).map((J)=>J.trim()),K=new Map;if(G.length===1)K.set("other",G[0]);else if(G.length===2)K.set("one",G[0]),K.set("other",G[1]);else if(G.length===3)K.set("zero",G[0]),K.set("one",G[1]),K.set("other",G[2]);else if(G.length>=4){let J=["zero","one","two","few","many","other"];for(let Q=0;Q<G.length&&Q<J.length;Q++)K.set(J[Q],G[Q])}return K}function O(q,B,G){let K=c(B,G);if(q.has(K))return q.get(K);if(q.has("other"))return q.get("other");let J=q.values().next();return J.done?"":J.value}var T={en:(q)=>q===1?"one":"other",de:(q)=>q===1?"one":"other",nl:(q)=>q===1?"one":"other",es:(q)=>q===1?"one":"other",it:(q)=>q===1?"one":"other",pt:(q)=>q===1?"one":"other",sv:(q)=>q===1?"one":"other",da:(q)=>q===1?"one":"other",no:(q)=>q===1?"one":"other",fi:(q)=>q===1?"one":"other",el:(q)=>q===1?"one":"other",he:(q)=>q===1?"one":"other",hu:(q)=>q===1?"one":"other",tr:(q)=>q===1?"one":"other",zh:()=>"other",ja:()=>"other",ko:()=>"other",vi:()=>"other",th:()=>"other",id:()=>"other",ms:()=>"other",fr:(q)=>q===0||q===1?"one":"other",ru:(q)=>{let B=q%10,G=q%100;if(B===1&&G!==11)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B>=5&&B<=9||G>=11&&G<=14)return"many";return"other"},uk:(q)=>{let B=q%10,G=q%100;if(B===1&&G!==11)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B>=5&&B<=9||G>=11&&G<=14)return"many";return"other"},pl:(q)=>{let B=q%10,G=q%100;if(q===1)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B===1||B>=5&&B<=9||G>=12&&G<=14)return"many";return"other"},cs:(q)=>{if(q===1)return"one";if(q>=2&&q<=4)return"few";return"other"},sk:(q)=>{if(q===1)return"one";if(q>=2&&q<=4)return"few";return"other"},ar:(q)=>{if(q===0)return"zero";if(q===1)return"one";if(q===2)return"two";let B=q%100;if(B>=3&&B<=10)return"few";if(B>=11&&B<=99)return"many";return"other"},cy:(q)=>{if(q===0)return"zero";if(q===1)return"one";if(q===2)return"two";if(q===3)return"few";if(q===6)return"many";return"other"},sl:(q)=>{let B=q%100;if(B===1)return"one";if(B===2)return"two";if(B===3||B===4)return"few";return"other"},ga:(q)=>{if(q===1)return"one";if(q===2)return"two";if(q>=3&&q<=6)return"few";if(q>=7&&q<=10)return"many";return"other"},lt:(q)=>{let B=q%10,G=q%100;if(B===1&&(G<11||G>19))return"one";if(B>=2&&B<=9&&(G<11||G>19))return"few";return"other"},lv:(q)=>{let B=q%10,G=q%100;if(q===0)return"zero";if(B===1&&G!==11)return"one";return"other"},ro:(q)=>{if(q===1)return"one";let B=q%100;if(q===0||B>=1&&B<=19)return"few";return"other"},hi:(q)=>q===0||q===1?"one":"other"};function Rq(){return Object.keys(T)}function Tq(q){return q.split("-")[0].toLowerCase()in T}function Fq(q,B){let G=q.split("-")[0].toLowerCase();T[G]=B}var v={locale:"en",fallbackLocale:"en",availableLocales:["en"],messages:{},warnMissing:!0,escapeValues:!1,keySeparator:".",pluralSeparator:"|"},Y="en",z="en",w={},H={...v};class j{_locale;_fallbackLocale;_messages;_config;constructor(q={}){this._config={...v,...q},this._locale=this._config.locale,this._fallbackLocale=this._config.fallbackLocale,this._messages=this._config.messages||{}}get locale(){return this._locale}set locale(q){this._locale=q}get fallbackLocale(){return this._fallbackLocale}get availableLocales(){return Object.keys(this._messages)}t=(q,B,G)=>{return this.translate(q,B,G)};tc=(q,B,G,K)=>{return this.translatePlural(q,B,G,K)};te=(q,B)=>{return this.hasTranslation(q,B)};tm=(q,B)=>{return this.getMessage(q,B)};setLocale=(q)=>{this._locale=q};addTranslations=(q,B)=>{if(!this._messages[q])this._messages[q]={};this._messages[q]=V(this._messages[q],B)};d=(q,B)=>{let G=typeof q==="number"?new Date(q):q,K=this._config.dateTimeFormats?.[this._locale]?.[B||"short"]||{};return new Intl.DateTimeFormat(this._locale,K).format(G)};n=(q,B)=>{let G=this._config.numberFormats?.[this._locale]?.[B||"decimal"]||{};return new Intl.NumberFormat(this._locale,G).format(q)};translate(q,B,G){let K=G||this._locale,J=this.getMessage(q,K);if(J===void 0){let Q=K.indexOf("-");if(Q>0){let X=K.slice(0,Q);if(X!==this._fallbackLocale)J=this.getMessage(q,X)}}if(J===void 0&&K!==this._fallbackLocale)J=this.getMessage(q,this._fallbackLocale);if(J===void 0){if(this._config.warnMissing)console.warn(`[i18n] Missing translation: "${q}" for locale "${K}"`);if(this._config.missingHandler){let Q=this._config.missingHandler(K,q);if(Q!==void 0)return Q}return q}if(typeof J!=="string")return q;return this.interpolate(J,B)}translatePlural(q,B,G,K){let J=K||this._locale,Q=this.getMessage(q,J);if(Q===void 0&&J!==this._fallbackLocale)Q=this.getMessage(q,this._fallbackLocale);if(Q===void 0||typeof Q!=="string"){if(this._config.warnMissing)console.warn(`[i18n] Missing plural translation: "${q}" for locale "${J}"`);return q}let X=C(Q,this._config.pluralSeparator),U=O(X,B,J),Z={count:B,n:B,...G};return this.interpolate(U,Z)}hasTranslation(q,B){let G=B||this._locale;return this.getMessage(q,G)!==void 0}getMessage(q,B){let G=this._messages[B];if(!G)return;let K=q.split(this._config.keySeparator||"."),J=G;for(let Q of K){if(J===void 0||typeof J==="string")return;J=J[Q]}return J}interpolate(q,B){if(!B)return q;return q.replace(/\{(\w+)\}/g,(G,K)=>{let J=B[K];if(J===void 0||J===null)return G;let Q=String(J);return this._config.escapeValues?qq(Q):Q})}}function n(q){Y=q}function _(){return Y}function Vq(q){z=q}function s(){return Object.keys(w)}function F(q,B){if(!w[q])w[q]={};w[q]=V(w[q],B)}function N(q){for(let[B,G]of Object.entries(q))F(B,G)}function L(q,B){return I(q,B||Y)!==void 0}function I(q,B){let G=w[B];if(!G)return;let K=q.split(H.keySeparator||"."),J=G;for(let Q of K){if(J===void 0||typeof J==="string")return;J=J[Q]}return J}function k(q,B){if(!B)return q;return q.replace(/\{(\w+)\}/g,(G,K)=>{let J=B[K];if(J===void 0||J===null)return G;return String(J)})}function f(q,B,G){let K=G||Y,J=I(q,K);if(J===void 0&&K!==z)J=I(q,z);if(J===void 0){if(H.warnMissing)console.warn(`[i18n] Missing translation: "${q}" for locale "${K}"`);return q}if(typeof J!=="string")return q;return k(J,B)}var t=f;function o(q,B,G,K){let J=K||Y,Q=I(q,J);if(Q===void 0&&J!==z)Q=I(q,z);if(Q===void 0||typeof Q!=="string")return q;let X=C(Q,H.pluralSeparator),U=O(X,B,J),Z={count:B,n:B,...G};return k(U,Z)}function l(q,B){return L(q,B)}function a(q,B){return I(q,B||Y)}function y(q={}){return new j(q)}function e(q={}){return y(q)}function Sq(q){if(H={...H,...q},q.locale)Y=q.locale;if(q.fallbackLocale)z=q.fallbackLocale;if(q.messages)N(q.messages)}function V(q,B){let G={...q};for(let K of Object.keys(B)){let J=B[K],Q=G[K];if(typeof J==="object"&&J!==null&&typeof Q==="object"&&Q!==null)G[K]=V(Q,J);else G[K]=J}return G}function qq(q){let B={"&":"&","<":"<",">":">",'"':""","'":"'"};return q.replace(/[&<>"']/g,(G)=>B[G]||G)}function Bq(q,B="medium",G){let K=G||_(),J=R(q),Q=typeof B==="string"?{dateStyle:B}:B;return new Intl.DateTimeFormat(K,Q).format(J)}function Gq(q,B="short",G){let K=G||_(),J=R(q),Q=typeof B==="string"?{timeStyle:B}:B;return new Intl.DateTimeFormat(K,Q).format(J)}function Jq(q,B="medium",G="short",K){let J=K||_(),Q=R(q);return new Intl.DateTimeFormat(J,{dateStyle:B,timeStyle:G}).format(Q)}function Kq(q,B=new Date,G="long",K){let J=K||_(),Q=R(q),X=R(B),U=Q.getTime()-X.getTime(),Z=Math.round(U/1000),$=Math.round(Z/60),A=Math.round($/60),E=Math.round(A/24),b=Math.round(E/7),h=Math.round(E/30),u=Math.round(E/365),W=new Intl.RelativeTimeFormat(J,{style:G});if(Math.abs(Z)<60)return W.format(Z,"second");if(Math.abs($)<60)return W.format($,"minute");if(Math.abs(A)<24)return W.format(A,"hour");if(Math.abs(E)<7)return W.format(E,"day");if(Math.abs(b)<4)return W.format(b,"week");if(Math.abs(h)<12)return W.format(h,"month");return W.format(u,"year")}function Qq(q,B={},G){let K=G||_(),J={...B};if(B.compact)J.notation="compact",J.compactDisplay="short";if(B.precision!==void 0)J.minimumFractionDigits=B.precision,J.maximumFractionDigits=B.precision;return new Intl.NumberFormat(K,J).format(q)}function Uq(q,B="USD",G={},K){let J=K||_();return new Intl.NumberFormat(J,{style:"currency",currency:B,...G}).format(q)}function Xq(q,B={},G){let K=G||_();return new Intl.NumberFormat(K,{style:"percent",...B}).format(q)}function Mq(q,B,G="short",K){let J=K||_();return new Intl.NumberFormat(J,{style:"unit",unit:B,unitDisplay:G}).format(q)}function Dq(q,B=2,G){let K=G||_();if(q===0)return"0 B";let J=1024,Q=["B","KB","MB","GB","TB","PB","EB","ZB","YB"],X=Math.floor(Math.log(q)/Math.log(J)),U=q/Math.pow(J,X);return`${new Intl.NumberFormat(K,{minimumFractionDigits:0,maximumFractionDigits:B}).format(U)} ${Q[X]}`}function Zq(q,B="conjunction",G="long",K){let J=K||_();return new Intl.ListFormat(J,{type:B,style:G}).format(q)}function p(q,B){let G=B||_();return new Intl.PluralRules(G).select(q)}function bq(q,B,G){let K=p(q,G);return B[K]??B.other}function S(q,B="language",G="long",K){let J=K||_();try{return new Intl.DisplayNames(J,{type:B,style:G}).of(q)}catch{return}}function hq(q,B){return S(q,"language","long",B)}function vq(q,B){return S(q,"region","long",B)}function Lq(q,B){return S(q,"currency","long",B)}function kq(q,B,G={},K){let J=K||_();return new Intl.Collator(J,G).compare(q,B)}function fq(q,B={},G){let K=G||_(),J=new Intl.Collator(K,B);return[...q].sort((Q,X)=>J.compare(Q,X))}function R(q){if(q instanceof Date)return q;if(typeof q==="number")return new Date(q);return new Date(q)}function _q(q){let G=(q||_()).split("-")[0].toLowerCase();return["ar","he","fa","ur","yi","ps","sd","ug","ku","ckb"].includes(G)?"rtl":"ltr"}function yq(q){return _q(q)==="rtl"}import{existsSync as $q,readdirSync as g}from"fs";import{readFile as Aq}from"fs/promises";import{basename as d,extname as P,join as x}from"path";import{log as Eq}from"@stacksjs/logging";async function Wq(q){let{directory:B,extensions:G=[".json",".yaml",".yml",".ts",".js"],recursive:K=!1,namespaceSeparator:J="."}=q,Q={};if(!$q(B))return Eq.warn(`[i18n] Translation directory not found: ${B}`),Q;return await m(B,Q,G,K,J,""),N(Q),Q}async function m(q,B,G,K,J,Q){let X=g(q,{withFileTypes:!0});for(let U of X){let Z=x(q,U.name);if(U.isDirectory()){let $=U.name;if(Iq($))await Yq(Z,$,B,G);else if(K){let A=Q?`${Q}${J}${U.name}`:U.name;await m(Z,B,G,K,J,A)}}else if(U.isFile()){let $=P(U.name);if(G.includes($)){let A=d(U.name,$);if(!B[A])B[A]={};let E=await M(Z);if(Q)Hq(B[A],Q,E,J);else B[A]=D(B[A],E)}}}}async function Yq(q,B,G,K){if(!G[B])G[B]={};let J=g(q,{withFileTypes:!0});for(let Q of J){if(!Q.isFile())continue;let X=P(Q.name);if(!K.includes(X))continue;let U=x(q,Q.name),Z=d(Q.name,X),$=await M(U);if(Z===B)G[B]=D(G[B],$);else G[B][Z]=$}}async function M(q){let B=P(q).toLowerCase(),G=await Aq(q,"utf8");switch(B){case".json":try{return JSON.parse(G)}catch{throw Error(`Invalid JSON in translation file: ${q}`)}case".yaml":case".yml":return zq(G);case".ts":case".js":{let K=await import(q);return K.default||K}default:throw Error(`Unsupported file type: ${B}`)}}async function wq(q,B){let G=await M(B);return F(q,G),G}function zq(q){try{let B=Bun.YAML.parse(q);if(!B||typeof B!=="object"||Array.isArray(B))throw Error("YAML content must be an object at the root level");return B}catch(B){let G=B instanceof Error?B.message:"Unknown YAML parse error";throw Error(`Invalid YAML translation file: ${G}`)}}function Iq(q){return/^[a-z]{2}(-[A-Z]{2})?(-[A-Za-z]+)?$/i.test(q)}function Hq(q,B,G,K){let J=B.split(K),Q=q;for(let U=0;U<J.length-1;U++){let Z=J[U];if(!Q[Z]||typeof Q[Z]==="string")Q[Z]={};Q=Q[Z]}let X=J[J.length-1];Q[X]=G}function D(q,B){let G={...q};for(let K of Object.keys(B)){let J=B[K],Q=G[K];if(typeof J==="object"&&J!==null&&typeof Q==="object"&&Q!==null)G[K]=D(Q,J);else G[K]=J}return G}function cq(q,B={}){return{load:()=>Wq({directory:q,...B}),loadLocale:(G,K)=>wq(G,x(q,K))}}async function rq(q){let{loadTranslations:B}=await import("@stacksjs/ts-i18n"),G=await B(q);return N(G),G}var sq={en:{code:"en",name:"English",nativeName:"English",direction:"ltr"},"en-US":{code:"en-US",name:"English (US)",nativeName:"English (US)",direction:"ltr",region:"US"},"en-GB":{code:"en-GB",name:"English (UK)",nativeName:"English (UK)",direction:"ltr",region:"GB"},es:{code:"es",name:"Spanish",nativeName:"Espa\xF1ol",direction:"ltr"},fr:{code:"fr",name:"French",nativeName:"Fran\xE7ais",direction:"ltr"},de:{code:"de",name:"German",nativeName:"Deutsch",direction:"ltr"},it:{code:"it",name:"Italian",nativeName:"Italiano",direction:"ltr"},pt:{code:"pt",name:"Portuguese",nativeName:"Portugu\xEAs",direction:"ltr"},"pt-BR":{code:"pt-BR",name:"Portuguese (Brazil)",nativeName:"Portugu\xEAs (Brasil)",direction:"ltr",region:"BR"},zh:{code:"zh",name:"Chinese",nativeName:"\u4E2D\u6587",direction:"ltr"},"zh-CN":{code:"zh-CN",name:"Chinese (Simplified)",nativeName:"\u7B80\u4F53\u4E2D\u6587",direction:"ltr",region:"CN"},"zh-TW":{code:"zh-TW",name:"Chinese (Traditional)",nativeName:"\u7E41\u9AD4\u4E2D\u6587",direction:"ltr",region:"TW"},ja:{code:"ja",name:"Japanese",nativeName:"\u65E5\u672C\u8A9E",direction:"ltr"},ko:{code:"ko",name:"Korean",nativeName:"\uD55C\uAD6D\uC5B4",direction:"ltr"},ar:{code:"ar",name:"Arabic",nativeName:"\u0627\u0644\u0639\u0631\u0628\u064A\u0629",direction:"rtl"},he:{code:"he",name:"Hebrew",nativeName:"\u05E2\u05D1\u05E8\u05D9\u05EA",direction:"rtl"},ru:{code:"ru",name:"Russian",nativeName:"\u0420\u0443\u0441\u0441\u043A\u0438\u0439",direction:"ltr"},nl:{code:"nl",name:"Dutch",nativeName:"Nederlands",direction:"ltr"},pl:{code:"pl",name:"Polish",nativeName:"Polski",direction:"ltr"},tr:{code:"tr",name:"Turkish",nativeName:"T\xFCrk\xE7e",direction:"ltr"},vi:{code:"vi",name:"Vietnamese",nativeName:"Ti\u1EBFng Vi\u1EC7t",direction:"ltr"},th:{code:"th",name:"Thai",nativeName:"\u0E44\u0E17\u0E22",direction:"ltr"},hi:{code:"hi",name:"Hindi",nativeName:"\u0939\u093F\u0928\u094D\u0926\u0940",direction:"ltr"}};import{loadTranslations as JB,generateTypes as KB,generateTypesFromModule as QB,generateSampleConfig as UB,writeOutputs as XB,createTranslator as ZB}from"@stacksjs/ts-i18n";export{XB as writeI18nOutputs,e as useI18n,t as trans,a as tm,l as te,o as tc,f as t,fq as sortStrings,n as setLocale,Vq as setFallbackLocale,O as selectPluralForm,bq as selectPlural,C as parsePluralForms,JB as loadTranslationsFromDisk,N as loadTranslations,wq as loadLocale,rq as loadFromTsI18n,Wq as loadFromDirectory,M as loadFile,yq as isRTL,L as hasTranslation,Tq as hasPluralRule,_q as getTextDirection,Rq as getSupportedPluralLocales,vq as getRegionName,r as getPluralRule,c as getPluralCategory,_ as getLocale,hq as getLanguageName,S as getDisplayName,Lq as getCurrencyName,s as getAvailableLocales,QB as generateI18nTypesFromModule,KB as generateI18nTypes,UB as generateI18nSampleConfig,Mq as formatUnit,Gq as formatTime,Kq as formatRelativeTime,p as formatPlural,Xq as formatPercent,Qq as formatNumber,Zq as formatList,Jq as formatDateTime,Bq as formatDate,Uq as formatCurrency,Dq as formatBytes,ZB as createSimpleTranslator,cq as createLoader,y as createI18n,Sq as configure,kq as compareStrings,F as addTranslations,Fq as addPluralRule,sq as LOCALE_INFO,j as I18n};
|
|
2
|
+
var i=import.meta.require;function c(q,B){return r(B)(q)}function r(q){let B=q.split("-")[0].toLowerCase(),G=T[B];if(G)return G;return()=>"other"}function C(q,B="|"){let G=q.split(B).map((J)=>J.trim()),K=new Map;if(G.length===1)K.set("other",G[0]);else if(G.length===2)K.set("one",G[0]),K.set("other",G[1]);else if(G.length===3)K.set("zero",G[0]),K.set("one",G[1]),K.set("other",G[2]);else if(G.length>=4){let J=["zero","one","two","few","many","other"];for(let Q=0;Q<G.length&&Q<J.length;Q++)K.set(J[Q],G[Q])}return K}function O(q,B,G){let K=c(B,G);if(q.has(K))return q.get(K);if(q.has("other"))return q.get("other");let J=q.values().next();return J.done?"":J.value}var T={en:(q)=>q===1?"one":"other",de:(q)=>q===1?"one":"other",nl:(q)=>q===1?"one":"other",es:(q)=>q===1?"one":"other",it:(q)=>q===1?"one":"other",pt:(q)=>q===1?"one":"other",sv:(q)=>q===1?"one":"other",da:(q)=>q===1?"one":"other",no:(q)=>q===1?"one":"other",fi:(q)=>q===1?"one":"other",el:(q)=>q===1?"one":"other",he:(q)=>q===1?"one":"other",hu:(q)=>q===1?"one":"other",tr:(q)=>q===1?"one":"other",zh:()=>"other",ja:()=>"other",ko:()=>"other",vi:()=>"other",th:()=>"other",id:()=>"other",ms:()=>"other",fr:(q)=>q===0||q===1?"one":"other",ru:(q)=>{let B=q%10,G=q%100;if(B===1&&G!==11)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B>=5&&B<=9||G>=11&&G<=14)return"many";return"other"},uk:(q)=>{let B=q%10,G=q%100;if(B===1&&G!==11)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B>=5&&B<=9||G>=11&&G<=14)return"many";return"other"},pl:(q)=>{let B=q%10,G=q%100;if(q===1)return"one";if(B>=2&&B<=4&&(G<12||G>14))return"few";if(B===0||B===1||B>=5&&B<=9||G>=12&&G<=14)return"many";return"other"},cs:(q)=>{if(q===1)return"one";if(q>=2&&q<=4)return"few";return"other"},sk:(q)=>{if(q===1)return"one";if(q>=2&&q<=4)return"few";return"other"},ar:(q)=>{if(q===0)return"zero";if(q===1)return"one";if(q===2)return"two";let B=q%100;if(B>=3&&B<=10)return"few";if(B>=11&&B<=99)return"many";return"other"},cy:(q)=>{if(q===0)return"zero";if(q===1)return"one";if(q===2)return"two";if(q===3)return"few";if(q===6)return"many";return"other"},sl:(q)=>{let B=q%100;if(B===1)return"one";if(B===2)return"two";if(B===3||B===4)return"few";return"other"},ga:(q)=>{if(q===1)return"one";if(q===2)return"two";if(q>=3&&q<=6)return"few";if(q>=7&&q<=10)return"many";return"other"},lt:(q)=>{let B=q%10,G=q%100;if(B===1&&(G<11||G>19))return"one";if(B>=2&&B<=9&&(G<11||G>19))return"few";return"other"},lv:(q)=>{let B=q%10,G=q%100;if(q===0)return"zero";if(B===1&&G!==11)return"one";return"other"},ro:(q)=>{if(q===1)return"one";let B=q%100;if(q===0||B>=1&&B<=19)return"few";return"other"},hi:(q)=>q===0||q===1?"one":"other"};function Rq(){return Object.keys(T)}function Tq(q){return q.split("-")[0].toLowerCase()in T}function Fq(q,B){let G=q.split("-")[0].toLowerCase();T[G]=B}var v={locale:"en",fallbackLocale:"en",availableLocales:["en"],messages:{},warnMissing:!0,escapeValues:!1,keySeparator:".",pluralSeparator:"|"},Y="en",z="en",w={},H={...v};class j{_locale;_fallbackLocale;_messages;_config;constructor(q={}){this._config={...v,...q},this._locale=this._config.locale,this._fallbackLocale=this._config.fallbackLocale,this._messages=this._config.messages||{}}get locale(){return this._locale}set locale(q){this._locale=q}get fallbackLocale(){return this._fallbackLocale}get availableLocales(){return Object.keys(this._messages)}t=(q,B,G)=>{return this.translate(q,B,G)};tc=(q,B,G,K)=>{return this.translatePlural(q,B,G,K)};te=(q,B)=>{return this.hasTranslation(q,B)};tm=(q,B)=>{return this.getMessage(q,B)};setLocale=(q)=>{this._locale=q};addTranslations=(q,B)=>{if(!this._messages[q])this._messages[q]={};this._messages[q]=V(this._messages[q],B)};d=(q,B)=>{let G=typeof q==="number"?new Date(q):q,K=this._config.dateTimeFormats?.[this._locale]?.[B||"short"]||{};return new Intl.DateTimeFormat(this._locale,K).format(G)};n=(q,B)=>{let G=this._config.numberFormats?.[this._locale]?.[B||"decimal"]||{};return new Intl.NumberFormat(this._locale,G).format(q)};translate(q,B,G){let K=G||this._locale,J=this.getMessage(q,K);if(J===void 0){let Q=K.indexOf("-");if(Q>0){let X=K.slice(0,Q);if(X!==this._fallbackLocale)J=this.getMessage(q,X)}}if(J===void 0&&K!==this._fallbackLocale)J=this.getMessage(q,this._fallbackLocale);if(J===void 0){if(this._config.warnMissing)console.warn(`[i18n] Missing translation: "${q}" for locale "${K}"`);if(this._config.missingHandler){let Q=this._config.missingHandler(K,q);if(Q!==void 0)return Q}return q}if(typeof J!=="string")return q;return this.interpolate(J,B)}translatePlural(q,B,G,K){let J=K||this._locale,Q=this.getMessage(q,J);if(Q===void 0&&J!==this._fallbackLocale)Q=this.getMessage(q,this._fallbackLocale);if(Q===void 0||typeof Q!=="string"){if(this._config.warnMissing)console.warn(`[i18n] Missing plural translation: "${q}" for locale "${J}"`);return q}let X=C(Q,this._config.pluralSeparator),U=O(X,B,J),Z={count:B,n:B,...G};return this.interpolate(U,Z)}hasTranslation(q,B){let G=B||this._locale;return this.getMessage(q,G)!==void 0}getMessage(q,B){let G=this._messages[B];if(!G)return;let K=q.split(this._config.keySeparator||"."),J=G;for(let Q of K){if(J===void 0||typeof J==="string")return;J=J[Q]}return J}interpolate(q,B){if(!B)return q;return q.replace(/\{(\w+)\}/g,(G,K)=>{let J=B[K];if(J===void 0||J===null)return G;let Q=String(J);return this._config.escapeValues?qq(Q):Q})}}function n(q){Y=q}function _(){return Y}function Vq(q){z=q}function s(){return Object.keys(w)}function F(q,B){if(!w[q])w[q]={};w[q]=V(w[q],B)}function N(q){for(let[B,G]of Object.entries(q))F(B,G)}function L(q,B){return I(q,B||Y)!==void 0}function I(q,B){let G=w[B];if(!G)return;let K=q.split(H.keySeparator||"."),J=G;for(let Q of K){if(J===void 0||typeof J==="string")return;J=J[Q]}return J}function k(q,B){if(!B)return q;return q.replace(/\{(\w+)\}/g,(G,K)=>{let J=B[K];if(J===void 0||J===null)return G;return String(J)})}function f(q,B,G){let K=G||Y,J=I(q,K);if(J===void 0&&K!==z)J=I(q,z);if(J===void 0){if(H.warnMissing)console.warn(`[i18n] Missing translation: "${q}" for locale "${K}"`);return q}if(typeof J!=="string")return q;return k(J,B)}var t=f;function o(q,B,G,K){let J=K||Y,Q=I(q,J);if(Q===void 0&&J!==z)Q=I(q,z);if(Q===void 0||typeof Q!=="string")return q;let X=C(Q,H.pluralSeparator),U=O(X,B,J),Z={count:B,n:B,...G};return k(U,Z)}function l(q,B){return L(q,B)}function a(q,B){return I(q,B||Y)}function y(q={}){return new j(q)}function e(q={}){return y(q)}function Sq(q){if(H={...H,...q},q.locale)Y=q.locale;if(q.fallbackLocale)z=q.fallbackLocale;if(q.messages)N(q.messages)}function V(q,B){let G={...q};for(let K of Object.keys(B)){let J=B[K],Q=G[K];if(typeof J==="object"&&J!==null&&typeof Q==="object"&&Q!==null)G[K]=V(Q,J);else G[K]=J}return G}function qq(q){let B={"&":"&","<":"<",">":">",'"':""","'":"'"};return q.replace(/[&<>"']/g,(G)=>B[G]||G)}function Bq(q,B="medium",G){let K=G||_(),J=R(q),Q=typeof B==="string"?{dateStyle:B}:B;return new Intl.DateTimeFormat(K,Q).format(J)}function Gq(q,B="short",G){let K=G||_(),J=R(q),Q=typeof B==="string"?{timeStyle:B}:B;return new Intl.DateTimeFormat(K,Q).format(J)}function Jq(q,B="medium",G="short",K){let J=K||_(),Q=R(q);return new Intl.DateTimeFormat(J,{dateStyle:B,timeStyle:G}).format(Q)}function Kq(q,B=new Date,G="long",K){let J=K||_(),Q=R(q),X=R(B),U=Q.getTime()-X.getTime(),Z=Math.round(U/1000),$=Math.round(Z/60),A=Math.round($/60),E=Math.round(A/24),b=Math.round(E/7),h=Math.round(E/30),u=Math.round(E/365),W=new Intl.RelativeTimeFormat(J,{style:G});if(Math.abs(Z)<60)return W.format(Z,"second");if(Math.abs($)<60)return W.format($,"minute");if(Math.abs(A)<24)return W.format(A,"hour");if(Math.abs(E)<7)return W.format(E,"day");if(Math.abs(b)<4)return W.format(b,"week");if(Math.abs(h)<12)return W.format(h,"month");return W.format(u,"year")}function Qq(q,B={},G){let K=G||_(),J={...B};if(B.compact)J.notation="compact",J.compactDisplay="short";if(B.precision!==void 0)J.minimumFractionDigits=B.precision,J.maximumFractionDigits=B.precision;return new Intl.NumberFormat(K,J).format(q)}function Uq(q,B="USD",G={},K){let J=K||_();return new Intl.NumberFormat(J,{style:"currency",currency:B,...G}).format(q)}function Xq(q,B={},G){let K=G||_();return new Intl.NumberFormat(K,{style:"percent",...B}).format(q)}function Mq(q,B,G="short",K){let J=K||_();return new Intl.NumberFormat(J,{style:"unit",unit:B,unitDisplay:G}).format(q)}function Dq(q,B=2,G){let K=G||_();if(q===0)return"0 B";let J=1024,Q=["B","KB","MB","GB","TB","PB","EB","ZB","YB"],X=Math.floor(Math.log(q)/Math.log(J)),U=q/Math.pow(J,X);return`${new Intl.NumberFormat(K,{minimumFractionDigits:0,maximumFractionDigits:B}).format(U)} ${Q[X]}`}function Zq(q,B="conjunction",G="long",K){let J=K||_();return new Intl.ListFormat(J,{type:B,style:G}).format(q)}function p(q,B){let G=B||_();return new Intl.PluralRules(G).select(q)}function bq(q,B,G){let K=p(q,G);return B[K]??B.other}function S(q,B="language",G="long",K){let J=K||_();try{return new Intl.DisplayNames(J,{type:B,style:G}).of(q)}catch{return}}function hq(q,B){return S(q,"language","long",B)}function vq(q,B){return S(q,"region","long",B)}function Lq(q,B){return S(q,"currency","long",B)}function kq(q,B,G={},K){let J=K||_();return new Intl.Collator(J,G).compare(q,B)}function fq(q,B={},G){let K=G||_(),J=new Intl.Collator(K,B);return[...q].sort((Q,X)=>J.compare(Q,X))}function R(q){if(q instanceof Date)return q;if(typeof q==="number")return new Date(q);return new Date(q)}function _q(q){let G=(q||_()).split("-")[0].toLowerCase();return["ar","he","fa","ur","yi","ps","sd","ug","ku","ckb"].includes(G)?"rtl":"ltr"}function yq(q){return _q(q)==="rtl"}import{existsSync as $q,readdirSync as g}from"fs";import{readFile as Aq}from"fs/promises";import{basename as d,extname as P,join as x}from"path";import{log as Eq}from"@stacksjs/logging";async function Wq(q){let{directory:B,extensions:G=[".json",".yaml",".yml",".ts",".js"],recursive:K=!1,namespaceSeparator:J="."}=q,Q={};if(!$q(B))return Eq.warn(`[i18n] Translation directory not found: ${B}`),Q;return await m(B,Q,G,K,J,""),N(Q),Q}async function m(q,B,G,K,J,Q){let X=g(q,{withFileTypes:!0});for(let U of X){let Z=x(q,U.name);if(U.isDirectory()){let $=U.name;if(Iq($))await Yq(Z,$,B,G);else if(K){let A=Q?`${Q}${J}${U.name}`:U.name;await m(Z,B,G,K,J,A)}}else if(U.isFile()){let $=P(U.name);if(G.includes($)){let A=d(U.name,$);if(!B[A])B[A]={};let E=await M(Z);if(Q)Hq(B[A],Q,E,J);else B[A]=D(B[A],E)}}}}async function Yq(q,B,G,K){if(!G[B])G[B]={};let J=g(q,{withFileTypes:!0});for(let Q of J){if(!Q.isFile())continue;let X=P(Q.name);if(!K.includes(X))continue;let U=x(q,Q.name),Z=d(Q.name,X),$=await M(U);if(Z===B)G[B]=D(G[B],$);else G[B][Z]=$}}async function M(q){let B=P(q).toLowerCase(),G=await Aq(q,"utf8");switch(B){case".json":try{return JSON.parse(G)}catch{throw Error(`Invalid JSON in translation file: ${q}`)}case".yaml":case".yml":return zq(G);case".ts":case".js":{let K=await import(q);return K.default||K}default:throw Error(`Unsupported file type: ${B}`)}}async function wq(q,B){let G=await M(B);return F(q,G),G}function zq(q){try{let B=Bun.YAML.parse(q);if(!B||typeof B!=="object"||Array.isArray(B))throw Error("YAML content must be an object at the root level");return B}catch(B){let G=B instanceof Error?B.message:"Unknown YAML parse error";throw Error(`Invalid YAML translation file: ${G}`)}}function Iq(q){return/^[a-z]{2}(?:-[A-Z]{2})?(?:-[A-Za-z]+)?$/i.test(q)}function Hq(q,B,G,K){let J=B.split(K),Q=q;for(let U=0;U<J.length-1;U++){let Z=J[U];if(!Q[Z]||typeof Q[Z]==="string")Q[Z]={};Q=Q[Z]}let X=J[J.length-1];Q[X]=G}function D(q,B){let G={...q};for(let K of Object.keys(B)){let J=B[K],Q=G[K];if(typeof J==="object"&&J!==null&&typeof Q==="object"&&Q!==null)G[K]=D(Q,J);else G[K]=J}return G}function cq(q,B={}){return{load:()=>Wq({directory:q,...B}),loadLocale:(G,K)=>wq(G,x(q,K))}}async function rq(q){let{loadTranslations:B}=await import("@stacksjs/ts-i18n"),G=await B(q);return N(G),G}var sq={en:{code:"en",name:"English",nativeName:"English",direction:"ltr"},"en-US":{code:"en-US",name:"English (US)",nativeName:"English (US)",direction:"ltr",region:"US"},"en-GB":{code:"en-GB",name:"English (UK)",nativeName:"English (UK)",direction:"ltr",region:"GB"},es:{code:"es",name:"Spanish",nativeName:"Espa\xF1ol",direction:"ltr"},fr:{code:"fr",name:"French",nativeName:"Fran\xE7ais",direction:"ltr"},de:{code:"de",name:"German",nativeName:"Deutsch",direction:"ltr"},it:{code:"it",name:"Italian",nativeName:"Italiano",direction:"ltr"},pt:{code:"pt",name:"Portuguese",nativeName:"Portugu\xEAs",direction:"ltr"},"pt-BR":{code:"pt-BR",name:"Portuguese (Brazil)",nativeName:"Portugu\xEAs (Brasil)",direction:"ltr",region:"BR"},zh:{code:"zh",name:"Chinese",nativeName:"\u4E2D\u6587",direction:"ltr"},"zh-CN":{code:"zh-CN",name:"Chinese (Simplified)",nativeName:"\u7B80\u4F53\u4E2D\u6587",direction:"ltr",region:"CN"},"zh-TW":{code:"zh-TW",name:"Chinese (Traditional)",nativeName:"\u7E41\u9AD4\u4E2D\u6587",direction:"ltr",region:"TW"},ja:{code:"ja",name:"Japanese",nativeName:"\u65E5\u672C\u8A9E",direction:"ltr"},ko:{code:"ko",name:"Korean",nativeName:"\uD55C\uAD6D\uC5B4",direction:"ltr"},ar:{code:"ar",name:"Arabic",nativeName:"\u0627\u0644\u0639\u0631\u0628\u064A\u0629",direction:"rtl"},he:{code:"he",name:"Hebrew",nativeName:"\u05E2\u05D1\u05E8\u05D9\u05EA",direction:"rtl"},ru:{code:"ru",name:"Russian",nativeName:"\u0420\u0443\u0441\u0441\u043A\u0438\u0439",direction:"ltr"},nl:{code:"nl",name:"Dutch",nativeName:"Nederlands",direction:"ltr"},pl:{code:"pl",name:"Polish",nativeName:"Polski",direction:"ltr"},tr:{code:"tr",name:"Turkish",nativeName:"T\xFCrk\xE7e",direction:"ltr"},vi:{code:"vi",name:"Vietnamese",nativeName:"Ti\u1EBFng Vi\u1EC7t",direction:"ltr"},th:{code:"th",name:"Thai",nativeName:"\u0E44\u0E17\u0E22",direction:"ltr"},hi:{code:"hi",name:"Hindi",nativeName:"\u0939\u093F\u0928\u094D\u0926\u0940",direction:"ltr"}};import{loadTranslations as JB,generateTypes as KB,generateTypesFromModule as QB,generateSampleConfig as UB,writeOutputs as XB,createTranslator as ZB}from"@stacksjs/ts-i18n";export{XB as writeI18nOutputs,e as useI18n,t as trans,a as tm,l as te,o as tc,f as t,fq as sortStrings,n as setLocale,Vq as setFallbackLocale,O as selectPluralForm,bq as selectPlural,C as parsePluralForms,JB as loadTranslationsFromDisk,N as loadTranslations,wq as loadLocale,rq as loadFromTsI18n,Wq as loadFromDirectory,M as loadFile,yq as isRTL,L as hasTranslation,Tq as hasPluralRule,_q as getTextDirection,Rq as getSupportedPluralLocales,vq as getRegionName,r as getPluralRule,c as getPluralCategory,_ as getLocale,hq as getLanguageName,S as getDisplayName,Lq as getCurrencyName,s as getAvailableLocales,QB as generateI18nTypesFromModule,KB as generateI18nTypes,UB as generateI18nSampleConfig,Mq as formatUnit,Gq as formatTime,Kq as formatRelativeTime,p as formatPlural,Xq as formatPercent,Qq as formatNumber,Zq as formatList,Jq as formatDateTime,Bq as formatDate,Uq as formatCurrency,Dq as formatBytes,ZB as createSimpleTranslator,cq as createLoader,y as createI18n,Sq as configure,kq as compareStrings,F as addTranslations,Fq as addPluralRule,sq as LOCALE_INFO,j as I18n};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/i18n",
|
|
3
|
-
"version": "0.70.
|
|
3
|
+
"version": "0.70.26",
|
|
4
4
|
"description": "Internationalization system for Stacks.js applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
18
19
|
"bun": "./src/index.ts",
|
|
19
|
-
"import": "./dist/index.js"
|
|
20
|
-
"types": "./dist/index.d.ts"
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"main": "./dist/index.js",
|
package/src/loader.ts
CHANGED
|
@@ -233,7 +233,7 @@ function parseYaml(content: string): TranslationMessages {
|
|
|
233
233
|
*/
|
|
234
234
|
function isLocaleCode(str: string): boolean {
|
|
235
235
|
// Match patterns like "en", "en-US", "zh-Hans", etc.
|
|
236
|
-
return /^[a-z]{2}(
|
|
236
|
+
return /^[a-z]{2}(?:-[A-Z]{2})?(?:-[A-Za-z]+)?$/i.test(str)
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
/**
|