-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30a761d
commit 027e769
Showing
10 changed files
with
577 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
This class return text from the map content given to it. | ||
There are several way copy can be indexed. | ||
map = { | ||
key0: Text in only language, | ||
key1: { | ||
en: "Text in English", | ||
es: "Texto en español", | ||
}, | ||
key2: [{ | ||
en: "Text in English", | ||
es: "Texto en español", | ||
}, { | ||
en: "Next text in English", | ||
es: "Texto siguiente en español", | ||
}] | ||
} | ||
*/ | ||
|
||
class Copy { | ||
|
||
constructor(map = {}) { | ||
this.map = map; | ||
this.lang = Copy.lang; | ||
this.key = Object.keys(this.map)[0]; | ||
this.counter = {}; | ||
} | ||
|
||
add(key, val) { | ||
if (typeof key !== "string") { | ||
return Object.entries(key).forEach(([k, v]) => this.add(k, v)); | ||
} | ||
if (this.map[key]) { | ||
console.error(`Key "${key}" already exists in copy.`); | ||
return "sd"; | ||
} | ||
this.map[key] = val; | ||
return this.get(key); | ||
} | ||
|
||
// returns the text based on key, array index and language | ||
get(key, i) { | ||
if (key === undefined) { | ||
console.error("No key was passed to the copy."); | ||
}; | ||
let val = this.map[key]; | ||
if (val === undefined) { | ||
console.error(`Key "${key}" not found in copy.`); | ||
return ""; | ||
} | ||
this.key = key; | ||
let lang = this.lang; | ||
if (val[lang]) return Copy.treat(val[lang]); | ||
if (typeof val === "string") return Copy.treat(val); | ||
if (!Array.isArray(val)) { | ||
console.error(`Language "${lang}" not found in copy at "${key}".`); | ||
return Copy.treat(val[Object.keys(val)[0]]); | ||
} | ||
if (i === undefined) i = 0; | ||
val = val[i]; | ||
if (val === undefined) return ""; | ||
this.counter[key] = i; | ||
if (val[lang]) return Copy.treat(val[lang]); | ||
if (typeof val === "string") return Copy.treat(val); | ||
console.error(`Language "${lang}" not found in copy at "${key}[${i}]".`); | ||
return Copy.treat(val[Object.keys(val)[0]]); | ||
} | ||
|
||
static treat(s){ | ||
if(!s) return s; | ||
if(Array.isArray(s)) return s.map(i => Copy.treat(i)); | ||
//if(s.includes('\n')) return Copy.treat(s.split('\n')); | ||
return s.replaceAll('—', '<em class="em-dash">--</em>'); | ||
} | ||
|
||
next() { | ||
let i = this.counter[this.key]; | ||
i = i === undefined ? 0 : i + 1; | ||
return this.get(this.key, i); | ||
} | ||
|
||
static copy = new Copy(); | ||
|
||
static add(...args) { | ||
return Copy.copy.add(...args); | ||
} | ||
|
||
static get(...args) { | ||
return Copy.copy.get(...args); | ||
} | ||
|
||
static next() { | ||
return Copy.copy.next(); | ||
} | ||
|
||
static set lang(val) { | ||
localStorage.setItem('copy-lang', val); | ||
location.reload(); | ||
} | ||
|
||
static LANG = { | ||
es: { | ||
code: "es", | ||
name: "Español", | ||
}, | ||
en: { | ||
code: "en", | ||
name: "English", | ||
}, | ||
} | ||
|
||
static get lang() { | ||
let lang = "en";//Copy.LANG.EN.code; | ||
if (navigator && navigator.language) { | ||
lang = navigator.language.split("-")[0]; | ||
} | ||
let savedLang = localStorage.getItem('copy-lang');; | ||
if (savedLang) { | ||
lang = savedLang; | ||
} | ||
return lang; | ||
} | ||
|
||
static getToggleLink(...langs){ | ||
return langs.map(lang => ({ | ||
display: Copy.lang !== lang.code ? "block" : "none", | ||
text: lang.name, | ||
click: () => Copy.lang = lang.code, | ||
})) | ||
} | ||
|
||
} | ||
|
||
export default Copy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,7 @@ DOM.set({ | |
}, | ||
}, | ||
|
||
aside: sponsorsModel, | ||
//aside: sponsorsModel, | ||
|
||
main: { | ||
align: "center", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.