mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
translations: move some computations to server start-up, remove console.log, add translations to $rootScope, update readme, update user.preference.language if not set
This commit is contained in:
@@ -16,11 +16,10 @@ Some files has been overwriteen after merging with develop, will revisit later:
|
||||
|
||||
- If the string is in a `.jade` file it can be accessed with `env.t('string name here')`
|
||||
- Strings under `clientSideStrings` can be accessed in `.jade` files with their property name (ie. `env.t('myString')` not `env.t('clientSideStrings.myString')`)
|
||||
- If the string is in a `.js` file it can be accessed with `window.translations['string name here']` and the string must be placed under the `clientSideStrings` part of app.json
|
||||
- If the string is in a `.js` file it can be accessed with `window.translations['string name here']` or `$rootScope.translations` (keep both or only window.env.translations?) and the string must be placed under the `clientSideStrings` part of app.json
|
||||
|
||||
## TODO
|
||||
- new users, the front page will be shown in the first avalaible language that match the languages list sent by the browser to the server, when they signup they should have their preferences.language already set?
|
||||
- add a "String not found." string for every language?
|
||||
- add a "String not found." string for every language and also for client side strings?
|
||||
|
||||
HabitRPG
|
||||
===============
|
||||
|
||||
@@ -11,6 +11,8 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
|
||||
$rootScope.user = User.user;
|
||||
$rootScope.settings = User.settings;
|
||||
|
||||
$rootScope.translations = window.env.translations;
|
||||
|
||||
// Angular UI Router
|
||||
$rootScope.$state = $state;
|
||||
$rootScope.$stateParams = $stateParams;
|
||||
|
||||
@@ -40,7 +40,6 @@ habitrpg.controller('SettingsCtrl',
|
||||
$rootScope.$on('userSynced', function(){
|
||||
location.reload();
|
||||
});
|
||||
console.log($scope.language);
|
||||
User.set('preferences.language', $scope.language);
|
||||
}
|
||||
|
||||
|
||||
@@ -84,32 +84,34 @@ var getManifestFiles = function(page){
|
||||
var translations = {};
|
||||
|
||||
fs.readdirSync(path.join(__dirname, "/../locales")).forEach(function(file) {
|
||||
translations[file] = require(path.join(__dirname, "/../locales/", file, 'app.json'))
|
||||
var t = translations[file] = {};
|
||||
t.server = require(path.join(__dirname, "/../locales/", file, 'app.json'));
|
||||
t.client = t.server.clientSideStrings;
|
||||
delete t.server.clientSideStrings;
|
||||
_.merge(t.server, t.client);
|
||||
});
|
||||
|
||||
var langCodes = Object.keys(translations);
|
||||
|
||||
var avalaibleLanguages = _.map(langCodes, function(langCode){
|
||||
return {
|
||||
code: langCode,
|
||||
name: translations[langCode].server.languageName
|
||||
}
|
||||
});
|
||||
|
||||
var getTranslatedString = function(locale, string){
|
||||
if(!locale || !string) throw new Error("Missing locale and/or string argument.");
|
||||
// Should never be called
|
||||
//if(!translations[locale]) throw new Error("Missing locale '" + locale + "'");
|
||||
|
||||
// TODO support nested dot-separated strings
|
||||
// TODO support nested, dot-separated, strings
|
||||
return (
|
||||
translations[locale][string] ||
|
||||
translations[locale]['clientSideStrings'][string] ||
|
||||
translations['en'][string] ||
|
||||
translations['en']['clientSideStrings'][string] ||
|
||||
translations[locale].server[string] ||
|
||||
translations.en.server[string] ||
|
||||
'String not found.')
|
||||
}
|
||||
|
||||
var avalaibleLanguages = _.map(langCodes, function(langCode){
|
||||
return {
|
||||
code: langCode,
|
||||
name: translations[langCode].languageName
|
||||
}
|
||||
});
|
||||
|
||||
var getUserLanguage = function(req, callback){
|
||||
var getFromBrowser = function(){
|
||||
var acceptable = _(req.acceptedLanguages).map(function(lang){
|
||||
@@ -126,7 +128,12 @@ var getUserLanguage = function(req, callback){
|
||||
if(user && user.preferences.language && translations[user.preferences.language]){
|
||||
return callback(null, _.find(avalaibleLanguages, {code: user.preferences.language}));
|
||||
}else{
|
||||
return callback(null, _.find(avalaibleLanguages, {code: getFromBrowser()}))
|
||||
var langCode = getFromBrowser();
|
||||
if(user){
|
||||
user.preferences.language = langCode;
|
||||
user.save(); //callback?
|
||||
}
|
||||
return callback(null, _.find(avalaibleLanguages, {code: langCode}))
|
||||
}
|
||||
});
|
||||
}else{
|
||||
@@ -148,7 +155,7 @@ module.exports.locals = function(req, res, next) {
|
||||
getBuildUrl: getBuildUrl,
|
||||
avalaibleLanguages: avalaibleLanguages,
|
||||
language: language,
|
||||
translations: translations[language.code].clientSideStrings,
|
||||
translations: translations[language.code].client,
|
||||
t: function(string){
|
||||
return getTranslatedString(language.code, string);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user