load-data.ls 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. assign = require 'lodash.assign'
  2. isequal = require 'lodash.isequal'
  3. # TODO: support overrides
  4. # Then we can expand this list to include e.g. wikipedia_url, but not warn in situations where there isn't a translated Wikipedia article
  5. # When we do this we should warn about the presence of non-translatable keys (e.g. the `protocols` key should always be consistent)
  6. translatable = ['notes', 'description']
  7. module.exports = load-data = (path, iso, find-missing = false) ->
  8. en-data = require path.replace('/' + iso, '/en')
  9. localized-data = require path
  10. for obj in en-data
  11. # Look for a localized version
  12. potential-localized-obj = null
  13. for localized-obj in localized-data
  14. if localized-obj.slug is obj.slug
  15. potential-localized-obj = assign {}, obj, localized-obj
  16. break
  17. if find-missing
  18. if not potential-localized-obj
  19. console.log 'Missing ' + iso + ' localization for ' + obj.slug
  20. continue
  21. else
  22. # First we check if entire objects are duplicated
  23. # We use localized-obj because potential-localized-obj has already been filled in by lodash.assign
  24. if isequal obj, localized-obj
  25. console.log iso + ' localization of ' + obj.slug + ' duplicates the English version'
  26. # Then we check for per-key problems
  27. else
  28. # For each (non-empty) localizable key in the English version...
  29. for key of obj when translatable.includes key and obj[key] is not ''
  30. # ...check if it hasn't been localized at all
  31. if not localized-obj[key]
  32. console.log 'Missing ' + iso + ' localization for key `' + key + '` in project ' + obj.slug
  33. # ...check if the localized version is a duplicate
  34. else if isequal obj[key], localized-obj[key]
  35. console.log iso + ' localization of key `' + key + '` in project ' + obj.slug + ' duplicates the English version'
  36. continue
  37. potential-localized-obj or obj