@@ -8,11 +8,33 @@ const DOWNLOADED_TRANSLATIONS_PATH = path.resolve(__dirname, '__translations');
8
8
const DOWNLOADED_TRANSLATIONS_DOCS_PATH = path . resolve (
9
9
__dirname ,
10
10
'__translations' ,
11
- 'docs' ,
11
+ 'test-17' , // TODO (crowdin) This is probably not part of the final export structure
12
+ 'docs'
12
13
) ;
13
14
15
+ const validateConfig = ( { key, threshold, url } ) => {
16
+ const errors = [ ] ;
17
+ if ( ! key ) {
18
+ errors . push ( 'key: No process.env.CROWDIN_API_KEY value defined.' ) ;
19
+ }
20
+ if ( ! Number . isInteger ( threshold ) ) {
21
+ errors . push ( `threshold: Invalid translation threshold defined.` ) ;
22
+ }
23
+ if ( ! url ) {
24
+ errors . push ( 'url: No Crowdin project URL defined.' ) ;
25
+ }
26
+ if ( errors . length > 0 ) {
27
+ console . error ( 'Invalid Crowdin config values for:\n• ' + errors . join ( '\n• ' ) ) ;
28
+
29
+ throw Error ( 'Invalid Crowdin config' ) ;
30
+ }
31
+ } ;
32
+
14
33
function main ( ) {
34
+ validateConfig ( config ) ;
35
+
15
36
const crowdin = new Crowdin ( { apiKey : config . key , endpointUrl : config . url } ) ;
37
+
16
38
process . chdir ( SYMLINKED_TRANSLATIONS_PATH ) ;
17
39
18
40
crowdin
@@ -23,14 +45,13 @@ function main() {
23
45
. then ( locales => {
24
46
const usableLocales = locales
25
47
. filter (
26
- locale => locale . translated_progress > config . translation_threshold ,
48
+ locale => locale . translated_progress > config . threshold ,
27
49
)
28
50
. map ( local => local . code ) ;
29
51
30
52
const localeDirectories = getDirectories (
31
53
DOWNLOADED_TRANSLATIONS_DOCS_PATH ,
32
54
) ;
33
-
34
55
const localeToFolderMap = createLocaleToFolderMap ( localeDirectories ) ;
35
56
36
57
usableLocales . forEach ( locale => {
@@ -43,14 +64,12 @@ function main() {
43
64
// Note that the current working directory of this node process should be where the symlink is created
44
65
// or else the relative paths would be incorrect
45
66
function createSymLink ( folder ) {
46
- symlink ( `../__translations/docs/ ${ folder } ` , folder , err => {
67
+ symlink ( path . resolve ( DOWNLOADED_TRANSLATIONS_DOCS_PATH , folder ) , folder , err => {
47
68
if ( ! err ) {
48
- console . log ( `Created symlink for ${ folder } .` ) ;
49
69
return ;
50
70
}
51
71
52
72
if ( err . code === 'EEXIST' ) {
53
- console . log (
54
73
`Skipped creating symlink for ${ folder } . A symlink already exists.` ,
55
74
) ;
56
75
} else {
@@ -60,19 +79,21 @@ function createSymLink(folder) {
60
79
} ) ;
61
80
}
62
81
63
- // When we run getTranslationStatus(), it gives us 2-ALPHA locale codes unless necessary
64
- // However, the folder structure of downloaded translations always has 4-ALPHA locale codes
65
- // This function creates a map from a locale code to its corresponding folder name
82
+ // When we run getTranslationStatus(), it typically gives us ISO 639-1 (e.g. "fr" for French) or 639-3 (e.g. "fil" for Filipino) language codes,
83
+ // But the folder structure of downloaded translations uses locale codes (e.g. "fr-FR" for French, "fil-PH" for the Philippines).
84
+ // This function creates a map between language and locale code.
66
85
function createLocaleToFolderMap ( directories ) {
67
- const twoAlphaLocale = locale => locale . substring ( 0 , 2 ) ;
86
+ const localeToLanguageCode = locale => locale . includes ( '-' ) ? locale . substr ( 0 , locale . indexOf ( '-' ) ) : locale ;
68
87
const localeToFolders = new Map ( ) ;
69
88
const localeToFolder = new Map ( ) ;
70
89
71
90
for ( let locale of directories ) {
91
+ const languageCode = localeToLanguageCode ( locale ) ;
92
+
72
93
localeToFolders . set (
73
- twoAlphaLocale ( locale ) ,
74
- localeToFolders . has ( twoAlphaLocale ( locale ) )
75
- ? localeToFolders . get ( twoAlphaLocale ( locale ) ) . concat ( locale )
94
+ languageCode ,
95
+ localeToFolders . has ( languageCode )
96
+ ? localeToFolders . get ( languageCode ) . concat ( locale )
76
97
: [ locale ] ,
77
98
) ;
78
99
}
@@ -87,6 +108,7 @@ function createLocaleToFolderMap(directories) {
87
108
}
88
109
} ) ;
89
110
111
+ console . log ( localeToFolder ) ;
90
112
return localeToFolder ;
91
113
}
92
114
0 commit comments