1
+ #!/usr/bin/env babel-node
2
+
3
+ /**
4
+ * @fileoverview
5
+ * Utilities for interfacing with Transifex API 3.
6
+ * TODO: add functions for pushing to Transifex
7
+ */
8
+
9
+ const transifexApi = require ( '@transifex/api' ) . transifexApi ;
10
+ const download = require ( 'download' ) ;
11
+
12
+ const ORG_NAME = 'llk' ;
13
+ const SOURCE_LOCALE = 'en' ;
14
+
15
+ try {
16
+ transifexApi . setup ( {
17
+ auth : process . env . TX_TOKEN
18
+ } ) ;
19
+ } catch ( err ) {
20
+ if ( ! process . env . TX_TOKEN ) {
21
+ throw new Error ( 'TX_TOKEN is not defined.' ) ;
22
+ }
23
+ throw err ;
24
+ }
25
+
26
+ /**
27
+ * Creates a download event for a specific project, resource, and locale.
28
+ * @param {string } projectSlug - project slug (for example, "scratch-editor")
29
+ * @param {string } resourceSlug - resource slug (for example, "blocks")
30
+ * @param {string } localeCode - language code (for example, "ko")
31
+ * @param {string } mode - translation status of strings to include
32
+ * @returns {string } - id of the created download event
33
+ */
34
+ const downloadResource = async function ( projectSlug , resourceSlug , localeCode , mode = 'default' ) {
35
+ const resource = {
36
+ data : {
37
+ id : `o:${ ORG_NAME } :p:${ projectSlug } :r:${ resourceSlug } ` ,
38
+ type : 'resources'
39
+ }
40
+ } ;
41
+
42
+ // if locale is English, create a download event of the source file
43
+ if ( localeCode === SOURCE_LOCALE ) {
44
+ return await transifexApi . ResourceStringsAsyncDownload . download ( {
45
+ resource
46
+ } ) ;
47
+ }
48
+
49
+ const language = {
50
+ data : {
51
+ id : `l:${ localeCode } ` ,
52
+ type : 'languages'
53
+ }
54
+ } ;
55
+
56
+ // if locale is not English, create a download event of the translation file
57
+ return await transifexApi . ResourceTranslationsAsyncDownload . download ( {
58
+ mode,
59
+ resource,
60
+ language
61
+ } ) ;
62
+ } ;
63
+
64
+ /**
65
+ * Pulls a translation json from transifex, for a specific project, resource, and locale.
66
+ * @param {string } project - project slug (for example, "scratch-editor")
67
+ * @param {string } resource - resource slug (for example, "blocks")
68
+ * @param {string } locale - language code (for example, "ko")
69
+ * @param {string } mode - translation status of strings to include
70
+ * @returns {object } - JSON object of translated resource strings (or, of the original resourse
71
+ * strings, if the local is the source language)
72
+ */
73
+ const txPull = async function ( project , resource , locale , mode = 'default' ) {
74
+ const url = await downloadResource ( project , resource , locale , mode ) ;
75
+ let buffer ;
76
+ for ( let i = 0 ; i < 5 ; i ++ ) {
77
+ try {
78
+ buffer = await download ( url ) ;
79
+ return JSON . parse ( buffer . toString ( ) ) ;
80
+ } catch ( e ) {
81
+ process . stdout . write ( `got ${ e . message } , retrying after ${ i + 1 } failed attempt(s)\n` ) ;
82
+ }
83
+ }
84
+ throw Error ( 'failed to pull after 5 retries' ) ;
85
+ } ;
86
+
87
+ module . exports = txPull ;
88
+
0 commit comments