2014-01-22 17:57:41 -05:00
|
|
|
errors = require '../commons/errors'
|
2014-02-04 17:08:20 -05:00
|
|
|
log = require 'winston'
|
2014-01-22 15:46:44 -05:00
|
|
|
locale = require '../../app/locale/locale' # requiring from app; will break if we stop serving from where app lives
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-04 16:29:13 -05:00
|
|
|
module.exports.setup = (app) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
app.all '/languages/add/:lang/:namespace', (req, res) ->
|
|
|
|
# Should probably store these somewhere
|
2014-02-04 17:08:20 -05:00
|
|
|
log.info "#{req.params.lang}.#{req.params.namespace} missing an i18n key:", req.body
|
2014-01-03 13:32:13 -05:00
|
|
|
res.send('')
|
|
|
|
res.end()
|
|
|
|
|
|
|
|
app.all '/languages', (req, res) ->
|
|
|
|
# Now that these are in the client, not sure when we would use this, but hey
|
|
|
|
return errors.badMethod(res) if req.route.method isnt 'get'
|
|
|
|
res.send(languages)
|
|
|
|
return res.end()
|
|
|
|
|
|
|
|
languages = []
|
|
|
|
for code, localeInfo of locale
|
|
|
|
languages.push code: code, nativeDescription: localeInfo.nativeDescription, englishDescription: localeInfo.englishDescription
|
|
|
|
|
|
|
|
module.exports.languages = languages
|
|
|
|
module.exports.languageCodes = languageCodes = (language.code for language in languages)
|
|
|
|
module.exports.languageCodesLower = languageCodesLower = (code.toLowerCase() for code in languageCodes)
|
|
|
|
|
|
|
|
# Keep keys lower-case for matching and values with second subtag uppercase like i18next expects
|
|
|
|
languageAliases =
|
|
|
|
'en': 'en-US'
|
|
|
|
|
|
|
|
'zh-cn': 'zh-HANS'
|
|
|
|
'zh-hans-cn': 'zh-HANS'
|
|
|
|
'zh-sg': 'zh-HANS'
|
|
|
|
'zh-hans-sg': 'zh-HANS'
|
|
|
|
|
|
|
|
'zh-tw': 'zh-HANT'
|
|
|
|
'zh-hant-tw': 'zh-HANT'
|
|
|
|
'zh-hk': 'zh-HANT'
|
|
|
|
'zh-hant-hk': 'zh-HANT'
|
|
|
|
'zh-mo': 'zh-HANT'
|
|
|
|
'zh-hant-mo': 'zh-HANT'
|
|
|
|
|
|
|
|
module.exports.languageCodeFromAcceptedLanguages = languageCodeFromAcceptedLanguages = (acceptedLanguages) ->
|
|
|
|
for lang in acceptedLanguages ? []
|
|
|
|
code = languageAliases[lang.toLowerCase()]
|
|
|
|
return code if code
|
|
|
|
codeIndex = _.indexOf languageCodesLower, lang
|
|
|
|
if codeIndex isnt -1
|
|
|
|
return languageCodes[codeIndex]
|
|
|
|
return 'en-US'
|