-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-british-english.js
190 lines (127 loc) · 7.14 KB
/
translate-british-english.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
MIT License
Copyright (c) 2019 ididntrealize
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//HOW TO USE THIS SCRIPT
//Do it like this:
/*
var exampleString = 'This product has a beautiful colour, any organisation could benefit from 15m of it. it carries 980ml of water, and weighs 15kg.';
translateBritishEnglish(exampleString);
*/
//This JSON object controls what will be converted into what, and how.
//note that only 'in' will be converted into 'X ft and Y in'
let conversionDetails = {
'ml' : { newUnit: 'oz', multiplier: 0.0352 },
'l' : { newUnit: 'oz', multiplier: 35.195 },
'mm' : { newUnit: 'in', multiplier: 0.0394 },
'cm' : { newUnit: 'in', multiplier: 0.3937 },
'm' : { newUnit: 'in', multiplier: 39.37 },
'g' : { newUnit: 'oz', multiplier: 0.0353 },
'kg' : { newUnit: 'lb', multiplier: 2.2046 }
};
//sometimes the number - unit combinations have special characters appearing directly after, these are accounted for:
let unitPunctuation = [' ', '.', ',', ':', ';', '!', '?', '-', '<'];
//main function will take a description and return it
//without metrics or british english spelling
function translateBritishEnglish(strToTranslate, debug){
//do nothing for null, blank strings, etc.
if(strToTranslate !== null && strToTranslate !== '' && strToTranslate !== undefined){
console.log("==============================")
console.log('The following translations happened:')
let newHTML = strToTranslate;
function translateMeasurements(fullText){
let newStr = fullText;
//collect units to convert from JSON obj above
let unitsToConvert = [];
for(var k in conversionDetails) unitsToConvert.push(k);
if(debug){ console.log("units to convert", unitsToConvert) }
//match any combination of number - unit - random punctuation
let regExStr = "(\\d+\\.?\\d*)+ ?(";
regExStr += unitsToConvert.join("|");
regExStr += ")(";
regExStr += "\\" + unitPunctuation.join("|\\");
regExStr += ")+";
let myRegEx = new RegExp(regExStr, 'gi');
let match = myRegEx.exec(fullText);
//review each match found
while (match != null) {
let replaceThis = match[0]; // 12.56 cm
let origValue = match[1]; // 12.56
let currUnits = match[2]; // cm
let lastThing = match[3]; // <
//using the multiplier from the corresponding unit conversion.
let converted = origValue * conversionDetails[currUnits].multiplier;
//inches needs to change into feet and inches:
if(conversionDetails[currUnits].newUnit == "in"){
if(converted/12 >= 1){
let feet = Math.floor(converted/12);
let inches = Math.round(converted%12);
//over 50 ft, inches don't matter:
if(feet > 10){
converted = feet + "ft ";
}else if(feet >= 1){
converted = Math.round(converted) + " in." ;
}else{
converted = Math.round(converted * 10)/10 + " in." ;
}
}else{
let inches = Math.round(converted);
//if inches would otherwise be 0
if(inches == 0){
converted = "0.5" + " in.";
}else{
converted = inches + " in.";
}
}
}else{
//everything else needs a supplementary rounded decimal situation..
//and corresponding unit abbreviation
converted = Math.round(converted * 10)/10 + " " + conversionDetails[currUnits].newUnit + ".";
}
//let oldNumberAndUnit = ' (' + replaceThis.replace("<", "").replace(" ", "") + ')';
//replace any strange punctuation/ special charaecteres:
converted = converted + lastThing; // 34 in. <
newStr = newStr.replace(replaceThis, converted );
if (debug){ console.log(replaceThis + ' => ' + converted) }
match = myRegEx.exec(fullText);
}
//feeds back original string with any need changes made
return newStr;
}
function translateEnglish(fullText){
let newStr = fullText;
//split all words into an array, discounting HTML tags
let tagsToSpaces = fullText.replace(/(<([^>]+)>)/ig," "),
wordsToCheck = tagsToSpaces.split(' ');
//check each word in passed string with the spelling diff lookup table
wordsToCheck.forEach(function(elem){
let formatted = elem.toLowerCase();
formatted = formatted.replace(/[^a-zA-Z ]/g, "");
if(spellingDifferences[formatted]){
let anythingRevomed = elem.replace(/[a-zA-Z ]/g, "");
//AUS English word found, replace it (with any random formatting associated)
newStr = newStr.replace(elem, spellingDifferences[formatted] + anythingRevomed);
if(debug){ console.log(elem + ' => ' + spellingDifferences[formatted] + anythingRevomed) }
}
});
return newStr;
}
//return the exact string of html other than changed measurements and British English
return translateEnglish(translateMeasurements(newHTML));
}
}