forked from HabitRPG/habitica
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/HabitRPG/habitrpg into d…
…evelop
- Loading branch information
Showing
35 changed files
with
344 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
var fs = require('fs'), | ||
path = require('path'), | ||
_ = require('lodash'), | ||
User = require('./models/user').model, | ||
shared = require('habitrpg-shared'), | ||
translations = {}; | ||
|
||
var loadTranslations = function(locale){ | ||
var files = fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/", locale)); | ||
translations[locale] = {}; | ||
_.each(files, function(file){ | ||
_.merge(translations[locale], require(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/", locale, file))); | ||
}); | ||
}; | ||
|
||
// First fetch english so we can merge with missing strings in other languages | ||
loadTranslations('en'); | ||
|
||
fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/")).forEach(function(file) { | ||
if(file === 'en' || file === 'README.md') return; | ||
loadTranslations(file); | ||
// Merge missing strings from english | ||
_.defaults(translations[file], translations.en); | ||
}); | ||
|
||
var langCodes = Object.keys(translations); | ||
|
||
var avalaibleLanguages = _.map(langCodes, function(langCode){ | ||
return { | ||
code: langCode, | ||
name: translations[langCode].languageName | ||
} | ||
}); | ||
|
||
// Load MomentJS localization files | ||
var momentLangs = {}; | ||
|
||
// Handle different language codes from MomentJS and /locales | ||
var momentLangsMapping = { | ||
'en': 'en-gb', | ||
'no': 'nn' | ||
}; | ||
|
||
var momentLangs = {}; | ||
|
||
_.each(langCodes, function(code){ | ||
var lang = _.find(avalaibleLanguages, {code: code}); | ||
lang.momentLangCode = (momentLangsMapping[code] || code); | ||
try{ | ||
// MomentJS lang files are JS files that has to be executed in the browser so we load them as plain text files | ||
var f = fs.readFileSync(path.join(__dirname, '/../node_modules/moment/lang/' + lang.momentLangCode + '.js'), 'utf8'); | ||
momentLangs[code] = f; | ||
}catch (e){} | ||
}); | ||
|
||
var getUserLanguage = function(req, res, next){ | ||
var getFromBrowser = function(){ | ||
var acceptable = _(req.acceptedLanguages).map(function(lang){ | ||
return lang.slice(0, 2); | ||
}).uniq().value(); | ||
var matches = _.intersection(acceptable, langCodes); | ||
return matches.length > 0 ? matches[0] : 'en'; | ||
}; | ||
|
||
var getFromUser = function(user){ | ||
var lang; | ||
if(user && user.preferences.language && translations[user.preferences.language]){ | ||
lang = user.preferences.language; | ||
}else{ | ||
var preferred = getFromBrowser(); | ||
lang = translations[preferred] ? preferred : 'en'; | ||
} | ||
req.language = lang; | ||
next(); | ||
}; | ||
|
||
if(req.locals && req.locals.user){ | ||
getFromUser(req.locals.user); | ||
}else if(req.session && req.session.userId){ | ||
User.findOne({_id: req.session.userId}, function(err, user){ | ||
if(err) return callback(err); | ||
getFromUser(user); | ||
}); | ||
}else{ | ||
getFromUser(null); | ||
} | ||
}; | ||
|
||
shared.i18n.translations = translations; | ||
|
||
module.exports = { | ||
translations: translations, | ||
avalaibleLanguages: avalaibleLanguages, | ||
langCodes: langCodes, | ||
getUserLanguage: getUserLanguage, | ||
momentLangs: momentLangs | ||
}; |
Oops, something went wrong.