Use Polyglot to add spanish translations

This commit is contained in:
Julian Calvento
2019-05-09 17:04:06 -03:00
parent dd48a879c2
commit c1198070bd
16 changed files with 126 additions and 1139 deletions

17
src/locales/en.json Normal file
View File

@ -0,0 +1,17 @@
{
"snake": {
"score": "Score",
"gameOver": "Game Over!",
"anyKey": "Press any key to play again"
},
"tanks": {
"playerWon": "Player %{num} won!",
"player": "Player %{number}",
"health": "Health %{value}",
"angle": "Angle %{value}"
},
"spacecraft": {
"score": "Score"
}
}

16
src/locales/es.json Normal file
View File

@ -0,0 +1,16 @@
{
"snake": {
"score": "Puntaje",
"gameOver": "¡Se acabó el juego!",
"anyKey": "Presioná cualquier tecla para jugar nuevamente"
},
"tanks": {
"playerWon": "¡Ha ganado el jugador %{name}!",
"player": "Jugador %{number}",
"health": "Salud %{value}",
"angle": "Ángulo %{value}"
},
"spacecraft": {
"score": "Puntaje"
}
}

View File

@ -1,5 +1,6 @@
import Unit from './classes/unit';
import Interface from './classes/interface';
import Polyglot from "node-polyglot";
let FRAME = 100;
let ui = new Interface();
@ -23,7 +24,7 @@ point.random();
let score = 0;
let stop = false;
function loop() {
function loop(polyglot) {
if (stop) return;
ui.clear();
@ -46,17 +47,15 @@ function loop() {
}
if (head.collides(snake.slice(2))) {
gameover();
gameover(i18n);
}
ui.cursor.goto(0, 0).yellow().write(`Score: ${score}`);
ui.cursor.goto(0, 0).yellow().write(`${i18n.t('snake.score')}: ${score}`);
ui.cursor.reset();
setTimeout(loop, FRAME);
setTimeout(loop, FRAME, i18n);
}
loop();
ui.onKey('right', () => {
changeDirection(RIGHT);
});
@ -146,8 +145,8 @@ function createPart() {
return part;
}
function gameover() {
const MSG = 'Game Over!';
function gameover(i18n) {
const MSG = i18n.t('snake.gameOver');
ui.cursor.goto(ui.center.x - MSG.length / 2, ui.center.y);
ui.cursor.red();
ui.cursor.bold();
@ -155,7 +154,7 @@ function gameover() {
ui.cursor.reset();
ui.cursor.hex('#f65590');
const RETRY = 'Press any key to play again';
const RETRY = i18n.t('snake.anyKey');
ui.cursor.goto(ui.center.x - RETRY.length / 2, ui.center.y + 2);
ui.write(RETRY);
@ -165,6 +164,14 @@ function gameover() {
}
process.on('exit', function() {
ui.cursor.horizontalAbsolute(0).eraseLine()
ui.cursor.horizontalAbsolute(0).eraseLine();
ui.cursor.show();
});
export default function(language) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
const i18n = new Polyglot({phrases: locale});
loop(i18n);
}

View File

@ -1,5 +1,6 @@
import Unit from './classes/unit';
import Interface from './classes/interface';
import Polyglot from "node-polyglot";
const FRAME = 20;
let ENEMY_SPAWN_RATE = 1000;
@ -20,49 +21,55 @@ explosion.dead = true;
let missles = [];
let enemies = [];
let score = 0;
setInterval(() => {
ui.clear();
player.draw();
export default function(language) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
missles.forEach((missle, i) => {
missle.move(1, 0);
missle.draw();
const i18n = new Polyglot({phrases: locale});
let enemy = missle.collides(enemies)
if (enemy) {
enemy.killed = 1;
enemy.color = 'red';
enemy.shape = '*';
missle.dead = true;
setInterval((i18n) => {
ui.clear();
ENEMY_SPAWN_RATE -= 5;
player.draw();
score++;
}
missles.forEach((missle, i) => {
missle.move(1, 0);
missle.draw();
if (missle.dead) {
missles.splice(i, 1);
}
});
let enemy = missle.collides(enemies);
if (enemy) {
enemy.killed = 1;
enemy.color = 'red';
enemy.shape = '*';
missle.dead = true;
enemies.forEach((enemy, i) => {
// move with speed
enemy.move();
enemy.draw();
ENEMY_SPAWN_RATE -= 5;
if (enemy.dead) {
enemies.splice(i, 1);
}
score++;
}
if (enemy.killed == 3) enemy.dead = true;
if (enemy.killed < 3) enemy.killed++;
})
if (missle.dead) {
missles.splice(i, 1);
}
});
ui.cursor.goto(0, 0).yellow().write(`Score: ${score}`);
ui.cursor.reset();
}, FRAME);
enemies.forEach((enemy, i) => {
// move with speed
enemy.move();
enemy.draw();
if (enemy.dead) {
enemies.splice(i, 1);
}
if (enemy.killed == 3) enemy.dead = true;
if (enemy.killed < 3) enemy.killed++;
})
ui.cursor.goto(0, 0).yellow().write(`${i18n.t('spacecraft.score')}: ${score}`);
ui.cursor.reset();
}, FRAME, i18n);
}
ui.onKey('right', () => {
player.move(1, 0);

View File

@ -1,6 +1,7 @@
import Unit from './classes/unit';
import Tank from './classes/tank';
import Interface from './classes/interface';
import Polyglot from "node-polyglot";
let FRAME = 50;
let ui = new Interface();
@ -14,14 +15,14 @@ two.go(ui.columns - 10, ui.rows);
let stop = false;
function loop() {
function loop(i18n) {
if (stop) return;
ui.clear();
if (one.dead || two.dead) {
let num = one.dead ? '2' : '1';
const msg = `Player ${num} won!`;
const msg = i18n.t('tanks.playerWon', {num: num});
ui.cursor.red();
ui.cursor.bold();
@ -50,12 +51,12 @@ function loop() {
ui.cursor.goto(0, 1);
if (turn() === one) ui.cursor.hex('#54ffff');
ui.write('Player 1');
ui.write(i18n.t('tanks.player', {number: "1"}));
ui.cursor.reset();
ui.cursor.goto(0, 2);
ui.write('Health: ' + one.health);
ui.write(i18n.t('tanks.health', {value: one.health}));
ui.cursor.goto(0, 3);
ui.write('Angle: ' + parseInt(one.angle));
ui.write(i18n.t('tanks.angle', {value: parseInt(one.angle)}));
two.draw();
two.bullets.forEach((bullet, i) => {
@ -75,18 +76,16 @@ function loop() {
ui.cursor.goto(ui.output.columns - 10, 1);
if (turn() === two) ui.cursor.hex('#54ffff');
ui.write('Player 2');
ui.write(i18n.t('tanks.player', {number: "2"}));
ui.cursor.reset();
ui.cursor.goto(ui.output.columns - 10, 2);
ui.write('Health: ' + two.health);
ui.write(i18n.t('tanks.health', {value: two.health}));
ui.cursor.goto(ui.output.columns - 10, 3);
ui.write('Angle: ' + parseInt(two.angle));
ui.write(i18n.t('tanks.angle', {value: parseInt(two.angle)}));
setTimeout(loop, FRAME);
setTimeout(loop, FRAME, i18n);
}
loop();
ui.onKey('down', () => {
if (immoblize) return;
@ -142,3 +141,11 @@ function turn() {
if (TURN) return one;
return two;
}
export default function(language) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
const i18n = new Polyglot({phrases: locale});
loop(i18n);
}