-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocaleKeyboard.js
86 lines (68 loc) · 2.09 KB
/
LocaleKeyboard.js
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
85
/*
* The lib creator/compiler to make the special lib.
*/
/*jshint esversion: 6 */
/* Know this script URL */
var scripts = document.getElementsByTagName('script');
var scriptDOM = scripts[scripts.length - 1];
var scriptUrl = scriptDOM.src;
/* Function to request files */
function getFile(sUrl) {
// Init request
var oReq = new XMLHttpRequest();
// Sending request
oReq.open("get", sUrl, false);
oReq.overrideMimeType("text/plain");
oReq.send(null);
// Getting response
if (oReq.readyState == 4 && (oReq.status == 200 || oReq.status === 0)) {
return oReq.responseText;
} else {
return undefined;
}
}
/* Main class */
class LocaleKeyboard {
constructor() {
/* Create the baseUrl */
this.baseUrl = scriptUrl.substring(0, scriptUrl.lastIndexOf("/"));
/* Locale is not set */
this.localeSet = false;
}
setLocale(lang) {
/* Define lang */
this.lang = lang;
/* Check for lang existence in localeList */
if (this.listLocales().indexOf(this.lang) == -1) {
console.error("Error: This locale doesn't exist !");
/* Locale is not set */
this.localeSet = false;
} else {
/* Locale is set */
this.localeSet = true;
}
}
getSource() {
if (!this.localeSet) {console.error("Error: Locale is not set !"); return undefined;}
/* Get files */
var langFile = getFile(this.baseUrl + "/locales/" + this.lang + ".lang");
var libPartOne = getFile(this.baseUrl + "/src/Keyboard-@1.cpp");
var libPartTwo = getFile(this.baseUrl + "/src/Keyboard-@2.cpp");
/* Just return the modified lib */
return libPartOne + langFile + libPartTwo;
}
getHeader() {
if (!this.localeSet) {console.error("Error: Locale is not set !"); return undefined;}
/* Just return the header */
return getFile(this.baseUrl + "/src/Keyboard.h");
}
listLocales() {
/* List all files *.lang in locales/ dir */
if (!this.localeArray) {
this.localeArray = getFile(this.baseUrl + "/locales/localeList").split('\n');
this.localeArray.pop();
}
/* Return the list */
return this.localeArray;
}
}