@tupipu/translator
Version:
A lightweight, extensible, and production-ready translation framework built specifically for Next.js apps. This package simplifies internationalization (i18n) by combining static and dynamic translations, language detection, server/client rendering compat
12 lines (11 loc) • 9.5 kB
JavaScript
;var e=require("react"),r=function(){return r=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e},r.apply(this,arguments)};function t(e,r,t,n){return new(t||(t=Promise))(function(o,a){function i(e){try{c(n.next(e))}catch(e){a(e)}}function s(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var r;e.done?o(e.value):(r=e.value,r instanceof t?r:new t(function(e){e(r)})).then(i,s)}c((n=n.apply(e,r||[])).next())})}function n(e,r){var t,n,o,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=s(0),i.throw=s(1),i.return=s(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(c){return function(s){if(t)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(t=1,n&&(o=2&s[0]?n.return:s[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,s[1])).done)return o;switch(n=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,n=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=r.call(e,a)}catch(e){s=[6,e],n=0}finally{t=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}"function"==typeof SuppressedError&&SuppressedError;var o,a={exports:{}},i={};var s,c,u={};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/function l(){return s||(s=1,"production"!==process.env.NODE_ENV&&function(){function r(e){if(null==e)return null;if("function"==typeof e)return e.$$typeof===O?null:e.displayName||e.name||null;if("string"==typeof e)return e;switch(e){case b:return"Fragment";case m:return"Profiler";case d:return"StrictMode";case k:return"Suspense";case S:return"SuspenseList";case x:return"Activity"}if("object"==typeof e)switch("number"==typeof e.tag&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case y:return"Portal";case g:return(e.displayName||"Context")+".Provider";case v:return(e._context.displayName||"Context")+".Consumer";case h:var t=e.render;return(e=e.displayName)||(e=""!==(e=t.displayName||t.name||"")?"ForwardRef("+e+")":"ForwardRef"),e;case w:return null!==(t=e.displayName||null)?t:r(e.type)||"Memo";case j:t=e._payload,e=e._init;try{return r(e(t))}catch(e){}}return null}function t(e){return""+e}function n(e){try{t(e);var r=!1}catch(e){r=!0}if(r){var n=(r=console).error,o="function"==typeof Symbol&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return n.call(r,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",o),t(e)}}function o(e){if(e===b)return"<>";if("object"==typeof e&&null!==e&&e.$$typeof===j)return"<...>";try{var t=r(e);return t?"<"+t+">":"<...>"}catch(e){return"<...>"}}function a(){return Error("react-stack-top-frame")}function i(){var e=r(this.type);return P[e]||(P[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),void 0!==(e=this.props.ref)?e:null}function s(e,t,o,a,s,u,f,y){var b,d=t.children;if(void 0!==d)if(a)if(E(d)){for(a=0;a<d.length;a++)c(d[a]);Object.freeze&&Object.freeze(d)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else c(d);if(_.call(t,"key")){d=r(e);var m=Object.keys(t).filter(function(e){return"key"!==e});a=0<m.length?"{key: someKey, "+m.join(": ..., ")+": ...}":"{key: someKey}",$[d+a]||(m=0<m.length?"{"+m.join(": ..., ")+": ...}":"{}",console.error('A props object containing a "key" prop is being spread into JSX:\n let props = %s;\n <%s {...props} />\nReact keys must be passed directly to JSX without using spread:\n let props = %s;\n <%s key={someKey} {...props} />',a,d,m,d),$[d+a]=!0)}if(d=null,void 0!==o&&(n(o),d=""+o),function(e){if(_.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return void 0!==e.key}(t)&&(n(t.key),d=""+t.key),"key"in t)for(var v in o={},t)"key"!==v&&(o[v]=t[v]);else o=t;return d&&function(e,r){function t(){l||(l=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",r))}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}(o,"function"==typeof e?e.displayName||e.name||"Unknown":e),function(e,r,t,n,o,a,s,c){return t=a.ref,e={$$typeof:p,type:e,key:r,props:a,_owner:o},null!==(void 0!==t?t:null)?Object.defineProperty(e,"ref",{enumerable:!1,get:i}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:s}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:c}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}(e,d,u,0,null===(b=T.A)?null:b.getOwner(),o,f,y)}function c(e){"object"==typeof e&&null!==e&&e.$$typeof===p&&e._store&&(e._store.validated=1)}var l,f=e,p=Symbol.for("react.transitional.element"),y=Symbol.for("react.portal"),b=Symbol.for("react.fragment"),d=Symbol.for("react.strict_mode"),m=Symbol.for("react.profiler"),v=Symbol.for("react.consumer"),g=Symbol.for("react.context"),h=Symbol.for("react.forward_ref"),k=Symbol.for("react.suspense"),S=Symbol.for("react.suspense_list"),w=Symbol.for("react.memo"),j=Symbol.for("react.lazy"),x=Symbol.for("react.activity"),O=Symbol.for("react.client.reference"),T=f.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,_=Object.prototype.hasOwnProperty,E=Array.isArray,N=console.createTask?console.createTask:function(){return null},P={},R=(f={"react-stack-bottom-frame":function(e){return e()}})["react-stack-bottom-frame"].bind(f,a)(),C=N(o(a)),$={};u.Fragment=b,u.jsx=function(e,r,t,n,a){var i=1e4>T.recentlyCreatedOwnerStacks++;return s(e,r,t,!1,0,a,i?Error("react-stack-top-frame"):R,i?N(o(e)):C)},u.jsxs=function(e,r,t,n,a){var i=1e4>T.recentlyCreatedOwnerStacks++;return s(e,r,t,!0,0,a,i?Error("react-stack-top-frame"):R,i?N(o(e)):C)}}()),u}var f=(c||(c=1,"production"===process.env.NODE_ENV?a.exports=function(){if(o)return i;o=1;var e=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function t(r,t,n){var o=null;if(void 0!==n&&(o=""+n),void 0!==t.key&&(o=""+t.key),"key"in t)for(var a in n={},t)"key"!==a&&(n[a]=t[a]);else n=t;return t=n.ref,{$$typeof:e,type:r,key:o,ref:void 0!==t?t:null,props:n}}return i.Fragment=r,i.jsx=t,i.jsxs=t,i}():a.exports=l()),a.exports),p=e.createContext(void 0),y=function(){var r=e.useContext(p);if(!r)throw new Error("useTranslator must be used within LanguageTranslator");return r};function b(e,r){return r.split(".").reduce(function(e,r){return e&&e[r]?e[r]:void 0},e)}exports.TranslatorProvider=function(o){var a=o.children,i=o.userId,s=o.email,c=o.apiKeyId,u=o.apiKey,l=e.useState("en"),y=l[0],b=l[1];e.useEffect(function(){var e=localStorage.getItem("lang")||navigator.language.slice(0,2);b(["fr","es","en"].includes(e)?e:"en")},[]),e.useEffect(function(){localStorage.setItem("lang",y)},[y]);var d=e.useCallback(function(e){return t(void 0,void 0,void 0,function(){var t,o,a,l;return n(this,function(n){switch(n.label){case 0:if("en"===y)return[2,e];t=new AbortController,o=setTimeout(function(){return t.abort()},3e4),n.label=1;case 1:return n.trys.push([1,4,,5]),[4,fetch("https://yjljx1tq80.execute-api.us-east-1.amazonaws.com/main/app/translator",{method:"POST",headers:r(r(r(r({"Content-Type":"application/json"},u&&{"x-api-key":u}),c&&{"x-api-key-id":c}),i&&{"x-user-id":i}),s&&{"x-user-email":s}),body:JSON.stringify({text:e,sourceLang:"en",targetLang:y})})];case 2:if(a=n.sent(),clearTimeout(o),!a.ok)throw new Error("HTTP error! status: ".concat(a.status));return[4,a.json()];case 3:return[2,n.sent().translatedText];case 4:return l=n.sent(),console.error("Translation error",l),[2,e];case 5:return[2]}})})},[y,u,c,i,s]);return f.jsx(p.Provider,{value:{language:y,setLanguage:b,translate:d},children:a})},exports.useTranslation=function(r){var o=y(),a=o.language,i=o.translate,s=e.useCallback(function(e){return t(void 0,void 0,void 0,function(){var t,o;return n(this,function(n){switch(n.label){case 0:return"string"==typeof(t=b(r[a],e))?[2,t]:"string"==typeof(o=b(r.en,e))?[2,o]:[4,i(e)];case 1:return[2,n.sent()]}})})},[a,i,r]);return{t:s}},exports.useTranslatorContext=y;
//# sourceMappingURL=index.cjs.map