Made it work

This commit is contained in:
Arne van Iterson 2020-02-20 13:08:13 +01:00
parent 2817cd1c22
commit a359e0b5e1
13 changed files with 290 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/node_modules/ /node_modules/
/result/*

5
README.md Normal file
View File

@ -0,0 +1,5 @@
## Polytranslatia
### Easily make your own language file for the Polytopia by Midjiwan.
bash```npm install
npm start```

33
css/styles.css Normal file
View File

@ -0,0 +1,33 @@
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
max-width: 500px;
position: relative;
margin: 0 auto;
}
body h1 {
font-weight: lighter;
margin-bottom: 0;
}
body h3 {
margin-top: 0;
font-weight: lighter;
}
body input {
margin: 5px;
}
body footer {
position: fixed;
width: 220px;
left: calc(50% - 110px);
bottom: 10px;
}
body footer a {
color: black;
}
/*# sourceMappingURL=styles.css.map */

9
css/styles.css.map Normal file
View File

@ -0,0 +1,9 @@
{
"version": 3,
"mappings": "AAAA,AAAA,IAAI,CAAC;EACD,WAAW,EAAE,4BAA4B;EACzC,UAAU,EAAE,MAAM;EAClB,SAAS,EAAE,KAAK;EAChB,QAAQ,EAAE,QAAQ;EAClB,MAAM,EAAE,MAAM;CAyBjB;;AA9BD,AAOI,IAPA,CAOA,EAAE,CAAC;EACC,WAAW,EAAE,OAAO;EACpB,aAAa,EAAE,CAAC;CACnB;;AAVL,AAYI,IAZA,CAYA,EAAE,CAAC;EACC,UAAU,EAAE,CAAC;EACb,WAAW,EAAE,OAAO;CACvB;;AAfL,AAiBI,IAjBA,CAiBA,KAAK,CAAC;EACF,MAAM,EAAE,GAAG;CACd;;AAnBL,AAqBI,IArBA,CAqBA,MAAM,CAAC;EACH,QAAQ,EAAE,KAAK;EACf,KAAK,EAAE,KAAK;EACZ,IAAI,EAAE,iBAAiB;EACvB,MAAM,EAAE,IAAI;CAIf;;AA7BL,AA0BQ,IA1BJ,CAqBA,MAAM,CAKF,CAAC,CAAC;EACE,KAAK,EAAE,KAAK;CACf",
"sources": [
"styles.scss"
],
"names": [],
"file": "styles.css"
}

31
css/styles.scss Normal file
View File

@ -0,0 +1,31 @@
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
max-width: 500px;
position: relative;
margin: 0 auto;
h1 {
font-weight: lighter;
margin-bottom: 0;
}
h3 {
margin-top: 0;
font-weight: lighter;
}
input {
margin: 5px;
}
footer {
position: fixed;
width: 220px;
left: calc(50% - 110px);
bottom: 10px;
a {
color: black;
}
}
}

36
index.html Normal file
View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Poliatranslatia</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
<h1>Poliatranslatia</h1>
<h3>Easily make your own language file for the Polytopia by Midjiwan.</h3>
<form action="/" method="POST">
<label for="sourceUrl">Source JSON:</label><br>
<input name="sourceUrl" id="sourceUrl" type="text" placeholder="URL to source JSON" required="true"><br>
<button id="useDefault">Use default en_US</button><br><br>
<label for="target">Target Language:</label><br>
<input type="target" name="target" id="target" required="true"><br>
<button type="submit">Submit</button>
</form>
<script>
$('#useDefault').on('click', function(e) {
e.preventDefault();
$('#sourceUrl').val("http://midjiwan.com/lang/en_US.json");
});
</script>
<footer>
Made by McArn<br>
Powered by <a href="http://translate.yandex.com/">Yandex.Translate</a>
</footer>
</body>
</html>

View File

@ -0,0 +1,85 @@
const express = require('express')
const app = express()
const axios = require('axios').default;
const fs = require('fs');
var mustacheExpress = require('mustache-express');
const port = 7659;
const apiKey = "trnsl.1.1.20200219T163804Z.dd385eb08e22b0a9.628dfdf2e50cf9faa61898cb7195740bb3ac3b86";
// Register '.mustache' extension with The Mustache Express
app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.use(express.urlencoded());
app.get('/', function(req, res) {
axios.get('https://translate.yandex.net/api/v1.5/tr.json/getLangs', {
params: {
key: apiKey
}
})
.then(function (response) {
res.render('index', response.data)
})
.catch(function (error) {
data = {
error: error
};
res.render('error', data)
})
});
app.get('/css/:file', function(req, res) {
res.sendFile(__dirname + '/./css/' + req.params.file);
});
app.get('/result/:file', function(req, res) {
res.sendFile(__dirname + '/./result/' + req.params.file);
});
app.post('/', function(req, res) {
var filename = "/result/" + Math.floor(Date.now() / 1000) + ".json";
axios.get(req.body.sourceUrl)
.then(function (response) {
var json = response.data;
var result = {};
let promises = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
promises.push(
axios.get('https://translate.yandex.net/api/v1.5/tr.json/translate', {
params: {
key: apiKey,
lang: req.body.target,
text: json[key],
target: key
}
})
.then(function (response) {
targetKey = response.request.path.match("(target=)(.+)")[2];
result[targetKey] = response.data.text[0]
})
);
}
}
Promise.all(promises).then(function () {
fs.writeFile(__dirname + filename, JSON.stringify(result));
var data = {
'link': filename
}
res.render('result', data);
});
})
.catch(function (error) {
data = {
error: error
};
res.render('error', data)
})
});
app.listen(port, () => console.log(`App listening on port ${port}`))

View File

@ -4,7 +4,7 @@
"description": "Translate Polytopia language files to any language you want!", "description": "Translate Polytopia language files to any language you want!",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "start": "nodemon index.js"
}, },
"author": "McArn", "author": "McArn",
"license": "ISC" "license": "ISC"

22
views/error.mustache Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Poliatranslatia</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
{{> partials/header}}
<h3>Something went wrong</h3>
<code>
{{error}}
</code>
{{> partials/footer}}
</body>
</html>

41
views/index.mustache Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Poliatranslatia</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
{{> partials/header}}
<form action="/" method="POST">
<label for="sourceUrl">Source JSON:</label><br>
<input name="sourceUrl" id="sourceUrl" type="text" placeholder="URL to source JSON" required="true"><br>
<button id="useDefault">Use default en_US</button><br><br>
<label for="target">Translation direction</label><br>
<p>For example:<br>From en_US default to dutch, use en_nl.</p>
<select name="target" id="targetLanguage">
{{#dirs}}
<option value={{.}}>{{.}}</option>
{{/dirs}}
</select>
<br><br>
<button type="submit">Submit</button>
</form>
<script>
$('#useDefault').on('click', function(e) {
e.preventDefault();
$('#sourceUrl').val("http://midjiwan.com/lang/en_US.json");
});
</script>
{{> partials/footer}}
</body>
</html>

View File

@ -0,0 +1,4 @@
<footer>
Made by McArn<br>
Powered by <a href="http://translate.yandex.com/">Yandex.Translate</a>
</footer>

View File

@ -0,0 +1,2 @@
<h1>Poliatranslatia</h1>
<h3>Easily make your own language file for the Polytopia by Midjiwan.</h3>

20
views/result.mustache Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Poliatranslatia</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
{{> partials/header}}
<p>Enter the following link into Polytopia to start using your custom language file. You can also share it with your friends!</p>
<a href="{{link}}">{{link}}</a>
{{> partials/footer}}
</body>
</html>