Skip to content

Commit 6fc7358

Browse files
authored
Merge pull request #38 from technix/master
Localization + configurable options
2 parents fd14fd5 + 1d78d7c commit 6fc7358

File tree

8 files changed

+179
-29
lines changed

8 files changed

+179
-29
lines changed

src/app/game.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ var Storage = require('./storage');
33
var Game = {
44
id: null,
55
name: 'Default Game',
6-
mute: true,
7-
preload: true,
8-
autosave_on_click: false,
6+
// === configurable options ===
7+
mute: true, // Mute all sounds
8+
preload: true, // Preload all images while game is starting
9+
autosave_on_click: false, // Autosave after each click
10+
log: false, // Enable logging
11+
// === end of configurable options ===
912
autosaveID: 9,
1013
importID: 10,
1114
saveSlots: 5,
@@ -22,6 +25,11 @@ var Game = {
2225
},
2326
saveExists: function saveExists(id) {
2427
return Storage.exists(this.getSaveName(id));
28+
},
29+
loadConfig: function loadConfig(cfg) {
30+
Object.keys(cfg).forEach(function applyConfig(key) {
31+
Game[key] = cfg[key];
32+
});
2533
}
2634
};
2735

src/app/i18n.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var i18n = {
2+
dict: {
3+
loading: 'Loading...',
4+
preload: 'Preloading images',
5+
empty: 'empty',
6+
menu_back: 'Return to game',
7+
menu_save: 'Save',
8+
menu_load: 'Load',
9+
menu_reset: 'Reset',
10+
menu_mute: 'Mute',
11+
menu_unmute: 'Unmute',
12+
menu_savegame: 'Save game',
13+
menu_loadgame: 'Load game',
14+
menu_import: 'Import',
15+
menu_export: 'Export saved game',
16+
menu_autosave: 'Autosave',
17+
menu_cancel: 'Cancel'
18+
},
19+
load: function init(d) {
20+
this.dict = d;
21+
},
22+
t: function translate(phrase) {
23+
if (phrase in this.dict) {
24+
return this.dict[phrase];
25+
}
26+
return phrase;
27+
}
28+
};
29+
30+
module.exports = i18n;

src/app/log.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
var $ = require('jquery');
2+
var Game = require('./game');
23

34
var Logger = {
45
log: function showLog() {
6+
if (!Game.log) {
7+
return; // if log is disabled
8+
}
59
var message = Array.prototype.join.call(arguments, ', ');
610
var style = '';
711
if (message.indexOf('>') === 0) {
@@ -16,6 +20,7 @@ var Logger = {
1620
var el = $('#log');
1721
el.append('<span ' + style + '>' + message + '<br/></span>');
1822
el.scrollTop(function h() { return this.scrollHeight; });
23+
return;
1924
}
2025
};
2126

src/app/manager.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var $ = require('jquery');
22

3+
var i18n = require('./i18n');
34
var Game = require('./game');
45
var Instead = require('./instead');
56
var UI = require('./ui');
@@ -13,7 +14,7 @@ var Manager = {
1314
init: function init() {
1415
this.el = $('#manager');
1516
this.el.perfectScrollbar({wheelSpeed: 1});
16-
this.el.append('<a href="" id="loading">Loading...</a>');
17+
this.el.append('<a href="" id="loading">' + i18n.t('loading') + '</a>');
1718

1819
var self = this;
1920
this.el.on('click', 'a', function selectGame(e) {
@@ -69,7 +70,7 @@ var Manager = {
6970
Preloader.load(
7071
allGames[gameid].preload,
7172
function preloadProgress(percent) {
72-
$('#stead-toolbar-info').html('Preloading images: ' + percent + '%');
73+
$('#stead-toolbar-info').html(i18n.t('preload') + ': ' + percent + '%');
7374
},
7475
function preloadSuccess() {
7576
$('#stead-toolbar-info').html('<b>' + Game.name + '</b>');

src/app/menu.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var $ = require('jquery');
22
var Game = require('./game');
33
var Storage = require('./storage');
4+
var i18n = require('./i18n');
45

56
var UTFsymbol = {
67
mute: '<div style="position:absolute; color: #CC0000; text-shadow: 0px 0px 2px #000000">&#10006;</div>&#128266;',
@@ -13,17 +14,25 @@ var Menu = {
1314
this.element = ui;
1415
var self = this;
1516

17+
// apply menu translations
18+
$('#menu-back').text(i18n.t('menu_back'));
19+
$('#menu-save').text(i18n.t('menu_save'));
20+
$('#menu-load').text(i18n.t('menu_load'));
21+
$('#menu-reset').text(i18n.t('menu_reset'));
22+
$('#menu-export').text(i18n.t('menu_export'));
23+
1624
function toggleMute() {
1725
if (Game.mute) {
1826
steadHandler.mute(false);
19-
ui.$menu_mute.text('Mute');
27+
ui.$menu_mute.text(i18n.t('menu_mute'));
2028
ui.$toolbar_mute.html(UTFsymbol.sound);
2129
} else {
2230
steadHandler.mute(true);
23-
ui.$menu_mute.text('Unmute');
31+
ui.$menu_mute.text(i18n.t('menu_unmute'));
2432
ui.$toolbar_mute.html(UTFsymbol.mute);
2533
}
2634
}
35+
Game.mute = !Game.mute; // set correct mute state before switching
2736
toggleMute();
2837

2938
ui.$menuButton.on('click', this.toggleMenu.bind(this));
@@ -69,10 +78,14 @@ var Menu = {
6978
}
7079
});
7180

72-
ui.$toolbar_log.on('click', function toggleLog(e) {
73-
e.preventDefault();
74-
$('#log').toggle().scrollTop(function sh() { return this.scrollHeight; });
75-
});
81+
if (Game.log) {
82+
ui.$toolbar_log.on('click', function toggleLog(e) {
83+
e.preventDefault();
84+
$('#log').toggle().scrollTop(function sh() { return this.scrollHeight; });
85+
});
86+
} else {
87+
ui.$toolbar_log.hide();
88+
}
7689
ui.$toolbar_mute.on('click', function toggleLog(e) {
7790
e.preventDefault();
7891
toggleMute();
@@ -125,48 +138,48 @@ var Menu = {
125138
});
126139
var i;
127140
if (action === 'save') {
128-
html += '<h3>Save game</h3>';
141+
html += '<h3>' + i18n.t('menu_savegame') + '</h3>';
129142
for (i = 1; i <= Game.saveSlots; i++) {
130143
html += '<a href="" data-action="save" data-id="' + i + '" class="slot-selector">';
131144
if (slots[i]) {
132145
html += i + ' - ' + slots[i];
133146
} else {
134-
html += i + ' - empty';
147+
html += i + ' - ' + i18n.t('empty');
135148
}
136149
html += '</a>';
137150
}
138151
} else if (action === 'load') {
139-
html += '<h3>Load game</h3>';
152+
html += '<h3>' + i18n.t('menu_loadgame') + '</h3>';
140153
html += '<a href="" data-action="load" data-id="' + Game.autosaveID +
141-
'" class="slot-selector">0 - Autosave</a>';
154+
'" class="slot-selector">0 - ' + i18n.t('menu_autosave') + '</a>';
142155
for (i = 1; i <= Game.saveSlots; i++) {
143156
if (slots[i]) {
144157
html += '<a href="" data-action="load" data-id="' + i + '" class="slot-selector">';
145158
html += i + ' - ' + slots[i];
146159
html += '</a>';
147160
} else {
148-
html += '<div class="slot-selector">' + i + ' - empty</div>';
161+
html += '<div class="slot-selector">' + i + ' - ' + i18n.t('empty') + '</div>';
149162
}
150163
}
151-
html += '<h3>Import</h3>';
164+
html += '<h3>' + i18n.t('menu_import') + '</h3>';
152165
html += '<input type="file" id="load-import" style="font-size: 0.8em"/>';
153166
html += '<br><br>';
154167
} else {
155-
html += '<h3>Export saved game</h3>';
168+
html += '<h3>' + i18n.t('menu_export') + '</h3>';
156169
html += '<a href="" data-action="export" data-id="' + Game.autosaveID +
157-
'" class="slot-selector">0 - Autosave</a>';
170+
'" class="slot-selector">0 - ' + i18n.t('menu_autosave') + '</a>';
158171
for (i = 1; i <= Game.saveSlots; i++) {
159172
if (slots[i]) {
160173
html += '<a href="" data-action="export" data-id="' + i + '" class="slot-selector">';
161174
html += i + ' - ' + slots[i];
162175
html += '</a>';
163176
} else {
164-
html += '<div class="slot-selector">' + i + ' - empty</div>';
177+
html += '<div class="slot-selector">' + i + ' - ' + i18n.t('empty') + '</div>';
165178
}
166179
}
167180
}
168181

169-
html += '<a href="" data-action="cancel">Cancel</a>';
182+
html += '<a href="" data-action="cancel">' + i18n.t('menu_cancel') + '</a>';
170183
ui.$menu_saveload.html(html);
171184
ui.$menu_saveload.show();
172185
} else {

src/index.en.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>INSTEAD.js</title>
8+
<script type="text/javascript">
9+
var INSTEADjs = {
10+
mute: true, // Mute all sounds
11+
preload: true, // Preload all images while game is starting
12+
autosave_on_click: false, // Autosave after each click
13+
log: false // Enable logging
14+
};
15+
</script>
16+
<style id="theme_css"></style>
17+
</head>
18+
<body>
19+
<div id="manager">
20+
<h1><img src="./instead.png"> INSTEAD.js</h1>
21+
</div>
22+
<div id="stead-container">
23+
<div id="menu">
24+
<div id="menu-content">
25+
<a href="" id="menu-back" data-action="back"></a>
26+
<a href="" id="menu-save" data-action="menu-save"></a>
27+
<a href="" id="menu-load" data-action="menu-load"></a>
28+
<a href="" id="menu-reset" data-action="reset"></a>
29+
<a href="" id="menu-mute" data-action="mute"></a>
30+
<a href="" id="menu-export" data-action="menu-export"></a>
31+
</div>
32+
<div id="menu-saveload">
33+
</div>
34+
</div>
35+
<div id="stead-toolbar">
36+
<div id="stead-toolbar-info"></div>
37+
<div id="stead-toolbar-buttons">
38+
<button id="toolbar-mute"></button>
39+
<button id="toolbar-log">&#128196;</button>
40+
</div>
41+
</div>
42+
<div id="stead">
43+
<div id="win">
44+
<div id="title"></div>
45+
<div id="picture"></div>
46+
<div id="ways-top"></div>
47+
<div class="wordwrap" id="text"></div>
48+
<div id="ways-bottom"></div>
49+
</div>
50+
<div id="inventory"></div>
51+
<div id="menu_button"><img id="menu_image"></img></div>
52+
</div>
53+
<div id="log"></div>
54+
</div>
55+
56+
</body>
57+
</html>

src/index.html

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
1+
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>INSTEAD.js</title>
8+
<script type="text/javascript">
9+
var INSTEADjs = {
10+
mute: true, // Mute all sounds
11+
preload: true, // Preload all images while game is starting
12+
autosave_on_click: false, // Autosave after each click
13+
log: false, // Enable logging
14+
translation: {
15+
loading: 'Загрузка...',
16+
preload: 'Кэширование картинок',
17+
empty: 'пусто',
18+
menu_back: 'Вернуться в игру',
19+
menu_save: 'Сохранить',
20+
menu_load: 'Загрузить',
21+
menu_reset: 'Начать заново',
22+
menu_mute: 'Выключить звук',
23+
menu_unmute: 'Включить звук',
24+
menu_savegame: 'Сохранить игру',
25+
menu_loadgame: 'Загрузить игру',
26+
menu_import: 'Импорт',
27+
menu_export: 'Экспорт сохранения',
28+
menu_autosave: 'Автосохранение',
29+
menu_cancel: 'Отмена'
30+
}
31+
};
32+
</script>
533
<style id="theme_css"></style>
634
</head>
735
<body>
@@ -11,12 +39,12 @@ <h1><img src="./instead.png"> INSTEAD.js</h1>
1139
<div id="stead-container">
1240
<div id="menu">
1341
<div id="menu-content">
14-
<a href="" data-action="back">Return to game</a>
15-
<a href="" data-action="menu-save">Save</a>
16-
<a href="" data-action="menu-load">Load</a>
17-
<a href="" data-action="reset">Reset</a>
18-
<a href="" id="menu-mute" data-action="mute">Mute</a>
19-
<a href="" data-action="menu-export">Export saved game</a>
42+
<a href="" id="menu-back" data-action="back"></a>
43+
<a href="" id="menu-save" data-action="menu-save"></a>
44+
<a href="" id="menu-load" data-action="menu-load"></a>
45+
<a href="" id="menu-reset" data-action="reset"></a>
46+
<a href="" id="menu-mute" data-action="mute"></a>
47+
<a href="" id="menu-export" data-action="menu-export"></a>
2048
</div>
2149
<div id="menu-saveload">
2250
</div>

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ require('perfect-scrollbar/jquery')($);
77

88
var Manager = require('./app/manager');
99

10+
var i18n = require('./app/i18n');
11+
var Game = require('./app/game');
1012
var Instead = require('./app/instead');
1113
var UI = require('./app/ui');
1214
var Menu = require('./app/menu');
1315

1416
document.addEventListener(
1517
'DOMContentLoaded',
1618
function onLoad() {
19+
if ('INSTEADjs' in window) {
20+
Game.loadConfig(window['INSTEADjs']); // eslint-disable-line dot-notation
21+
if (window['INSTEADjs']['translation']) { // eslint-disable-line dot-notation
22+
i18n.load(window['INSTEADjs']['translation']); // eslint-disable-line dot-notation
23+
}
24+
}
1725
Manager.init();
1826
// initialization of INSTEAD components
1927
Instead.init();

0 commit comments

Comments
 (0)