Skip to content

Commit c39ae3b

Browse files
committed
added language support
1 parent b298716 commit c39ae3b

7 files changed

Lines changed: 115 additions & 232 deletions

File tree

public/css/style.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,11 +795,11 @@ key {
795795
width: min-content;
796796
width: -moz-min-content;
797797
}
798-
.themes{
798+
.themes, .languages{
799799
display: grid;
800800
grid-template-columns: 1fr 1fr 1fr 1fr;
801801
// gap: 1rem;
802-
.theme{
802+
.theme, .language{
803803
// padding: 1rem 2rem;
804804
text-align: center;
805805
color: var(--sub-color);

public/index.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,14 @@ <h1>show key tips</h1>
216216
<div class="text">Shows the keybind tips at the bottom of the page.</div>
217217
<div class="buttons"><div class="button on" tabindex="0">show</div><div class="button off" tabindex="0">hide</div></div>
218218
</div>
219+
<div class="section" style="grid-column: 1/3;">
220+
<h1>languages</h1>
221+
<div class="languages">
222+
</div>
223+
</div>
219224
<div class="section" style="grid-column: 1/3;">
220225
<h1>theme</h1>
221226
<div class="themes">
222-
<div class="theme">light</div>
223-
<div class="theme active">dark</div>
224-
<div class="theme">light</div>
225-
<div class="theme">light</div>
226-
<div class="theme">light</div>
227-
<div class="theme">light</div>
228-
229227
</div>
230228
</div>
231229
</div>
@@ -330,12 +328,11 @@ <h1>theme</h1>
330328
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
331329
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
332330
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@0.1.3/src/chartjs-plugin-trendline.min.js"></script>
333-
331+
<script src="js/words.js"></script>
334332
<script src="js/db.js"></script>
335333
<script src="js/commandline.js"></script>
336334
<script src="js/settings.js"></script>
337335
<script src="js/userconfig.js"></script>
338-
<script src="js/words.js"></script>
339336
<script src="js/script.js"></script>
340337
<script src="js/account.js"></script>
341338

public/js/commandline.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ let commands = {
5151
showCommandLine();
5252
}
5353
},
54+
{
55+
id: "changeLanguage",
56+
display: "Change language...",
57+
subgroup: true,
58+
exec: () => {
59+
currentCommands = commandsLanguages;
60+
showCommandLine();
61+
}
62+
},
5463
{
5564
id: "changeMode",
5665
display: "Change mode...",
@@ -263,6 +272,31 @@ let commandsThemes = {
263272
]
264273
};
265274

275+
let commandsLanguages = {
276+
title: "Change language...",
277+
list: [
278+
{
279+
id: "couldnotload",
280+
display: "Could not load the languages list :("
281+
}
282+
]
283+
};
284+
285+
if (Object.keys(words).length > 0) {
286+
commandsLanguages.list = [];
287+
Object.keys(words).forEach(language => {
288+
commandsLanguages.list.push({
289+
id: "changeLanguage" + capitalizeFirstLetter(language),
290+
display: language.replace('_', ' '),
291+
exec: () => {
292+
changeLanguage(language);
293+
restartTest();
294+
saveConfigToCookie();
295+
}
296+
})
297+
})
298+
}
299+
266300
$("#commandLine input").keyup((e) => {
267301
if (e.keyCode == 38 || e.keyCode == 40) return;
268302
updateSuggestedCommands();

public/js/script.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,28 @@ function initWords() {
7878
inputHistory = [];
7979
currentInput = "";
8080

81+
let language = words[config.language];
82+
83+
if (language == undefined || language == []) {
84+
showNotification("Error generating word list", 3000);
85+
return;
86+
}
87+
8188
if (config.mode == "time" || config.mode == "words") {
8289

8390
let wordsBound = config.mode == "time" ? 50 : config.words;
84-
let randomWord = words[Math.floor(Math.random() * words.length)];
85-
wordsList.push(randomWord);
91+
let randomWord = language[Math.floor(Math.random() * language.length)];
92+
while (randomWord.indexOf(' ') > -1) {
93+
randomWord = language[Math.floor(Math.random() * language.length)];
94+
}
95+
wordsList.push(randomWord.toLowerCase());
8696
for (let i = 1; i < wordsBound; i++) {
87-
randomWord = words[Math.floor(Math.random() * words.length)];
97+
randomWord = language[Math.floor(Math.random() * language.length)];
8898
previousWord = wordsList[i - 1];
89-
while (randomWord == previousWord && (!config.punctuation && randomWord == "I")) {
90-
randomWord = words[Math.floor(Math.random() * words.length)];
99+
while (randomWord == previousWord || (!config.punctuation && randomWord == "I") || randomWord.indexOf(' ') > -1) {
100+
randomWord = language[Math.floor(Math.random() * language.length)];
91101
}
92-
wordsList.push(randomWord);
102+
wordsList.push(randomWord.toLowerCase());
93103
}
94104

95105
} else if (config.mode == "custom") {
@@ -423,7 +433,8 @@ function showResult() {
423433
mode: config.mode,
424434
mode2: mode2,
425435
punctuation: config.punctuation,
426-
timestamp: Date.now()
436+
timestamp: Date.now(),
437+
language: config.language
427438
};
428439
if (stats.wpm > 0 && stats.wpm < 250 && stats.acc > 50 && stats.acc <= 100) {
429440
if (firebase.auth().currentUser != null) {
@@ -452,15 +463,17 @@ function showResult() {
452463

453464

454465
let infoText = "";
455-
infoText = config.mode;
456466

467+
468+
infoText += config.mode;
457469
if (config.mode == "time") {
458470
infoText += " " + config.time
459471
} else if (config.mode == "words") {
460472
infoText += " " + config.words
461473
}
474+
infoText += "<br>" + config.language.replace('_', ' ') ;
462475
if (config.punctuation) {
463-
infoText += " with punctuation"
476+
infoText += "<br>with punctuation"
464477
}
465478

466479
$("#result .stats .info .bottom").html(infoText);

public/js/settings.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ function updateSettingsPage(){
22

33
let themesEl = $(".pageSettings .section .themes").empty();
44
themesList.forEach(theme => {
5-
if (config.theme == 'theme') {
6-
themesEl.append(`<div class="theme active" theme='${theme}'>${theme.replace('_', ' ')}</div>`);
7-
} else {
8-
themesEl.append(`<div class="theme" theme='${theme}'>${theme.replace('_', ' ')}</div>`);
9-
}
5+
themesEl.append(`<div class="theme" theme='${theme}'>${theme.replace('_', ' ')}</div>`);
6+
})
7+
8+
let langEl = $(".pageSettings .section .languages").empty();
9+
Object.keys(words).forEach(language => {
10+
langEl.append(`<div class="language" language='${language}'>${language.replace('_', ' ')}</div>`);
1011
})
1112

1213
setSettingsButton('smoothCaret', config.smoothCaret);
1314
setSettingsButton('quickTab', config.quickTab);
1415
setSettingsButton('liveWpm', config.showLiveWpm);
1516
setSettingsButton('keyTips', config.showKeyTips);
16-
setActiveThemeButton(config.theme);
1717

18+
setActiveThemeButton();
19+
setActiveLanguageButton();
1820

1921
if (config.showKeyTips) {
2022
$(".pageSettings .tip").removeClass('hidden');
@@ -25,9 +27,14 @@ function updateSettingsPage(){
2527

2628
}
2729

28-
function setActiveThemeButton(theme) {
29-
$(".pageSettings .themes .theme").removeClass('active');
30-
$(".pageSettings .themes .theme[theme=" + theme + "]").addClass('active');
30+
function setActiveThemeButton() {
31+
$(`.pageSettings .section .themes .theme`).removeClass('active');
32+
$(`.pageSettings .section .themes .theme[theme=${config.theme}]`).addClass('active');
33+
}
34+
35+
function setActiveLanguageButton() {
36+
$(`.pageSettings .section .languages .language`).removeClass('active');
37+
$(`.pageSettings .section .languages .language[language=${config.language}]`).addClass('active');
3138
}
3239

3340
function setSettingsButton(buttonSection,tf) {
@@ -99,6 +106,7 @@ $(".pageSettings .section.keyTips .buttons .button.off").click(e => {
99106
}
100107
})
101108

109+
//themes
102110
$(document).on("mouseover",".pageSettings .section .themes .theme", (e) => {
103111
let theme = $(e.currentTarget).attr('theme');
104112
previewTheme(theme);
@@ -107,8 +115,17 @@ $(document).on("mouseover",".pageSettings .section .themes .theme", (e) => {
107115
$(document).on("click",".pageSettings .section .themes .theme", (e) => {
108116
let theme = $(e.currentTarget).attr('theme');
109117
setTheme(theme);
118+
setActiveThemeButton();
110119
})
111120

112121
$(document).on("mouseleave",".pageSettings .section .themes", (e) => {
113122
setTheme(config.theme);
123+
})
124+
125+
//languages
126+
$(document).on("click",".pageSettings .section .languages .language", (e) => {
127+
let language = $(e.currentTarget).attr('language');
128+
changeLanguage(language);
129+
restartTest();
130+
setActiveLanguageButton();
114131
})

public/js/userconfig.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ let config = {
77
punctuation: false,
88
words: 50,
99
time: 30,
10-
mode: "words"
10+
mode: "words",
11+
language: "english"
1112
}
1213

1314
//cookies
@@ -28,6 +29,7 @@ function loadConfigFromCookie() {
2829
changeTimeConfig(newConfig.time);
2930
changeWordCount(newConfig.words);
3031
changeMode(newConfig.mode);
32+
changeLanguage(newConfig.language);
3133
config = newConfig;
3234
restartTest();
3335
}
@@ -140,3 +142,14 @@ function setTheme(name) {
140142
theme: name
141143
});
142144
}
145+
146+
function changeLanguage(language) {
147+
if (language == null || language == undefined) {
148+
language = "english";
149+
}
150+
config.language = language;
151+
firebase.analytics().logEvent('changedLanguage', {
152+
language: language
153+
});
154+
saveConfigToCookie();
155+
}

0 commit comments

Comments
 (0)