@@ -5,16 +5,43 @@ const {symlink, lstatSync, readdirSync} = require('fs');
5
5
6
6
const SYMLINKED_TRANSLATIONS_PATH = path . resolve ( __dirname , 'translations' ) ;
7
7
const DOWNLOADED_TRANSLATIONS_PATH = path . resolve ( __dirname , '__translations' ) ;
8
- const DOWNLOADED_TRANSLATIONS_DOCS_PATH = path . resolve (
8
+
9
+ const getDownloadedDocsPath = ( { downloadedRootDirectory } ) => path . resolve (
9
10
__dirname ,
10
11
'__translations' ,
12
+ downloadedRootDirectory ,
11
13
'docs' ,
12
14
) ;
13
15
16
+ const validateConfig = ( { key, downloadedRootDirectory, threshold, url } ) => {
17
+ const errors = [ ] ;
18
+ if ( ! key ) {
19
+ errors . push ( 'key: No process.env.CROWDIN_API_KEY value defined.' ) ;
20
+ }
21
+ if ( ! Number . isInteger ( threshold ) ) {
22
+ errors . push ( `threshold: Invalid translation threshold defined.` ) ;
23
+ }
24
+ if ( ! downloadedRootDirectory ) {
25
+ errors . push ( 'downloadedRootDirectory: No root directory defined for the downloaded translations bundle.' ) ;
26
+ }
27
+ if ( ! url ) {
28
+ errors . push ( 'url: No Crowdin project URL defined.' ) ;
29
+ }
30
+ if ( errors . length > 0 ) {
31
+ console . error ( 'Invalid Crowdin config values for:\n• ' + errors . join ( '\n• ' ) ) ;
32
+ throw Error ( 'Invalid Crowdin config' ) ;
33
+ }
34
+ } ;
35
+
14
36
function main ( ) {
37
+ validateConfig ( config ) ;
38
+
15
39
const crowdin = new Crowdin ( { apiKey : config . key , endpointUrl : config . url } ) ;
40
+
16
41
process . chdir ( SYMLINKED_TRANSLATIONS_PATH ) ;
17
42
43
+ const downloadedDocsPath = getDownloadedDocsPath ( config ) ;
44
+
18
45
crowdin
19
46
// .export() // Not sure if this should be called in the script since it could be very slow
20
47
// .then(() => crowdin.downloadToPath(DOWNLOADED_TRANSLATIONS_PATH))
@@ -23,34 +50,30 @@ function main() {
23
50
. then ( locales => {
24
51
const usableLocales = locales
25
52
. filter (
26
- locale => locale . translated_progress > config . translation_threshold ,
53
+ locale => locale . translated_progress > config . threshold ,
27
54
)
28
55
. map ( local => local . code ) ;
29
56
30
- const localeDirectories = getDirectories (
31
- DOWNLOADED_TRANSLATIONS_DOCS_PATH ,
32
- ) ;
33
-
57
+ const localeDirectories = getDirectories ( downloadedDocsPath ) ;
34
58
const localeToFolderMap = createLocaleToFolderMap ( localeDirectories ) ;
35
59
36
60
usableLocales . forEach ( locale => {
37
- createSymLink ( localeToFolderMap . get ( locale ) ) ;
61
+ createSymLink ( downloadedDocsPath , localeToFolderMap . get ( locale ) ) ;
38
62
} ) ;
39
63
} ) ;
40
64
}
41
65
42
66
// Creates a relative symlink from a downloaded translation in the current working directory
43
67
// Note that the current working directory of this node process should be where the symlink is created
44
68
// or else the relative paths would be incorrect
45
- function createSymLink ( folder ) {
46
- symlink ( `../__translations/docs/ ${ folder } ` , folder , err => {
69
+ function createSymLink ( downloadedDocsPath , folder ) {
70
+ symlink ( path . resolve ( downloadedDocsPath , folder ) , folder , err => {
47
71
if ( ! err ) {
48
- console . log ( `Created symlink for ${ folder } .` ) ;
49
72
return ;
50
73
}
51
74
52
75
if ( err . code === 'EEXIST' ) {
53
- console . log (
76
+ console . info (
54
77
`Skipped creating symlink for ${ folder } . A symlink already exists.` ,
55
78
) ;
56
79
} else {
@@ -60,19 +83,21 @@ function createSymLink(folder) {
60
83
} ) ;
61
84
}
62
85
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
86
+ // 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,
87
+ // But the folder structure of downloaded translations uses locale codes (e.g. "fr-FR" for French, "fil-PH" for the Philippines).
88
+ // This function creates a map between language and locale code.
66
89
function createLocaleToFolderMap ( directories ) {
67
- const twoAlphaLocale = locale => locale . substring ( 0 , 2 ) ;
90
+ const localeToLanguageCode = locale => locale . includes ( '-' ) ? locale . substr ( 0 , locale . indexOf ( '-' ) ) : locale ;
68
91
const localeToFolders = new Map ( ) ;
69
92
const localeToFolder = new Map ( ) ;
70
93
71
94
for ( let locale of directories ) {
95
+ const languageCode = localeToLanguageCode ( locale ) ;
96
+
72
97
localeToFolders . set (
73
- twoAlphaLocale ( locale ) ,
74
- localeToFolders . has ( twoAlphaLocale ( locale ) )
75
- ? localeToFolders . get ( twoAlphaLocale ( locale ) ) . concat ( locale )
98
+ languageCode ,
99
+ localeToFolders . has ( languageCode )
100
+ ? localeToFolders . get ( languageCode ) . concat ( locale )
76
101
: [ locale ] ,
77
102
) ;
78
103
}
0 commit comments