Skip to content

Commit

Permalink
language improved
Browse files Browse the repository at this point in the history
  • Loading branch information
luiseduardobrito committed Sep 25, 2013
1 parent 8ed3f0f commit a12e48d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
11 changes: 7 additions & 4 deletions api/controllers/error.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
var response = require("../adapters/response");

var language = require("../../language");
var lang = language.getDefault();

module.exports = {

index: function(req, res) {

response(res).json({

result: "error",
result: lang.response.error || "error",
code: 500,
message: "unknown"
message: lang.error.unknown || "unknown"

}, 500);
},
Expand All @@ -17,9 +20,9 @@ module.exports = {

response(res).json({

result: "error",
result: lang.response.error || "error",
code: 404,
message: "not found"
message: lang.error.not_found || "not found"

}, 404);
}
Expand Down
6 changes: 4 additions & 2 deletions language/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
// Error Handling
error: {

unknown: "Unknown error",
unknown: "Unknown error: %s",

// Error Levels
critical: "Critical",
Expand Down Expand Up @@ -65,6 +65,8 @@ module.exports = {
login_error: "User could not be logged in",

logout_success: "User logged out successfully",
logout_error: "User could not be logged out"
logout_error: "User could not be logged out",

forbidden_access: "Forbidden access. You're not a root user"
}
}
30 changes: 30 additions & 0 deletions language/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var config = require("../config/general");
var vsprintf = require("format").vsprintf;

var Language = function(default_lang) {

Expand All @@ -19,6 +20,35 @@ var Language = function(default_lang) {

}; exports.get = get;

var val = function() {

if(!arguments.length)
throw new Error("No string id supplied");

// args: id, var1, var2, var3
var id = arguments[0];
id = id.split('.') || id;

var args = [];

for(var k in arguments)
args.push(arguments[k]);

var str = getDefault()[id[0] || id + ""];

for(var i = 1; i < id.length; i++)
str = str[id[i]];

if(!args.length)
return str;
else
return vsprintf(str, args);

};

exports.val = val;
exports.value = val;

var getDefault = function() {

var lang = config.language ||
Expand Down
4 changes: 3 additions & 1 deletion language/pt-BR.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ module.exports = {
login_error: "O login não pode ser efetuado",

logout_success: "Logout efetuado com sucesso",
logout_error: "O logout não pode ser efetuado"
logout_error: "O logout não pode ser efetuado",

forbidden_access: "Acesso restrito, vocẽ não tem permissões administrativas"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"async": "~0.2.9",
"express-ejs-layouts": "~0.3.1",
"ejs": "~0.8.4",
"forever": "~0.10.8"
"forever": "~0.10.8",
"format": "~0.2.1"
},
"devDependencies": {
"nodeunit": "~0.8.1"
Expand Down
6 changes: 6 additions & 0 deletions tests/language/language_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ exports.test_simpleError = function(test) {
var lang = language.get("en-US");
test.ok(lang.error.unknown);
test.done();
}

exports.test_simpleValTest = function(test) {

test.ok(language.val("error.unknown"));
test.done();
}

0 comments on commit a12e48d

Please sign in to comment.