UNPKG

@i18n-pro/solid

Version:

An out-of-the-box, lightweight i18n auto-translation solution for Solid

3 lines (2 loc) 3.14 kB
// @i18n-pro/solid v2.0.0-alpha.1 2025/6/29 12:29:27 MIT@Copyright (c) 2023-present Eyelly Wu <https://github.com/eyelly-wu> import{createComponent as t}from"solid-js/web";import{createSignal as e,createContext as r,useContext as n,splitProps as o}from"solid-js";import{initI18n as i}from"i18n-pro";var a=function(){return a=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},a.apply(this,arguments)};function c(t,e,r,n){return new(r||(r=Promise))((function(o,i){function a(t){try{u(n.next(t))}catch(t){i(t)}}function c(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,c)}u((n=n.apply(t,e||[])).next())}))}function u(t,e){var r,n,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=c(0),a.throw=c(1),a.return=c(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(u){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=e.call(t,i)}catch(t){c=[6,t],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var l="unknown",f={t:function(t){return t},setI18n:function(t){return Promise.resolve(a(a({},t),{namespace:l}))},i18nState:e({namespace:l})[0]},s=r(f),p=s.Provider;function h(){var t=n(s);return t===f&&console.warn("useI18n should be wrapped by Provider"),t}function v(r){var n=this,a=o(r,["children"]),l=a[0],f=a[1],s=i(f),h=s.t,v=s.setI18n,y=e(f),b=y[0],d=y[1],w=e(h),m=w[0],g=w[1],j=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return m().apply(void 0,function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))}([t],e,!1))};Object.defineProperties(j,{t:{get:function(){return m().t}},withLocale:{get:function(){return m().withLocale}}});var I={t:j,setI18n:function(t){return c(n,void 0,void 0,(function(){var e;return u(this,(function(r){switch(r.label){case 0:return[4,v(t)];case 1:return e=r.sent(),g((function(){return h.withLocale()})),d(e),[2,e]}}))}))},i18nState:b};return t(p,{value:I,get children(){return l.children}})}export{v as I18nProvider,h as useI18n};