refactor(i18n): start using strings for api messages

This commit is contained in:
Matteo Pagliazzi 2014-03-16 18:37:53 +01:00
parent f80cff8916
commit ad7a890160
4 changed files with 67 additions and 39 deletions

View file

@ -11911,7 +11911,7 @@ var _;
_ = require('lodash');
module.exports = {
strings: {},
strings: null,
translations: {},
t: function(stringName) {
var locale, string, stringNotFound, vars;
@ -11923,7 +11923,7 @@ module.exports = {
vars = arguments[1];
locale = arguments[2];
}
string = locale ? module.exports.translations[locale][stringName] : module.exports.strings[stringName];
string = locale && !strings ? module.exports.translations[locale][stringName] : module.exports.strings[stringName];
if (string) {
if (vars) {
return _.template(string, vars);
@ -11931,7 +11931,7 @@ module.exports = {
return string;
}
} else {
stringNotFound = locale ? module.exports.translations[locale].stringNotFound : module.exports.strings.stringNotFound;
stringNotFound = locale && !strings ? module.exports.translations[locale].stringNotFound : module.exports.strings.stringNotFound;
return _.template(stringNotFound, {
string: stringName
});
@ -11942,7 +11942,11 @@ module.exports = {
},{"lodash":3}],7:[function(require,module,exports){
(function (process){
<<<<<<< HEAD
var $w, api, content, moment, preenHistory, sanitizeOptions, sortOrder, _,
=======
var $w, api, content, i18n, moment, preenHistory, sanitizeOptions, _,
>>>>>>> refactor(i18n): start using strings for api messages
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
moment = require('moment');
@ -11951,9 +11955,11 @@ _ = require('lodash');
content = require('./content.coffee');
i18n = require('./i18n.coffee');
api = module.exports = {};
api.i18n = require('./i18n.coffee');
api.i18n = i18n;
$w = function(s) {
return s.split(' ');
@ -12841,7 +12847,9 @@ api.wrap = function(user, main) {
}
return typeof cb === "function" ? cb((item ? {
code: 200,
message: "Your " + (item.text()) + " broke."
message: i18n.t('messageLostItem', {
itemText: item.text
}, req.language)
} : null), user) : void 0;
},
reset: function(req, cb) {
@ -12874,7 +12882,7 @@ api.wrap = function(user, main) {
if (user.balance < 1) {
return typeof cb === "function" ? cb({
code: 401,
message: "Not enough gems."
message: i18n.t('notEnoughGems', req.language)
}) : void 0;
}
user.balance--;
@ -12894,7 +12902,7 @@ api.wrap = function(user, main) {
if (user.balance < 2 && user.stats.lvl < 100) {
return typeof cb === "function" ? cb({
code: 401,
message: "Not enough gems."
message: i18n.t('notEnoughGems', req.language)
}) : void 0;
}
if (user.stats.lvl < 100) {
@ -12996,7 +13004,7 @@ api.wrap = function(user, main) {
if (!task) {
return typeof cb === "function" ? cb({
code: 404,
message: "Task not found."
message: i18n.t('messageTaskNotFound', req.language)
}) : void 0;
}
if (!((to != null) && (from != null))) {
@ -13011,7 +13019,7 @@ api.wrap = function(user, main) {
if (!(task = user.tasks[(_ref = req.params) != null ? _ref.id : void 0])) {
return typeof cb === "function" ? cb({
code: 404,
message: "Task not found"
message: i18n.t('messageTaskNotFound', req.language)
}) : void 0;
}
_.merge(task, _.omit(req.body, ['checklist', 'id']));
@ -13029,7 +13037,7 @@ api.wrap = function(user, main) {
if (!task) {
return typeof cb === "function" ? cb({
code: 404,
message: 'Task not found'
message: i18n.t('messageTaskNotFound', req.language)
}) : void 0;
}
i = user[task.type + "s"].indexOf(task);
@ -13075,7 +13083,7 @@ api.wrap = function(user, main) {
if (!~i) {
return typeof cb === "function" ? cb({
code: 404,
message: 'Tag not found'
message: i18n.t('messageTagNotFound', req.language)
}) : void 0;
}
user.tags[i].name = req.body.name;
@ -13090,7 +13098,7 @@ api.wrap = function(user, main) {
if (!~i) {
return typeof cb === "function" ? cb({
code: 404,
message: 'Tag not found'
message: i18n.t('messageTagNotFound', req.language)
}) : void 0;
}
tag = user.tags[i];
@ -13113,25 +13121,25 @@ api.wrap = function(user, main) {
if (!userPets[pet]) {
return typeof cb === "function" ? cb({
code: 404,
message: ":pet not found in user.items.pets"
message: i18n.t('messagePetNotFound', req.language)
}) : void 0;
}
if (!((_ref2 = user.items.food) != null ? _ref2[food.key] : void 0)) {
return typeof cb === "function" ? cb({
code: 404,
message: ":food not found in user.items.food"
message: i18n.t('messageFoodNotFound', req.language)
}) : void 0;
}
if (content.specialPets[pet] || (egg === "Egg")) {
return typeof cb === "function" ? cb({
code: 401,
message: "Can't feed this pet."
message: i18n.t('messageCannotFeedPet', req.language)
}) : void 0;
}
if (user.items.mounts[pet]) {
return typeof cb === "function" ? cb({
code: 401,
message: "You already have that mount. Try feeding another pet."
message: i18n.t('messageAlreadyMount', req.language)
}) : void 0;
}
message = '';
@ -13141,17 +13149,25 @@ api.wrap = function(user, main) {
if (pet === user.items.currentPet) {
user.items.currentPet = "";
}
return message = "You have tamed " + egg + ", let's go for a ride!";
return message = i18n.t('messageAlreadyMount', {
egg: egg
}, req.language);
};
if (food.key === 'Saddle') {
evolve();
} else {
if (food.target === potion) {
userPets[pet] += 5;
message = "" + egg + " really likes the " + (food.text()) + "!";
message = i18n.t('messageLikesFood', {
egg: egg,
foodText: food.text
}, req.language);
} else {
userPets[pet] += 2;
message = "" + egg + " eats the " + (food.text()) + " but doesn't seem to enjoy it.";
message = i18n.t('messageDontEnjoyFood', {
egg: egg,
foodText: food.text
}, req.language);
}
if (userPets[pet] >= 50 && !user.items.mounts[pet]) {
evolve();

12
locales/en/messages.json Normal file
View file

@ -0,0 +1,12 @@
{
"messageLostItem": "Your <%= itemText() %> broke.",
"messageTaskNotFound": "Task not found.",
"messageTagNotFound": "Tag not found.",
"messagePetNotFound": ":pet not found in user.items.pets",
"messageFoodNotFound": ":food not found in user.items.food",
"messageCannotFeedPet": "Can't feed this pet.",
"messageAlreadyMount": "You already have that mount. Try feeding another pet.",
"messageEvolve": "You have tamed <%= egg %>, let's go for a ride!",
"messageLikesFood": "<%= egg %> really likes the <%= foodText() %>!",
"messageDontEnjoyFood": "<%= egg %> eats the <%= foodText() %> but doesn't seem to enjoy it."
}

View file

@ -1,7 +1,7 @@
_ = require 'lodash'
module.exports =
strings: {}, # Strings for one single language
strings: null, # Strings for one single language
translations: {} # Strings for multiple languages {en: strings, de: strings, ...}
t: (stringName) -> # Other parameters allowed are vars (Object) and locale (String)
vars = arguments[1]
@ -13,10 +13,10 @@ module.exports =
vars = arguments[1]
locale = arguments[2]
string = if locale then module.exports.translations[locale][stringName] else module.exports.strings[stringName]
string = if (locale and !strings) then module.exports.translations[locale][stringName] else module.exports.strings[stringName]
if string
if vars then _.template(string, vars) else string
else
stringNotFound = if locale then module.exports.translations[locale].stringNotFound else module.exports.strings.stringNotFound
stringNotFound = if (locale and !strings) then module.exports.translations[locale].stringNotFound else module.exports.strings.stringNotFound
_.template(stringNotFound, {string: stringName})

View file

@ -1,10 +1,10 @@
moment = require('moment')
_ = require('lodash')
content = require('./content.coffee')
i18n = require('./i18n.coffee')
api = module.exports = {}
api.i18n = require('./i18n.coffee')
api.i18n = i18n
# little helper for large arrays of strings. %w"this that another" equivalent from Rails, I really miss that function
$w = (s)->s.split(' ')
@ -388,7 +388,7 @@ api.wrap = (user, main=true) ->
user.items.gear.equipped[item.type] = "#{item.type}_base_0" if user.items.gear.equipped[item.type] is lostItem
user.items.gear.costume[item.type] = "#{item.type}_base_0" if user.items.gear.costume[item.type] is lostItem
user.markModified? 'items.gear'
cb? (if item then {code:200,message:"Your #{item.text()} broke."} else null), user
cb? (if item then {code:200,message: i18n.t('messageLostItem', {itemText: item.text}, req.language)} else null), user
reset: (req, cb) ->
user.habits = []
@ -413,7 +413,7 @@ api.wrap = (user, main=true) ->
reroll: (req, cb, ga) ->
if user.balance < 1
return cb? {code:401,message: "Not enough gems."}
return cb? {code:401,message: i18n.t('notEnoughGems', req.language)}
user.balance--
_.each user.tasks, (task) ->
unless task.type is 'reward'
@ -425,7 +425,7 @@ api.wrap = (user, main=true) ->
rebirth: (req, cb, ga) ->
# Cost is 8 Gems ($2)
if (user.balance < 2 && user.stats.lvl < 100)
return cb? {code:401,message: "Not enough gems."}
return cb? {code:401,message: i18n.t('notEnoughGems', req.language)}
# only charge people if they are under level 100 - ryan
if user.stats.lvl < 100
user.balance -= 2
@ -497,14 +497,14 @@ api.wrap = (user, main=true) ->
{id} = req.params
{to, from} = req.query
task = user.tasks[id]
return cb?({code:404, message: "Task not found."}) unless task
return cb?({code:404, message: i18n.t('messageTaskNotFound', req.language)}) unless task
return cb?('?to=__&from=__ are required') unless to? and from?
tasks = user["#{task.type}s"]
tasks.splice to, 0, tasks.splice(from, 1)[0]
cb? null, tasks
updateTask: (req, cb) ->
return cb?({code:404,message:"Task not found"}) unless task = user.tasks[req.params?.id]
return cb?({code:404,message:i18n.t('messageTaskNotFound', req.language)}) unless task = user.tasks[req.params?.id]
_.merge task, _.omit(req.body,['checklist','id'])
task.checklist = req.body.checklist if req.body.checklist
task.markModified? 'tags'
@ -512,7 +512,7 @@ api.wrap = (user, main=true) ->
deleteTask: (req, cb) ->
task = user.tasks[req.params?.id]
return cb?({code:404,message:'Task not found'}) unless task
return cb?({code:404,message:i18n.t('messageTaskNotFound', req.language)}) unless task
i = user[task.type + "s"].indexOf(task)
user[task.type + "s"].splice(i, 1) if ~i
cb? null, {}
@ -540,14 +540,14 @@ api.wrap = (user, main=true) ->
updateTag: (req, cb) ->
tid = req.params.id
i = _.findIndex user.tags, {id: tid}
return cb?({code:404,message:'Tag not found'}) if !~i
return cb?({code:404,message:i18n.t('messageTagNotFound', req.language)}) if !~i
user.tags[i].name = req.body.name
cb? null, user.tags[i]
deleteTag: (req, cb) ->
tid = req.params.id
i = _.findIndex user.tags, {id: tid}
return cb?({code:404,message:'Tag not found'}) if !~i
return cb?({code:404,message:i18n.t('messageTagNotFound', req.language)}) if !~i
tag = user.tags[i]
delete user.filters[tag.id]
user.tags.splice i, 1
@ -570,10 +570,10 @@ api.wrap = (user, main=true) ->
[egg, potion] = pet.split('-')
userPets = user.items.pets
return cb?({code:404, message:":pet not found in user.items.pets"}) unless userPets[pet]
return cb?({code:404, message:":food not found in user.items.food"}) unless user.items.food?[food.key]
return cb?({code:401, message:"Can't feed this pet."}) if content.specialPets[pet] or (egg is "Egg")
return cb?({code:401, message:"You already have that mount. Try feeding another pet."}) if user.items.mounts[pet]
return cb?({code:404, message:i18n.t('messagePetNotFound', req.language)}) unless userPets[pet]
return cb?({code:404, message:i18n.t('messageFoodNotFound', req.language)}) unless user.items.food?[food.key]
return cb?({code:401, message:i18n.t('messageCannotFeedPet', req.language)}) if content.specialPets[pet] or (egg is "Egg")
return cb?({code:401, message:i18n.t('messageAlreadyMount', req.language)}) if user.items.mounts[pet]
message = ''
evolve = ->
@ -581,17 +581,17 @@ api.wrap = (user, main=true) ->
# changed to -1 to mark "owned" pets
user.items.mounts[pet] = true
user.items.currentPet = "" if pet is user.items.currentPet
message = "You have tamed #{egg}, let's go for a ride!"
message = i18n.t('messageAlreadyMount', {egg: egg}, req.language)
if food.key is 'Saddle'
evolve()
else
if food.target is potion
userPets[pet] += 5
message = "#{egg} really likes the #{food.text()}!"
message = i18n.t('messageLikesFood', {egg: egg, foodText: food.text}, req.language)
else
userPets[pet] += 2
message = "#{egg} eats the #{food.text()} but doesn't seem to enjoy it."
message = i18n.t('messageDontEnjoyFood', {egg: egg, foodText: food.text}, req.language)
if userPets[pet] >= 50 and !user.items.mounts[pet]
evolve()
user.items.food[food.key]--