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')`
|
- 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')`)
|
- 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
|
## 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 and also for client side strings?
|
||||||
- add a "String not found." string for every language?
|
|
||||||
|
|
||||||
HabitRPG
|
HabitRPG
|
||||||
===============
|
===============
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
|
|||||||
$rootScope.user = User.user;
|
$rootScope.user = User.user;
|
||||||
$rootScope.settings = User.settings;
|
$rootScope.settings = User.settings;
|
||||||
|
|
||||||
|
$rootScope.translations = window.env.translations;
|
||||||
|
|
||||||
// Angular UI Router
|
// Angular UI Router
|
||||||
$rootScope.$state = $state;
|
$rootScope.$state = $state;
|
||||||
$rootScope.$stateParams = $stateParams;
|
$rootScope.$stateParams = $stateParams;
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ habitrpg.controller('SettingsCtrl',
|
|||||||
$rootScope.$on('userSynced', function(){
|
$rootScope.$on('userSynced', function(){
|
||||||
location.reload();
|
location.reload();
|
||||||
});
|
});
|
||||||
console.log($scope.language);
|
|
||||||
User.set('preferences.language', $scope.language);
|
User.set('preferences.language', $scope.language);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,32 +84,34 @@ var getManifestFiles = function(page){
|
|||||||
var translations = {};
|
var translations = {};
|
||||||
|
|
||||||
fs.readdirSync(path.join(__dirname, "/../locales")).forEach(function(file) {
|
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 langCodes = Object.keys(translations);
|
||||||
|
|
||||||
|
var avalaibleLanguages = _.map(langCodes, function(langCode){
|
||||||
|
return {
|
||||||
|
code: langCode,
|
||||||
|
name: translations[langCode].server.languageName
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var getTranslatedString = function(locale, string){
|
var getTranslatedString = function(locale, string){
|
||||||
if(!locale || !string) throw new Error("Missing locale and/or string argument.");
|
if(!locale || !string) throw new Error("Missing locale and/or string argument.");
|
||||||
// Should never be called
|
// Should never be called
|
||||||
//if(!translations[locale]) throw new Error("Missing locale '" + locale + "'");
|
//if(!translations[locale]) throw new Error("Missing locale '" + locale + "'");
|
||||||
|
|
||||||
// TODO support nested dot-separated strings
|
// TODO support nested, dot-separated, strings
|
||||||
return (
|
return (
|
||||||
translations[locale][string] ||
|
translations[locale].server[string] ||
|
||||||
translations[locale]['clientSideStrings'][string] ||
|
translations.en.server[string] ||
|
||||||
translations['en'][string] ||
|
|
||||||
translations['en']['clientSideStrings'][string] ||
|
|
||||||
'String not found.')
|
'String not found.')
|
||||||
}
|
}
|
||||||
|
|
||||||
var avalaibleLanguages = _.map(langCodes, function(langCode){
|
|
||||||
return {
|
|
||||||
code: langCode,
|
|
||||||
name: translations[langCode].languageName
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var getUserLanguage = function(req, callback){
|
var getUserLanguage = function(req, callback){
|
||||||
var getFromBrowser = function(){
|
var getFromBrowser = function(){
|
||||||
var acceptable = _(req.acceptedLanguages).map(function(lang){
|
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]){
|
if(user && user.preferences.language && translations[user.preferences.language]){
|
||||||
return callback(null, _.find(avalaibleLanguages, {code: user.preferences.language}));
|
return callback(null, _.find(avalaibleLanguages, {code: user.preferences.language}));
|
||||||
}else{
|
}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{
|
}else{
|
||||||
@@ -148,7 +155,7 @@ module.exports.locals = function(req, res, next) {
|
|||||||
getBuildUrl: getBuildUrl,
|
getBuildUrl: getBuildUrl,
|
||||||
avalaibleLanguages: avalaibleLanguages,
|
avalaibleLanguages: avalaibleLanguages,
|
||||||
language: language,
|
language: language,
|
||||||
translations: translations[language.code].clientSideStrings,
|
translations: translations[language.code].client,
|
||||||
t: function(string){
|
t: function(string){
|
||||||
return getTranslatedString(language.code, string);
|
return getTranslatedString(language.code, string);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user