-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabel.js
More file actions
84 lines (65 loc) · 2.26 KB
/
babel.js
File metadata and controls
84 lines (65 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Babel est un localizer pour les applications en javascript
*
* Il tire son nom de la tour du même nom dans le folklore hébraique.
* Son utilisation est libre, et sa modification est permise, cependant merci de
* me créditer dans ce cas.
* Son intérêt est de simbplifier la localisation des textes de votre
* application dans un objectif de traduction.
* Pour cela il suffit d'ajouter ce document dans votre projet npm et de
* le require pour accéder à ses méthodes static ainsi qu'au classes
* qu'il propose.
*
* AUTHOR : Com (Comdec35000)
* VERSION : 1.1.1 2021/10/28
*/
const fs = require('fs');
class Babel {
static config = {
langDirPath: __dirname + '/',
langages: [
'fr_fr',
'en_us'
],
defaultLang: 'en_us'
};
/**
*
* @param {String} pos The path to the text in the json. EX: path.to.my.text
* @param {String} lang The lang in witch you want to translate your text.
* @returns {String} The localized text
*/
static localize(pos, lang) {
var file = require(Babel.config.langDirPath + 'lang/' + lang.toLowerCase() + '.json');
var txt = file[pos];
if(!txt) {
file = require(Babel.config.langDirPath + 'lang/' + Babel.config.defaultLang.toLowerCase() + '.json');
txt = file[pos];
}
if(!txt) txt = pos;
return txt;
}
static localizeFormated(pos, lang) {
var text = Babel.localize(pos, lang);
delete arguments[0];
delete arguments[1];
return Babel.format(text, arguments);
}
static format(string) {
let a = string;
delete arguments[0];
Object.values(arguments[1]).forEach((v, i) => {
a = a.replace("<" + i + "/>", v);
});
return a;
}
static genFiles() {
if(!fs.existsSync(Babel.config.langDirPath + 'lang/')) fs.mkdir(Babel.config.langDirPath + 'lang/', err => {
if(err) console.log(err);
});
Babel.config.langages.forEach(file => {
if(!fs.existsSync(Babel.config.langDirPath + 'lang/' + file + '.json')) fs.writeFileSync(Babel.config.langDirPath + 'lang/' + file + '.json', JSON.stringify({}));
});
}
}
module.exports = Babel;