imba-localization 0.1.7 → 0.1.8

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/README.md CHANGED
@@ -6,11 +6,12 @@ A lightweight Imba module for loading and handling JSON-based localization files
6
6
  ## ✨ Features
7
7
 
8
8
  - 🔍 **Automatic language detection** - Uses the user's browser language settings
9
+ * 💾 **Persistence**: Stores user choice in local storage across sessions.
9
10
  - 🔄 **Smart fallback system** - Falls back to a default language when needed
10
11
  - 🧠 **Intuitive access** - Proxy-based access to translation strings
11
12
  - 📡 **Event handling** - Support for `onready`, `onchange`, and `onerror` events
12
13
  - 🚀 **Simple integration** - Easy to use in any Imba-based web application
13
- - 🧩 **<LanguageSelector>** - Plug and play component for switching languages
14
+ - 🧩 **`<LanguageSelector>`** - Plug and play component for switching languages
14
15
 
15
16
  ## 📘 Notes
16
17
 
package/index.imba CHANGED
@@ -4,7 +4,6 @@ export class Localization
4
4
  onchange
5
5
  languages = {}
6
6
  preferred = (window..navigator..language || 'en-US').slice(0, 2)
7
- #active
8
7
  default
9
8
 
10
9
  def constructor url, dft = 'en'
@@ -41,11 +40,14 @@ export class Localization
41
40
  onready! if onready isa Function
42
41
 
43
42
  get active
44
- return languages[#active] ? #active : default
43
+ const saved = window.localStorage.getItem('imba-localization')
44
+ return saved if saved and languages[saved]
45
+ return preferred if languages[preferred]
46
+ return default
45
47
 
46
48
  set active name
47
- if name and languages[name]
48
- #active = name
49
- else
50
- #active = languages[preferred] ? preferred : default
51
- onchange(#active) if onchange isa Function
49
+ name = name and languages[name] ? name : active
50
+ if window.localStorage.getItem('imba-localization') != name
51
+ window.localStorage.setItem('imba-localization', name)
52
+ onchange(name) if onchange isa Function
53
+
package/index.js CHANGED
@@ -1 +1 @@
1
- var S=Symbol.for("#__init__"),I=Symbol.for("#__initor__"),W=Symbol.for("#__inited__"),H=Symbol.for("#__hooks__"),O=Symbol.for("#__patch__"),K=Symbol.for("#has"),X=Symbol.for("#meta"),Y=Symbol.for("imba"),c=Symbol.for("#__mixin__"),M=Symbol.for("#matcher"),C=Symbol.for("#L"),j=Symbol.for("#appendChild"),i=Symbol.for("#afterVisit"),d=Symbol.for("#beforeReconcile"),g=Symbol.for("#afterReconcile"),y=Symbol.for("##up");var G={IsExtension:1,IsTag:2,HasDescriptors:4,HasSuperCalls:8,HasConstructor:16,HasFields:32,HasMixins:64,HasInitor:128,HasDecorators:256,IsObjectExtension:512},U=new Map,Z=globalThis[Y]||={counter:0,classes:U};function E(h,n={}){return U.has(h)||U.set(h,{symbol:Symbol(h.name),parent:Object.getPrototypeOf(h.prototype)?.constructor,for:h,uses:null,inits:null,id:Z.counter++,...n}),U.get(h)}function q(h,n){return typeof n==="string"?typeof h===n:n?.[Symbol.hasInstance]?.(h)}function A(h,n){if(!h||!n)return!1;if(h.get)return n.get===h.get;if(h.set)return n.set===h.set;if(h.value)return h.value===n.value}function J(h,n,f,u={}){let r=h.constructor;if(!f&&n){if(f=Object.getOwnPropertyDescriptors(n),delete f.constructor,f[S])console.warn(`Cannot define plain fields when extending class ${r.name}`),delete f[S]}let o=E(r);if(o&&o.augments){let F=new Map;for(let z of Object.keys(f)){let w=Object.getOwnPropertyDescriptor(h,z);for(let v of o.augments){let T=F.get(v);T||F.set(v,T={});let B=Object.getOwnPropertyDescriptor(v.prototype,z);if(B&&!A(w,B))console.warn("wont extend",z,B,w);else T[z]=f[z]}}for(let[z,w]of F)if(Object.keys(w).length)J(z.prototype,null,w)}return Object.defineProperties(h,f),h}function L(h,n){let f=E(h),u=E(n);if(u.parent){if(!(h.prototype instanceof u.parent))throw new Error(`Mixin ${n.name} has superclass not present in target class`)}if(!u.augments){u.augments=new Set;let o=u.ref=Symbol(n.name),F=Object[Symbol.hasInstance];n.prototype[o]=!0,Object.defineProperty(n,Symbol.hasInstance,{value:function(z){return this===n?z&&!!z[o]:F.call(this,z)}})}if(h.prototype[u.ref])return h;if(u.uses)for(let o of u.uses)L(h,o);u.augments.add(h),f.uses||=[],f.uses.push(n);let r=Object.getOwnPropertyDescriptors(n.prototype);if(delete r.constructor,r[S])f.inits||=[],f.inits.push(n.prototype[S]),delete r[S];if(Object.defineProperties(h.prototype,r),n?.mixed instanceof Function)n.mixed(h);return h}var P={cache:{},self:null,target:null,proxy:new Proxy({},{apply:(h,n,...f)=>{return P.target[n].apply(P.self,f)},get:(h,n)=>{return Reflect.get(P.target,n,P.self)},set:(h,n,f,u)=>{return Reflect.set(P.target,n,f,P.self)}})};function N(h,n,f,u,r=null){let o=Object.getPrototypeOf(h.prototype),F=u&G.HasMixins,z;if(F)U.set(h,U.get(o.constructor)),o=Object.getPrototypeOf(o);if(r){let v=u&G.IsObjectExtension?r:r.prototype,T=E(h);if(T.uses){if(r===v)console.warn("Cannot extend object with mixins");for(let B of T.uses)L(r,B)}if(u&G.HasSuperCalls)P.cache[n]=Object.create(Object.getPrototypeOf(v),Object.getOwnPropertyDescriptors(v));return J(v,h.prototype),r}let w=o?.constructor;if(z=E(h,{symbol:n}),Object.defineProperty(h,X,{value:z,enumerable:!1,configurable:!0}),f&&h.name!==f)Object.defineProperty(h,"name",{value:f,configurable:!0});if(z.flags=u,u&G.HasConstructor)h.prototype[I]=n;if(z.uses)for(let v of z.uses)v.mixes?.(h);if(w?.inherited instanceof Function)w.inherited(h);return h}function Q(h,n){if(h[I]===n)h[W]?.(),h[H]&&h[H].inited(h)}var R=Symbol.for("#active"),V=Symbol();class D{[S](h=null,n=!0,f=!0){var u;this.onready=h?h.onready:void 0,this.onerror=h?h.onerror:void 0,this.onchange=h?h.onchange:void 0,this.languages=h&&(u=h.languages)!==void 0?u:{},this.preferred=h&&(u=h.preferred)!==void 0?u:(window?.navigator?.language||"en-US").slice(0,2),this[R]=h?h[R]:void 0,this.default=h?h.default:void 0}constructor(h,n="en"){var f=this;return this[S](),this.default=n,window.fetch(h).then(function(u){return u.json()}).then(function(u){return f._finalize(u,void 0)}).catch(function(u){return f._finalize(void 0,u)}),new Proxy(this,{get:function(u,r,o){if(f[r])return Reflect.get(u,r,o);if(u.languages[r])return u.languages[r];if(u.languages[f.active]&&u.languages[f.active][r])return u.languages[f.active][r];if(q(f.onerror,Function))f.onerror("no_localization_key",r);return""}});Q(this,V)}_finalize(h,n){if(n){if(q(this.onerror,Function))this.onerror("no_localization_file",n);else console.log("Localization file was not loaded",n);return}if(h)this.languages=h;if(!this.languages[this.default]){if(q(this.onerror,Function))this.onerror("no_default_localization",this.default);else console.log("There is no Localization for the default language",this.default);return}if(this.active=this.preferred,q(this.onready,Function))return this.onready()}get active(){return this.languages[this[R]]?this[R]:this.default}set active(h){if(h&&this.languages[h])this[R]=h;else this[R]=this.languages[this.preferred]?this.preferred:this.default;if(q(this.onchange,Function))this.onchange(this[R])}static{N(this,V,"Localization",16)}}export{D as Localization};
1
+ var S=Symbol.for("#__init__"),E=Symbol.for("#__initor__"),Q=Symbol.for("#__inited__"),B=Symbol.for("#__hooks__"),A=Symbol.for("#__patch__"),D=Symbol.for("#has"),V=Symbol.for("#meta"),W=Symbol.for("imba"),O=Symbol.for("#__mixin__"),K=Symbol.for("#matcher"),M=Symbol.for("#L"),i=Symbol.for("#appendChild"),C=Symbol.for("#afterVisit"),j=Symbol.for("#beforeReconcile"),g=Symbol.for("#afterReconcile"),d=Symbol.for("##up");var q={IsExtension:1,IsTag:2,HasDescriptors:4,HasSuperCalls:8,HasConstructor:16,HasFields:32,HasMixins:64,HasInitor:128,HasDecorators:256,IsObjectExtension:512},P=new Map,X=globalThis[W]||={counter:0,classes:P};function U(h,n={}){return P.has(h)||P.set(h,{symbol:Symbol(h.name),parent:Object.getPrototypeOf(h.prototype)?.constructor,for:h,uses:null,inits:null,id:X.counter++,...n}),P.get(h)}function R(h,n){return typeof n==="string"?typeof h===n:n?.[Symbol.hasInstance]?.(h)}function Y(h,n){if(!h||!n)return!1;if(h.get)return n.get===h.get;if(h.set)return n.set===h.set;if(h.value)return h.value===n.value}function G(h,n,r,u={}){let o=h.constructor;if(!r&&n){if(r=Object.getOwnPropertyDescriptors(n),delete r.constructor,r[S])console.warn(`Cannot define plain fields when extending class ${o.name}`),delete r[S]}let z=U(o);if(z&&z.augments){let I=new Map;for(let f of Object.keys(r)){let v=Object.getOwnPropertyDescriptor(h,f);for(let w of z.augments){let F=I.get(w);F||I.set(w,F={});let T=Object.getOwnPropertyDescriptor(w.prototype,f);if(T&&!Y(v,T))console.warn("wont extend",f,T,v);else F[f]=r[f]}}for(let[f,v]of I)if(Object.keys(v).length)G(f.prototype,null,v)}return Object.defineProperties(h,r),h}function H(h,n){let r=U(h),u=U(n);if(u.parent){if(!(h.prototype instanceof u.parent))throw new Error(`Mixin ${n.name} has superclass not present in target class`)}if(!u.augments){u.augments=new Set;let z=u.ref=Symbol(n.name),I=Object[Symbol.hasInstance];n.prototype[z]=!0,Object.defineProperty(n,Symbol.hasInstance,{value:function(f){return this===n?f&&!!f[z]:I.call(this,f)}})}if(h.prototype[u.ref])return h;if(u.uses)for(let z of u.uses)H(h,z);u.augments.add(h),r.uses||=[],r.uses.push(n);let o=Object.getOwnPropertyDescriptors(n.prototype);if(delete o.constructor,o[S])r.inits||=[],r.inits.push(n.prototype[S]),delete o[S];if(Object.defineProperties(h.prototype,o),n?.mixed instanceof Function)n.mixed(h);return h}var c={cache:{},self:null,target:null,proxy:new Proxy({},{apply:(h,n,...r)=>{return c.target[n].apply(c.self,r)},get:(h,n)=>{return Reflect.get(c.target,n,c.self)},set:(h,n,r,u)=>{return Reflect.set(c.target,n,r,c.self)}})};function J(h,n,r,u,o=null){let z=Object.getPrototypeOf(h.prototype),I=u&q.HasMixins,f;if(I)P.set(h,P.get(z.constructor)),z=Object.getPrototypeOf(z);if(o){let w=u&q.IsObjectExtension?o:o.prototype,F=U(h);if(F.uses){if(o===w)console.warn("Cannot extend object with mixins");for(let T of F.uses)H(o,T)}if(u&q.HasSuperCalls)c.cache[n]=Object.create(Object.getPrototypeOf(w),Object.getOwnPropertyDescriptors(w));return G(w,h.prototype),o}let v=z?.constructor;if(f=U(h,{symbol:n}),Object.defineProperty(h,V,{value:f,enumerable:!1,configurable:!0}),r&&h.name!==r)Object.defineProperty(h,"name",{value:r,configurable:!0});if(f.flags=u,u&q.HasConstructor)h.prototype[E]=n;if(f.uses)for(let w of f.uses)w.mixes?.(h);if(v?.inherited instanceof Function)v.inherited(h);return h}function L(h,n){if(h[E]===n)h[Q]?.(),h[B]&&h[B].inited(h)}var N=Symbol();class Z{[S](h=null,n=!0,r=!0){var u;this.onready=h?h.onready:void 0,this.onerror=h?h.onerror:void 0,this.onchange=h?h.onchange:void 0,this.languages=h&&(u=h.languages)!==void 0?u:{},this.preferred=h&&(u=h.preferred)!==void 0?u:(window?.navigator?.language||"en-US").slice(0,2),this.default=h?h.default:void 0}constructor(h,n="en"){var r=this;return this[S](),this.default=n,window.fetch(h).then(function(u){return u.json()}).then(function(u){return r._finalize(u,void 0)}).catch(function(u){return r._finalize(void 0,u)}),new Proxy(this,{get:function(u,o,z){if(r[o])return Reflect.get(u,o,z);if(u.languages[o])return u.languages[o];if(u.languages[r.active]&&u.languages[r.active][o])return u.languages[r.active][o];if(R(r.onerror,Function))r.onerror("no_localization_key",o);return""}});L(this,N)}_finalize(h,n){if(n){if(R(this.onerror,Function))this.onerror("no_localization_file",n);else console.log("Localization file was not loaded",n);return}if(h)this.languages=h;if(!this.languages[this.default]){if(R(this.onerror,Function))this.onerror("no_default_localization",this.default);else console.log("There is no Localization for the default language",this.default);return}if(this.active=this.preferred,R(this.onready,Function))return this.onready()}get active(){let h=window.localStorage.getItem("imba-localization");if(h&&this.languages[h])return h;if(this.languages[this.preferred])return this.preferred;return this.default}set active(h){if(h=h&&this.languages[h]?h:this.active,window.localStorage.getItem("imba-localization")!=h){if(window.localStorage.setItem("imba-localization",h),R(this.onchange,Function))this.onchange(h)}}static{J(this,N,"Localization",16)}}export{Z as Localization};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imba-localization",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/HeapVoid/imba-localization.git"