Added i18n coverage logic to the CocoModel.

This commit is contained in:
Scott Erickson 2014-10-17 10:11:26 -04:00
parent 54de8482f1
commit 092997bffe
2 changed files with 49 additions and 1 deletions

View file

@ -332,6 +332,30 @@ class CocoModel extends Backbone.Model
error: ->
console.error 'Miserably failed to fetch unnotified achievements', arguments
CocoModel.pollAchievements = _.debounce CocoModel.pollAchievements, 500
CocoModel.pollAchievements = _.debounce CocoModel.pollAchievements, 500
#- Internationalization
updateI18NCoverage: ->
i18nObjects = @findI18NObjects()
console.log 'i18n objects', i18nObjects
langCodeArrays = (_.keys(i18n) for i18n in i18nObjects)
console.log 'lang code arrays', langCodeArrays
window.codes = langCodeArrays
@set('i18nCoverage', _.intersection(langCodeArrays...))
findI18NObjects: (data, results) ->
data ?= @attributes
results ?= []
if _.isPlainObject(data) or _.isArray(data)
for [key, value] in _.pairs data
if key is 'i18n'
results.push value
else if _.isPlainObject(value) or _.isArray(value)
@findI18NObjects(value, results)
return results
module.exports = CocoModel

View file

@ -150,3 +150,27 @@ describe 'CocoModel', ->
else return ready false
request.response {status:200, responseText: JSON.stringify me}
describe 'updateI18NCoverage', ->
class FlexibleClass extends CocoModel
@className: 'Flexible'
@schema: {}
it 'only includes languages for which all objects include a translation', ->
m = new FlexibleClass({
i18n: { es: {}, fr: {} }
prop1: 1
prop2: 'string'
prop3: true
innerObject: {
i18n: { es: {}, de: {}, fr: {} }
prop4: [
{
i18n: { es: {} }
}
]
}
})
m.updateI18NCoverage()
expect(JSON.stringify(m.get('i18nCoverage'))).toBe('["es"]')