-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvox.js
131 lines (120 loc) · 3.46 KB
/
vox.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Vox = Voice in Latin
let vox = {}
vox.dex = null // this is the dictionary
vox.pluralisation = null // this is a function for the plural form
vox.debug = false
// Please add wanted language to .mapPlurals
// And also document yourself and create the plural function.
// https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
vox.mapPlurals = [
{
langs: ["jp", "vi", "ko", "th"],
plural: () => 0
},
{
langs: ["en", "de", "nl", "sv", "da", "no", "es", "pt", "it", "gr", "bg", "fi", "et", "eo", "hu", "tk"],
plural: (n) => (n != 1 ? 1 : 0)
},
{
langs: ["br", "fr"],
plural: (n) => (n > 1 ? 1 : 0)
}
]
vox.initPluralisation = function (arg_lang = null) {
let OK = false
let crt_lang = arg_lang || navigator.language
for (let group of vox.mapPlurals) {
for (let lang of group.langs) {
if (crt_lang.includes(lang)) {
OK = true
vox.pluralisation = group.plural
vox.lang = lang
}
}
if (OK) break
}
}
vox.fetchDict = async function (URL) {
// https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
let res = await fetch(URL)
.then((res) => res.json())
.catch(() => {
console.error("Could not fetch the translation from", URL)
return null // if error vox.dex is null
})
return res
}
vox._t = function () {
/**
* This function translates single text/paragraphs/words
* usage:
* _t('grandpa drinks beer') = "おじいちゃんはビールを飲みます"
*/
let a1 = arguments[0]
if (vox.dex === null || vox.dex === undefined) {
return a1
}
let trans = ""
if (a1 in vox.dex.singular) {
trans = vox.dex.singular[a1]
} else {
trans = a1
}
vox.debug && console.log("_t--trans:", trans)
return trans
}
vox._tc = function () {
/**
* This function add a context to the text/word/s.
* First argument is context, second is the text/word/s
* usage:
* _tc("pc-file","file")
* _tc("paper","file")
*/
let ct = arguments[0] // context
let p1 = arguments[1] // translation text
let trans = ""
if (vox.dex === null || vox.dex === undefined) {
return p1
}
if (ct in vox.dex.context) {
trans = vox.dex.context[ct][p1]
}
if (!trans) {
trans = p1
}
vox.debug && console.log("_tc-trans:", trans)
return trans
}
vox._t2 = function () {
/**
* This function is used to add pluralisation to the text.
* First 2 arguments, are the text, singular & plural
* '%s' will be replaced with the 3rd argument
* Usage ( for en ):
* _t2('I have %s apple','I have %s apples, 1) => Will translate to : "I have 1 apple"
* _t2('I have %s apple','I have %s apples, 2) => Will translate to : "I have 2 apples"
*/
if (arguments.length !== 3) {
console.error("Empty invokation _t2 ->", new Error().stack.split("\n")[1])
vox.debug && console.trace()
return
}
let sg = arguments[0] // singular
let pl = arguments[1] // plural
let n = arguments[2] // the number
let np = vox.pluralisation(n) // get pluralisation : 0 or 1 or 2 etc
let trans = ""
if (vox.dex && sg in vox.dex.plural && vox.dex.plural[sg][np] !== undefined && vox.dex.plural[sg][np] !== "") {
trans = vox.dex.plural[sg][np]
} else {
// as fallback or no match
// english pluralisation -> plural: n!=1
trans = n != 1 ? pl : sg
}
trans = trans.replace("%s", n)
vox.debug && console.log("_t2-trans:", trans)
return trans
}
// comment next line if used with vanilla javascript
export default vox