forked from digitalfabrik/integreat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
260 lines (240 loc) · 6.23 KB
/
config.ts
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
259
260
type FontType =
| 'notoSans'
| 'raleway'
| 'varelaRound'
| 'noto-sans-sc' // https://www.google.com/get/noto/help/cjk/
| 'noto-sans-georgian'
| 'noto-sans-arabic'
export type UiDirectionType = 'rtl' | 'ltr'
type LanguageType = {
rtl: boolean
additionalFont?: FontType
}
type SupportedLanguagesType = Record<string, LanguageType>
type FallbacksType = Record<string, string[]>
class Config {
// The language from which we translate
sourceLanguage = 'de'
// The languages into which we translate from 'sourceLanguage' including the sourceLanguage
// See https://wiki.tuerantuer.org/integreat-languages and https://iso639-3.sil.org/code_tables/639/data
supportedLanguages: SupportedLanguagesType = {
de: {
rtl: false,
},
ar: {
// Lateef for arabic ui and content, Open Sans for latin text in arabic text, Raleway for latin ui
rtl: true,
additionalFont: 'noto-sans-arabic',
},
en: {
rtl: false,
},
pes: {
rtl: true,
additionalFont: 'noto-sans-arabic',
},
fr: {
rtl: false,
},
ro: {
rtl: false,
},
tr: {
rtl: false,
},
pl: {
rtl: false,
},
ti: {
rtl: false,
},
ckb: {
rtl: true,
additionalFont: 'noto-sans-arabic',
},
ru: {
rtl: false,
},
so: {
rtl: false,
},
hr: {
rtl: false,
},
es: {
rtl: false,
},
'sr-Latn': {
rtl: false,
},
'sr-Cyrl': {
rtl: false,
},
ps: {
rtl: true,
},
kmr: {
rtl: false,
},
am: {
rtl: false,
},
bg: {
rtl: false,
},
el: {
rtl: false,
},
it: {
rtl: false,
},
'zh-CN': {
rtl: false,
additionalFont: 'noto-sans-sc',
},
mk: {
rtl: false,
},
sq: {
rtl: false,
},
ka: {
rtl: false,
additionalFont: 'noto-sans-georgian',
},
prs: {
rtl: true,
additionalFont: 'noto-sans-arabic',
},
hu: {
rtl: false,
},
ur: {
rtl: true,
additionalFont: 'noto-sans-arabic',
},
uk: {
rtl: false,
},
fi: {
rtl: false,
},
nl: {
rtl: false,
},
pt: {
rtl: false,
},
cs: {
rtl: false,
},
sk: {
rtl: false,
},
orm: {
rtl: false,
},
da: {
rtl: false,
},
rom: {
rtl: false,
},
}
// Fallbacks for unnormalized language codes from our backend
fallbacks: FallbacksType = {
ku: ['kmr'],
fa: ['pes'],
'fa-AF': ['prs'],
fa_pr: ['pes'],
'de-si': ['de'],
sr: ['sr-Cyrl'],
'pt-br': ['pt'],
'zh-hans': ['zh-CN'],
// Slugs from the CMS are (and have to be) lowercase
'sr-cyrl': ['sr-Cyrl'],
'sr-latn': ['sr-Latn'],
'zh-cn': ['zh-CN'],
}
defaultFallback = 'de' // If the language code is not found in our translations then use this
constructor() {
this.checkConsistency()
}
getSupportedLanguageTags(): string[] {
return Object.keys(this.supportedLanguages)
}
/**
* Returns the passed languageTag if it is supported or that of a supported fallback or undefined if not supported
*/
getLanguageTagIfSupported(languageTag: string): string | undefined {
return this.getSupportedLanguageTags().find(
key => key === languageTag || this.fallbacks[languageTag]?.includes(key),
)
}
getSupportedLanguage(languageTag: string): LanguageType | undefined {
const fallbacks = this.fallbacks[languageTag]
if (fallbacks) {
const found = fallbacks.find(fallback => !!this.supportedLanguages[fallback])
if (found) {
return this.supportedLanguages[found]
}
}
return this.supportedLanguages[languageTag]
}
isSupportedLanguage(languageTag: string): boolean {
return !!this.getSupportedLanguage(languageTag)
}
/**
* Checks whether the languageTag has an RTL script. This decision is made by the project "Integreat".
* Writing direction is not an attribute of "language", but of "scripts". That means that there are languages which
* can have RTL and LTR scripts.
*
* <b>Typical Usage</b>
*
* If you do not know whether to display UI elements RTL/LTR you can use this method. Browsers for example, do not
* disclose the system script direction. On Android and iOS system libraries are used instead of this method
* ({@link https://github.com/zoontek/react-native-localize/blob/de9c01ab99f69bcf655ed2cb83c7081b75298bd2/android/src/main/java/com/zoontek/rnlocalize/RNLocalizeModule.java#L191|Android})
* ({@link https://github.com/zoontek/react-native-localize/blob/d8f265ee665cf593f98ef92b308f4416cd251b30/ios/RNLocalize.m#L113|iOS}).
* This only works for {@link #supportedLanguages} and not for arbitrary ones in which case we just return false.
* This is because we simply do now know it and cannot know it.
*
* @see http://www.i18nguy.com/temp/rtl.html
* @param languageTag for the check
* @returns {*} whether script is RTL
*/
hasRTLScript(languageTag: string): boolean {
const language = this.getSupportedLanguage(languageTag)
if (!language) {
return false
}
return language.rtl
}
getScriptDirection(languageTag: string): UiDirectionType {
return this.hasRTLScript(languageTag) ? 'rtl' : 'ltr'
}
getAdditionalFont(languageTag: string): FontType | null | undefined {
return this.getSupportedLanguage(languageTag)?.additionalFont
}
getFallbackLanguageTags(): string[] {
return Object.keys(this.fallbacks)
}
getFallbackTargetLanguageTags(): string[] {
const languageTags: string[] = []
const fallbacks: string[][] = Object.values(this.fallbacks)
fallbacks.forEach((languagesInFallbacks: string[]) => {
languagesInFallbacks.forEach((languageTag: string) => {
languageTags.push(languageTag)
})
})
return languageTags
}
checkConsistency() {
const supportedLanguageTags = this.getSupportedLanguageTags()
this.getFallbackTargetLanguageTags().forEach((languageTag: string) => {
if (!supportedLanguageTags.includes(languageTag)) {
throw Error(`The code ${languageTag} was mentioned in the fallbacks but is not included in 'targetLanguage'`)
}
})
}
}
export default new Config()