forked from NetrunnerDB/netrunner-cards-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_locales.coffee
70 lines (55 loc) · 2.26 KB
/
update_locales.coffee
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
#merge data files from default English with each locale found in translations directory
fs = require 'fs'
path = require 'path'
_ = require 'lodash'
[bin, script, locale] = process.argv
i18nDir = path.join __dirname, 'translations'
things = ['cycles', 'factions', 'packs', 'types', 'sides']
stripProps = (json, props) ->
_.map json, (item) ->
_.pick item, props
loadThings = (root) ->
result = {}
for thing in things
file = "#{thing}.json"
filepath = path.join(root, file)
try
json = JSON.parse fs.readFileSync(filepath, 'UTF-8')
result[file] = stripProps json, ["code", "name"]
result
loadCards = (root) ->
result = {}
localeRoot = path.join root, 'pack'
try
fs.mkdirSync(localeRoot)
files = fs.readdirSync localeRoot
for file in files
json = JSON.parse fs.readFileSync(path.join(localeRoot, file), 'UTF-8')
result[file] = stripProps json, ['code','flavor','title','text','keywords']
result
merge_data = (defaultLocale, locale) ->
result = {}
for file in _.union(_.keys(defaultLocale), _.keys(locale))
targetFile = file.replace(/^(\w+).json$/, "$1.#{code}.json")
result[targetFile] = _(_.merge({}, _.keyBy(defaultLocale[file] or {}, 'code'), _.keyBy(locale[file] or {}, 'code'))).values().sortBy('code').value()
result
things_en = loadThings __dirname
cards_en = loadCards __dirname
codes = fs.readdirSync i18nDir
for code in codes when not locale? or code is locale
console.log "Updating locale '#{code}'..."
localeRoot = path.join i18nDir, code
l_things = loadThings localeRoot
l_cards = loadCards localeRoot
m_things = merge_data(things_en, l_things)
m_cards = merge_data(cards_en, l_cards)
for file in _.keys m_things
target = path.join localeRoot, file
if !fs.existsSync(target, fs.constants.R_OK)
fs.writeFileSync target, JSON.stringify(m_things[file], null, 4)+"\n"
console.log "Written #{target}"
for file in _.keys m_cards
target = path.join localeRoot, 'pack', file
if !fs.existsSync(target, fs.constants.R_OK)
fs.writeFileSync target, JSON.stringify(m_cards[file], null, 4)+"\n"
console.log "Written #{target}"