-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguageHelper.js
258 lines (237 loc) · 8.39 KB
/
languageHelper.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import DataStorage from './DataStorage'
import storage from '../utils/storageHelper'
import {
clearClutter,
downloadFile,
fallbackIfFails,
generateHash,
getUrlParam,
isNodeJS,
isStr,
textCapitalize,
} from './utils'
export const translations = new DataStorage('totem_static_translations')
// language the app texts are written
export const APP_LANG = fallbackIfFails(() => process.env.REACT_APP_LANG) || 'EN'
export const MODULE_KEY = 'language'
const rw = value => storage.settings.module(MODULE_KEY, value) || {}
let _selected = rw().selected || APP_LANG
const _window = fallbackIfFails(() => window, [], {})
export const BUILD_MODE = isNodeJS()
? process.env.BUILD_MODE
: getUrlParam('build-mode', _window.location.href)
.toLowerCase() === 'true'
&& _window.location.hostname !== 'totem.live'
export const languages = Object.freeze({
// AR: 'Arabic - عربي',
BN: 'Bengali - বাংলা',
DE: 'German - Deutsch',
EN: 'English',
ES: 'Spanish - Español',
FR: 'French - Français',
HI: 'Hindi - हिन्दी',
ID: 'Indonesian - Bahasa Indonesia',
IT: 'Italian - Italiano',
JA: 'Japanese - 日本',
KO: 'Korean - 한국인',
NL: 'Dutch - Nederlandse Taal',
PL: 'Polish - Polski',
PT: 'Portuguese - Português',
RU: 'Russian - Русский',
TR: 'Turkish - Türkçe',
UK: 'Ukrainian - українська',
VI: 'Vietnamese - Tiếng Việt',
ZH: 'Chinese - 中国人',
})
const digits = {
// AR: ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'],
// BN: ['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯'],
// HI: ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'],
// ZH: ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'],
}
export const digitsTranslated = (texts = {}, langCode = getSelected()) => !digits[langCode]
? texts
: new Proxy(texts, {
get: (self, key) => `${self[key]}`.replace(
/[0-9]/g,
n => digits[langCode][n],
),
})
// downloadTextListCSV generates a CSV file with all the unique application texts
// that can be used to translate by opening the file in Google Drive
// NB: this function should not be used when BUILD_MODE is false (URL param 'build-mode' not 'true')
export const downloadTextListCSV = !BUILD_MODE ? null : () => {
const seperator = ','
const langCodes = [
APP_LANG,
...Object
.keys(languages)
.filter(x => x != APP_LANG),
]
const rest = langCodes.slice(1)
const cols = 'abcdefghijklmnopqrstuvwxyz'
.repeat(5)
.toUpperCase()
.split('')
const maxRows = _window.enList.length + 1
// use batch functions so that translation request is only executed once.
// only the first data cell in each column needs this function.
// To avoid being rate limited, manuall set "=" when opening in Google Sheets
const getRowTranslateFunction = colName =>
`BYROW(A2:INDEX(A:A, ${maxRows}), LAMBDA(x, GOOGLETRANSLATE(x, A1, ${colName}1)))`
//
// `=BYROW(A2:INDEX(A:A, MAX((A:A<>"")*ROW(A:A))), LAMBDA(x, GOOGLETRANSLATE(x, A1, ${colName}1)))`
const str = langCodes.join(seperator) + '\n' + (_window.enList || []).map((x, i) => {
// const rowNo = i + 2
// const functions = rest.map((_, c) => `"=GOOGLETRANSLATE($A${rowNo}, $A$1, ${cols[c + 1]}$1)"`).join(',')
const functions = i >= 1
? langCodes.map(_ => '') // empty cells
: rest.map((_, j) =>
`"${getRowTranslateFunction(cols[j + 1])}"`
)
return `"${clearClutter(x)}"${seperator}${functions.join(seperator)}`
}).join(',\n')
downloadFile(str, `English-texts-${new Date().toISOString()}.csv`, 'text/csv')
}
/**
* @name fetchNSaveTexts
* @summary retrieve and cache English and translated texts based on selected language
*
* @param {Object} client Messaging server client
*
* @returns {Boolean} true: data freshly updated. Falsy: using cache or update not required
*/
export const fetchNSaveTexts = async (client = require('./chatClient').default) => {
if (!client) return console.trace('ChatClient not specified')
const selected = getSelected()
if (selected === APP_LANG) {
setTexts(selected, null, null)
return
}
const selectedHash = generateHash(getTexts(selected) || '')
const engHash = generateHash(getTexts(APP_LANG) || '')
const func = client.languageTranslations
const [textsEn, texts = textsEn] = await Promise.all([
func(APP_LANG, engHash),
APP_LANG === selected
? undefined
: func(selected, selectedHash),
])
// update not required => existing list of language is exactly the same as in the database
if (!texts && !textsEn) return
console.log('Language text list updated', { selected, texts, textsEn })
// save only if update required
setTexts(selected, texts, textsEn)
// success
return true
}
// get selected language code
export const getSelected = () => _selected
export const getTexts = langCode => translations.get(langCode)
/**
* @name setSelected
* @summary set selected language code and retrieve translated texts (if required and `client` is supplied)
*
* @param {String} selected
* @param {Object} client Messaging server client
*
* @returns {Array}
*/
export const setSelected = async (selected, client) => {
rw({ selected })
_selected = selected
// retrieve translated texts from server
client ??= require('./chatClient').default
const listUpdated = await fetchNSaveTexts(client)
return listUpdated
}
// save translated list of texts retrieved from server
export const setTexts = (langCode, texts, enTexts) => translations.setAll(
new Map(
// remove all language cache if selected is English
langCode === APP_LANG
? []
: [
[APP_LANG, enTexts || translations.get(APP_LANG)],
[langCode, texts || translations.get(langCode)],
].filter(Boolean)
),
true,
)
class Translated extends String {
constructor(text) {
super(text)
}
toLowerCase() { return `${this}`.toLowerCase() }
}
_window.Translated = Translated
export const translated = (
texts = {},
capitalized = false,
fullSentence,
forceLowercase,
) => {
if (isStr(texts)) {
const result = translated({ texts }, capitalized)
return result[capitalized ? 1 : 0].texts
}
// gets rid of extra spacing etc.
Object
.keys(texts)
.forEach(key => {
texts[key] = clearClutter(texts[key])
})
const langCode = getSelected()
if (langCode !== APP_LANG || BUILD_MODE) {
const en = translations.get(APP_LANG) || []
// list of selected language texts
const selected = translations.get(langCode) || []
// attempt to build a single list of english texts for translation
if (BUILD_MODE) {
_window.enList = _window.enList || []
Object.values(texts).forEach(text => {
if (!text) return
// text = clearClutter(text)
enList.indexOf(text) === -1 && enList.push(text)
})
_window.enList = enList.sort()
}
Object
.keys(texts)
.forEach(key => {
if (!texts[key]) return
const text = texts[key]
const enIndex = en.indexOf(text)
const translatedText = selected[enIndex]
// fall back to original/English,
// if selected language is not supported
// or due to network error language data download failed
// or somehow supplied text wasn't translated
if (!translatedText) return
texts[key] = translatedText
})
} else {
}
texts = digitsTranslated(texts, langCode)
if (capitalized) {
const textsNoCaps = { ...texts }
capitalized = digitsTranslated(
textCapitalize(
texts,
fullSentence,
forceLowercase,
),
langCode,
)
texts = textsNoCaps
}
return [texts, capitalized]
}
export default {
digitsTranslated,
translations,
translated,
setTexts,
getSelected,
setSelected,
}