Skip to content
Permalink
Browse files

split lang sources between fullkeys, shortkeys, wikidata

- fullkeys: the key is the english full version of the text
- shortkeys: the key is just a key pointing to a longer text
- wikidata: wikidata properties with /^P[0-9]+$/ keys

lang files are build from the english version and override it
when not null
  • Loading branch information...
maxlath committed Jan 8, 2015
1 parent 8643382 commit 2a9985442e750525f3c3dfed956583518de71b25
Showing with 184 additions and 2 deletions.
  1. +1 −1 app/lib/i18n.coffee
  2. +2 −1 package.json
  3. +49 −0 scripts/build_en_from_fr.coffee
  4. +113 −0 scripts/generate_lang_json.coffee
  5. +19 −0 scripts/valid_langs.coffee
@@ -31,7 +31,7 @@ setLanguage = (app, lang)->

requestI18nFile = (polyglot, lang)->
polyglot.changingTo = lang
return _.preq.get "/static/i18n/#{lang}.json?DIGEST"
return _.preq.get "/static/i18n/dist/#{lang}.json?DIGEST"
.then (res)->
polyglot.replace res
polyglot.locale lang
@@ -20,7 +20,8 @@
"lint": "coffeelint app/* test/* brunch-config.coffee",
"slim-packages": "coffee ./scripts/slim_packages.coffee",
"install-scripts": "./scripts/install_scripts",
"bower-fix": "coffee ./scripts/bower_fix.coffee"
"bower-fix": "coffee ./scripts/bower_fix.coffee",
"update-i18n": "coffee ./scripts/generate_lang_json.coffee"
},
"dependencies": {
"auto-reload-brunch": "1.7",
@@ -0,0 +1,49 @@
#!/usr/bin/env coffee

unless /client$/.test process.cwd()
throw new Error 'this script should be run from the /client/ folder'

require 'colors'
fs = require 'graceful-fs'
Promise = require 'bluebird'
Promise.promisifyAll(fs)
pluckSettled = (results)-> _.pluck results, '_settledValue'
_ = require 'lodash'

__ =
src:
root: "./public/i18n/src"
fullkey: (path)-> "#{@root}/fullkey/#{path}.json"
shortkey: (path)-> "#{@root}/shortkey/#{path}.json"
wikidata: (path)-> "#{@root}/wikidata/#{path}.json"
dist: (path)-> "./public/i18n/dist/#{path}.json"


json_ =
read: (path)->
fs.readFileAsync(path)
.then (text)-> JSON.parse text.toString()
.catch (err)->
if err?.cause?.errno is 34
console.log "file not found: #{path}".yellow
return {}
else
console.log "error reading file at #{path}".red, err.stack
throw err

write: (path, data)->
json = JSON.stringify data, null, 4
fs.writeFileAsync(path, json)

# FRENCH INITIALIZATION
# read french fullkey
json_.read __.src.fullkey('fr')
.then (obj)->
result = {}
for k,v of obj
# french key is the english value too
result[k] = k
return result
.then (res)->
# write english fullkey
json_.write __.src.fullkey('en'), res
@@ -0,0 +1,113 @@
#!/usr/bin/env coffee

unless /client$/.test process.cwd()
throw new Error 'this script should be run from the /client/ folder'

validLangs = require './valid_langs'
args = process.argv.slice(2)

checkLang = (lang)->
unless lang in validLangs
throw new Error "#{lang} isnt a valid lang argument"

if args.length > 0
if args[0] is 'all' then args = validLangs
else args.forEach checkLang
else throw new Error "expected at least one 2-letter language code as argument, got 0"

require 'colors'
fs = require 'graceful-fs'
Promise = require 'bluebird'
Promise.promisifyAll(fs)
pluckSettled = (results)-> _.pluck results, '_settledValue'
_ = require 'lodash'

__ =
src:
root: "./public/i18n/src"
fullkey: (path)-> "#{@root}/fullkey/#{path}.json"
shortkey: (path)-> "#{@root}/shortkey/#{path}.json"
wikidata: (path)-> "#{@root}/wikidata/#{path}.json"
dist: (path)-> "./public/i18n/dist/#{path}.json"


json_ =
read: (path)->
fs.readFileAsync(path)
.then (text)-> JSON.parse text.toString()
.catch (err)->
if err?.cause?.errno is 34
console.log "file not found: #{path}".yellow
return {}
else
console.log "error reading file at #{path}".red, err.stack
throw err

write: (path, data)->
json = JSON.stringify data, null, 4
fs.writeFileAsync(path, json)

extendLangWithDefault = (lang)->
Promise.settle getSources(lang)
.then pluckSettled
.spread (args...)->
rethrowErrors(args)

[
enFull, enShort, enWd
langFull, langShort, langWd
] = args

[full, updateFull] = compareDefaultAndLang(enFull, langFull)
[short, updateShort] = compareDefaultAndLang(enShort, langShort)
[wd, updateWd] = compareDefaultAndLang(enWd, langWd)

saveMissingValue lang, updateFull, updateShort, updateWd
writeResult lang, _.extend({}, full, short, wd)

.catch (err)-> console.error "#{lang} err".red, err

rethrowErrors = (args)->
args.forEach (arg)->
if arg instanceof Error then throw arg

getSources = (lang)->
return [
json_.read __.src.fullkey('en')
json_.read __.src.shortkey('en')
json_.read __.src.wikidata('en')
json_.read __.src.fullkey(lang)
json_.read __.src.shortkey(lang)
json_.read __.src.wikidata(lang)
]

compareDefaultAndLang = (enObj, langObj)->
result = {}
for k,v of enObj
langVal = langObj[k]
if langVal?
result[k] = langVal
else
result[k] = v
langObj[k] = null
return [result, langObj]

saveMissingValue = (lang, updateFull, updateShort, updateWd)->
Promise.all [
json_.write(__.src.fullkey(lang), updateFull)
json_.write(__.src.shortkey(lang), updateShort)
json_.write(__.src.wikidata(lang), updateWd)
]
.then -> console.log "#{lang} src updated!".blue
.catch (err)-> console.log "couldnt update #{lang} src files", err.stack

count = 0
total = args.length
writeResult = (lang, result)->
json_.write __.dist(lang), result
.then ->
console.log "#{lang} done! total: #{++count}".green
if count is total then console.timeEnd 'generate'.grey

console.time 'generate'.grey
args.forEach extendLangWithDefault
@@ -0,0 +1,19 @@
module.exports = [
"aa","ab","af","ak","am","an","ar","as","av","ay","az",
"ba","be","bg","bh","bi","bm","bn","bo","br","bs","ca",
"ce","ch","co","cr","cs","cu","cv","cy","da","de","dv",
"dz","ee","el","en","eo","es","et","eu","fa","ff","fi",
"fj","fo","fr","fy","ga","gd","gl","gn","gu","gv","ha",
"he","hi","ho","hr","ht","hu","hy","hz","ia","id","ie",
"ig","ii","ik","io","is","it","iu","ja","jv","ka","kg",
"ki","kj","kk","kl","km","kn","ko","kr","ks","ku","kv",
"kw","ky","la","lb","lg","li","ln","lo","lt","lv","mg",
"mh","mi","mk","ml","mn","mo","mr","ms","mt","my","na",
"nd","ne","ng","nl","nn","no","nr","nv","ny","oc","oj",
"om","or","os","pa","pi","pl","ps","pt","qu","rm","rn",
"ro","ru","rw","sa","sc","sd","se","sg","sh","si","sk",
"sl","sm","sn","so","sq","sr","ss","st","su","sv","sw",
"ta","te","tg","th","ti","tk","tl","tn","to","tr","ts",
"tt","tw","ty","ug","uk","ur","uz","ve","vi","vo","wa",
"wo","xh","yi","yo","za","zh","zu"
]

0 comments on commit 2a99854

Please sign in to comment.
You can’t perform that action at this time.