imba-localization 0.1.7 → 0.1.9

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'
@@ -37,15 +36,17 @@ export class Localization
37
36
  else
38
37
  console.log('There is no Localization for the default language', default)
39
38
  return
40
- active = preferred
41
39
  onready! if onready isa Function
42
40
 
43
41
  get active
44
- return languages[#active] ? #active : default
42
+ const saved = window.localStorage.getItem('imba-localization')
43
+ return saved if saved and languages[saved]
44
+ return preferred if languages[preferred]
45
+ return default
45
46
 
46
47
  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
48
+ name = name and languages[name] ? name : active
49
+ if window.localStorage.getItem('imba-localization') != name
50
+ window.localStorage.setItem('imba-localization', name)
51
+ onchange(name) if onchange isa Function
52
+
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"),C=Symbol.for("#appendChild"),i=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},R=new Map,X=globalThis[W]||={counter:0,classes:R};function c(h,n={}){return R.has(h)||R.set(h,{symbol:Symbol(h.name),parent:Object.getPrototypeOf(h.prototype)?.constructor,for:h,uses:null,inits:null,id:X.counter++,...n}),R.get(h)}function T(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,o,u={}){let r=h.constructor;if(!o&&n){if(o=Object.getOwnPropertyDescriptors(n),delete o.constructor,o[S])console.warn(`Cannot define plain fields when extending class ${r.name}`),delete o[S]}let z=c(r);if(z&&z.augments){let I=new Map;for(let f of Object.keys(o)){let v=Object.getOwnPropertyDescriptor(h,f);for(let w of z.augments){let P=I.get(w);P||I.set(w,P={});let U=Object.getOwnPropertyDescriptor(w.prototype,f);if(U&&!Y(v,U))console.warn("wont extend",f,U,v);else P[f]=o[f]}}for(let[f,v]of I)if(Object.keys(v).length)G(f.prototype,null,v)}return Object.defineProperties(h,o),h}function H(h,n){let o=c(h),u=c(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),o.uses||=[],o.uses.push(n);let r=Object.getOwnPropertyDescriptors(n.prototype);if(delete r.constructor,r[S])o.inits||=[],o.inits.push(n.prototype[S]),delete r[S];if(Object.defineProperties(h.prototype,r),n?.mixed instanceof Function)n.mixed(h);return h}var F={cache:{},self:null,target:null,proxy:new Proxy({},{apply:(h,n,...o)=>{return F.target[n].apply(F.self,o)},get:(h,n)=>{return Reflect.get(F.target,n,F.self)},set:(h,n,o,u)=>{return Reflect.set(F.target,n,o,F.self)}})};function J(h,n,o,u,r=null){let z=Object.getPrototypeOf(h.prototype),I=u&q.HasMixins,f;if(I)R.set(h,R.get(z.constructor)),z=Object.getPrototypeOf(z);if(r){let w=u&q.IsObjectExtension?r:r.prototype,P=c(h);if(P.uses){if(r===w)console.warn("Cannot extend object with mixins");for(let U of P.uses)H(r,U)}if(u&q.HasSuperCalls)F.cache[n]=Object.create(Object.getPrototypeOf(w),Object.getOwnPropertyDescriptors(w));return G(w,h.prototype),r}let v=z?.constructor;if(f=c(h,{symbol:n}),Object.defineProperty(h,V,{value:f,enumerable:!1,configurable:!0}),o&&h.name!==o)Object.defineProperty(h,"name",{value:o,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,o=!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 o=this;return this[S](),this.default=n,window.fetch(h).then(function(u){return u.json()}).then(function(u){return o._finalize(u,void 0)}).catch(function(u){return o._finalize(void 0,u)}),new Proxy(this,{get:function(u,r,z){if(o[r])return Reflect.get(u,r,z);if(u.languages[r])return u.languages[r];if(u.languages[o.active]&&u.languages[o.active][r])return u.languages[o.active][r];if(T(o.onerror,Function))o.onerror("no_localization_key",r);return""}});L(this,N)}_finalize(h,n){if(n){if(T(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(T(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(T(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),T(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.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/HeapVoid/imba-localization.git"