Skip to content

Commit

Permalink
Merge pull request #7 from a4vi2r/i18n
Browse files Browse the repository at this point in the history
Add internationalization methods
  • Loading branch information
uberspot authored Mar 1, 2017
2 parents 3ee5ab5 + 4e9733d commit 7986ea0
Show file tree
Hide file tree
Showing 13 changed files with 7,273 additions and 1,046 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.sass-cache/

# Eclipse project files
.project
.settings/
17 changes: 9 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<script src="js/i18n.js"></script>
<div class="container">
<div class="heading">
<h1 class="title">2048</h1>
Expand All @@ -30,20 +31,20 @@ <h1 class="title">2048</h1>
<div id="night" style="padding:10px;">
<a id="nightbtn"><img src="style/night.png"></a>
</div>
<p class="game-intro">Join the numbers and get to the <strong>2048 tile!</strong></p>
<a class="restart-button">New Game</a>
<a class="undo-button">Undo</a>
<p class="game-intro"><script>document.write(i18n.get('intro'))</script></p>
<a class="restart-button"><script>document.write(i18n.get('new_game'))</script></a>
<a class="undo-button"><script>document.write(i18n.get('undo'))</script></a>
</div>

<div class="game-container">
<div class="game-message">
<p></p>
<div class="lower">
<a class="keep-playing-button">Keep going</a>
<a class="retry-button">Try again</a>
<a class="confirm-button">OK</a>
<a class="undo-move-button">Undo</a>
<a class="cancel-button">Cancel</a>
<a class="keep-playing-button"><script>document.write(i18n.get('keep_going'))</script></a>
<a class="retry-button"><script>document.write(i18n.get('try_again'))</script></a>
<a class="confirm-button"><script>document.write(i18n.get('ok'))</script></a>
<a class="undo-move-button"><script>document.write(i18n.get('undo'))</script></a>
<a class="cancel-button"><script>document.write(i18n.get('cancel'))</script></a>
</div>
</div>

Expand Down
6 changes: 3 additions & 3 deletions js/html_actuator.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ HTMLActuator.prototype.updateBestScore = function (bestScore) {

HTMLActuator.prototype.message = function (won) {
var type = won ? "game-won" : "game-over";
var message = won ? "You win!" : "Game over!";
var message = won ? i18n.get('you_win') : i18n.get('game_over');

this.messageContainer.classList.add(type);
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
};

HTMLActuator.prototype.promptRestart = function () {
this.clearMessage();
var message = "Start a new game?";
var message = i18n.get('start_a_new_game');
this.messageContainer.classList.add("restart-game");
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
};
Expand All @@ -160,7 +160,7 @@ HTMLActuator.prototype.clearMessage = function () {

HTMLActuator.prototype.promptUndo = function () {
this.clearMessage();
var message = "Undo the current move?";
var message = i18n.get('undo_the_current_move');
this.messageContainer.classList.add("undo-move");
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
};
108 changes: 108 additions & 0 deletions js/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Internationalization methods for '2048' game.
*
* @author Igor A. <[email protected]>
*/

'use strict';

var Localizer = (function(lang) {
var defaultMsgs = {
intro : 'Join the numbers and get to the <strong>2048 tile!</strong>',
new_game : 'New Game',
undo : 'Undo',
keep_going : 'Keep going',
try_again : 'Try again',
ok : 'OK',
cancel : 'Cancel',
start_a_new_game : 'Start a new game?',
undo_the_current_move : 'Undo the current move?',
you_win : 'You win!',
game_over : 'Game over!'
};

var localizedMsgs = {};

this.lang = lang || 'en';

switch (this.lang) {
case 'ru':
localizedMsgs = {
intro : 'Объединяйте числа и получите <strong>2048!</strong>',
new_game : 'Новая игра',
undo : 'Отменить ход',
keep_going : 'Продолжайте',
try_again : 'Попробовать ещё раз',
ok : 'ОК',
cancel : 'Отмена',
start_a_new_game : 'Начать новую игру?',
undo_the_current_move : 'Отменить текущий ход?',
you_win : 'Вы победили!',
game_over : 'Игра закончена!'
};
break;
case 'uk':
localizedMsgs = {
intro : 'Об\'єднуйте числа і отримаєте <strong>2048!</strong>',
new_game : 'Нова гра',
undo : 'Скасувати хід',
keep_going : 'Продовжуйте',
try_again : 'Спробувати ще раз',
ok : 'ОК',
cancel : 'Відміна',
start_a_new_game : 'Почати нову гру?',
undo_the_current_move : 'Скасувати поточний хід?',
you_win : 'Ви виграли!',
game_over : 'Гра завершена!'
};
break;
}

// In Android (ES5 and earlier) we can not use Object.assign method
this.messages = mergeObjects(defaultMsgs, localizedMsgs);

/**
* Return localized string by key or default english string if key not
* found.
*/
this.get = function(key) {
if (typeof this.messages[key] === 'string') {
return this.messages[key];
}
return '';
};

return this;
});

/**
* Overwrites o1's values with o2's and adds o2's if non existent in o1.
*/
function mergeObjects(o1, o2) {
var r = {};
for ( var attrname in o1) {
r[attrname] = o1[attrname];
}
for ( var attrname in o2) {
r[attrname] = o2[attrname];
}
return r;
}

/**
* Retrieve GET parameter's value from query.
*/
function findGetParameter(name) {
var r = null, tmp = [];
var items = location.search.substr(1).split('&');
for (var index = 0; index < items.length; index++) {
tmp = items[index].split('=');
if (tmp[0] === name) {
r = decodeURIComponent(tmp[1]);
}
}
return r;
}

// Usage: i18n.get('key')
var i18n = new Localizer(findGetParameter('lang'));
Binary file modified style/fonts/ClearSans-Bold-webfont.eot
Binary file not shown.
Loading

0 comments on commit 7986ea0

Please sign in to comment.