1
+ /*
2
+ Copyright 2022 The Matrix.org Foundation C.I.C.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
1
17
const en = require ( "../src/i18n/strings/en_EN" ) ;
2
18
const de = require ( "../src/i18n/strings/de_DE" ) ;
3
19
const lv = {
4
20
"Save" : "Saglabāt" ,
5
21
"Uploading %(filename)s and %(count)s others|one" : "Качване на %(filename)s и %(count)s друг" ,
6
22
} ;
7
23
24
+ function weblateToCounterpart ( inTrs ) {
25
+ const outTrs = { } ;
26
+
27
+ for ( const key of Object . keys ( inTrs ) ) {
28
+ const keyParts = key . split ( '|' , 2 ) ;
29
+ if ( keyParts . length === 2 ) {
30
+ let obj = outTrs [ keyParts [ 0 ] ] ;
31
+ if ( obj === undefined ) {
32
+ obj = outTrs [ keyParts [ 0 ] ] = { } ;
33
+ } else if ( typeof obj === "string" ) {
34
+ // This is a transitional edge case if a string went from singular to pluralised and both still remain
35
+ // in the translation json file. Use the singular translation as `other` and merge pluralisation atop.
36
+ obj = outTrs [ keyParts [ 0 ] ] = {
37
+ "other" : inTrs [ key ] ,
38
+ } ;
39
+ console . warn ( "Found entry in i18n file in both singular and pluralised form" , keyParts [ 0 ] ) ;
40
+ }
41
+ obj [ keyParts [ 1 ] ] = inTrs [ key ] ;
42
+ } else {
43
+ outTrs [ key ] = inTrs [ key ] ;
44
+ }
45
+ }
46
+
47
+ return outTrs ;
48
+ }
49
+
8
50
// Mock the browser-request for the languageHandler tests to return
9
51
// Fake languages.json containing references to en_EN, de_DE and lv
10
52
// en_EN.json
@@ -13,7 +55,7 @@ const lv = {
13
55
module . exports = jest . fn ( ( opts , cb ) => {
14
56
const url = opts . url || opts . uri ;
15
57
if ( url && url . endsWith ( "languages.json" ) ) {
16
- cb ( undefined , { status : 200 } , JSON . stringify ( {
58
+ cb ( undefined , { status : 200 } , JSON . stringify ( {
17
59
"en" : {
18
60
"fileName" : "en_EN.json" ,
19
61
"label" : "English" ,
@@ -24,16 +66,16 @@ module.exports = jest.fn((opts, cb) => {
24
66
} ,
25
67
"lv" : {
26
68
"fileName" : "lv.json" ,
27
- "label" : "Latvian"
28
- }
69
+ "label" : "Latvian" ,
70
+ } ,
29
71
} ) ) ;
30
72
} else if ( url && url . endsWith ( "en_EN.json" ) ) {
31
- cb ( undefined , { status : 200 } , JSON . stringify ( en ) ) ;
73
+ cb ( undefined , { status : 200 } , JSON . stringify ( weblateToCounterpart ( en ) ) ) ;
32
74
} else if ( url && url . endsWith ( "de_DE.json" ) ) {
33
- cb ( undefined , { status : 200 } , JSON . stringify ( de ) ) ;
75
+ cb ( undefined , { status : 200 } , JSON . stringify ( weblateToCounterpart ( de ) ) ) ;
34
76
} else if ( url && url . endsWith ( "lv.json" ) ) {
35
- cb ( undefined , { status : 200 } , JSON . stringify ( lv ) ) ;
77
+ cb ( undefined , { status : 200 } , JSON . stringify ( weblateToCounterpart ( lv ) ) ) ;
36
78
} else {
37
- cb ( true , { status : 404 } , "" ) ;
79
+ cb ( true , { status : 404 } , "" ) ;
38
80
}
39
81
} ) ;
0 commit comments