FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
git-point/scripts/extract-i18n.js at master · gitpoint/git-point · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
gitpoint
/
git-point
Public
Notifications
You must be signed in to change notification settings
Fork
773
Star
4.8k
Code
Issues
96
Pull requests
40
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
git-point
/
scripts
/
extract-i18n.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
166 lines (131 loc) · 4 KB
Breadcrumbs
git-point
/
scripts
/
extract-i18n.js
Copy path
File metadata and controls
166 lines (131 loc) · 4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-console */
/* eslint-disable no-continue */
const
chalk
=
require
(
'chalk'
)
;
const
glob
=
require
(
'glob'
)
;
const
fs
=
require
(
'fs'
)
;
const
babylon
=
require
(
'babylon'
)
;
const
i18nConfig
=
require
(
'./../package.json'
)
.
i18n
;
const
report
=
message
=>
{
console
.
log
(
message
)
;
}
;
const
extractFromSource
=
config
=>
{
const
messages
=
{
}
;
config
.
sourcePath
.
forEach
(
source
=>
{
const
files
=
glob
.
sync
(
`
${
source
}
/**/*.js`
,
{
debug
:
false
}
)
;
files
.
forEach
(
file
=>
{
for
(
let
i
=
0
;
i
<
config
.
ignorePath
.
length
;
i
++
)
{
if
(
file
.
indexOf
(
config
.
ignorePath
[
i
]
)
!==
-
1
)
{
return
;
}
}
const
contents
=
fs
.
readFileSync
(
file
)
.
toString
(
)
;
const
tokens
=
babylon
.
parse
(
contents
,
{
sourceType
:
'module'
,
plugins
:
[
'jsx'
,
'classProperties'
,
'objectRestSpread'
,
'flow'
]
,
}
)
.
tokens
;
let
counter
=
0
;
for
(
let
i
=
0
;
i
<
tokens
.
length
;
i
++
)
{
const
token
=
tokens
[
i
]
;
if
(
token
.
type
.
label
===
'name'
&&
token
.
value
===
config
.
translator
)
{
const
parenthesis
=
tokens
[
i
+
1
]
;
const
message
=
tokens
[
i
+
2
]
;
if
(
!
parenthesis
||
parenthesis
.
type
.
label
!==
'('
)
{
continue
;
}
if
(
!
message
||
message
.
type
.
label
!==
'string'
)
{
continue
;
}
if
(
messages
[
message
.
value
]
===
''
)
{
continue
;
}
counter
+=
1
;
messages
[
message
.
value
]
=
''
;
}
}
report
(
` - parsing
${
chalk
.
bold
(
file
)
}
..
${
chalk
.
bold
(
counter
)
}
strings found!`
)
;
}
)
;
}
)
;
return
messages
;
}
;
const
generateFiles
=
(
config
,
messages
)
=>
{
report
(
'Generating translations:'
)
;
let
exports
=
'/* eslint-disable quote-props */\nexport default {\n'
;
config
.
languages
.
forEach
(
language
=>
{
const
filepath
=
`
${
process
.
cwd
(
)
}
/
${
config
.
messagePath
}
/
${
language
}
.js`
;
let
oldMessages
=
{
}
;
try
{
oldMessages
=
require
(
filepath
)
;
}
catch
(
e
)
{
console
.
log
(
e
)
;
}
const
action
=
oldMessages
&&
oldMessages
.
length
===
0
?
'generated'
:
'merged'
;
// Get a fresh copy of found strings
const
newMessages
=
{
...
messages
}
;
if
(
language
===
config
.
sourceLanguage
)
{
Object
.
keys
(
newMessages
)
.
forEach
(
key
=>
{
newMessages
[
key
]
=
key
;
}
)
;
}
// Iterate on old strings
Object
.
keys
(
oldMessages
)
.
forEach
(
key
=>
{
if
(
typeof
newMessages
[
key
]
!==
'undefined'
)
{
// Translation needed & found: copy it
if
(
oldMessages
[
key
]
!==
''
)
{
newMessages
[
key
]
=
oldMessages
[
key
]
;
}
}
else
if
(
typeof
oldMessages
[
key
]
===
'object'
)
{
// old syntax, copy blindly
newMessages
[
key
]
=
oldMessages
[
key
]
;
}
else
{
// Translation old & gone: surround it with @
newMessages
[
key
]
=
`@
${
oldMessages
[
key
]
.
replace
(
/
^
@
+
|
@
+
$
/
g
,
''
)
}
@`
;
}
}
)
;
const
ordered
=
{
}
;
Object
.
keys
(
newMessages
)
.
sort
(
)
.
forEach
(
key
=>
{
ordered
[
key
]
=
newMessages
[
key
]
;
}
)
;
// Write the new message file
fs
.
writeFile
(
filepath
,
`module.exports =
${
JSON
.
stringify
(
ordered
,
null
,
2
)
}
;`
,
'utf8'
,
err
=>
{
if
(
err
)
throw
err
;
}
)
;
report
(
` -
${
action
}
${
chalk
.
bold
(
filepath
)
}
`
)
;
exports
+=
` '
${
language
}
': require('./
${
language
}
'),\n`
;
}
)
;
exports
+=
'};\n'
;
fs
.
writeFile
(
`
${
process
.
cwd
(
)
}
/
${
config
.
messagePath
}
/index.js`
,
exports
,
'utf8'
,
err
=>
{
if
(
err
)
throw
err
;
}
)
;
}
;
const
extractMessages
=
config
=>
{
report
(
`Extracting messages in
${
chalk
.
green
(
config
.
sourcePath
)
}
using
${
chalk
.
green
(
config
.
translator
)
}
()`
)
;
const
messages
=
extractFromSource
(
config
)
;
report
(
'Done parsing\n'
)
;
generateFiles
(
config
,
messages
)
;
report
(
'Done, have fun translating!\n'
)
;
// Et voilà!
return
true
;
}
;
extractMessages
(
i18nConfig
)
;
Back
|
FazBrowse Home
|
New Git URL