Improve i18n editor.

- Add a one-click `download JSON` button.
- Add a `New language` button.

Add secondary editor InLang (remote service) to docs.

Closes #1266, #1267.
This commit is contained in:
Kailash Nadh 2023-04-06 11:57:57 +05:30
parent 71a9138b23
commit 7db3d7da0f
4 changed files with 98 additions and 45 deletions

View file

@ -17,9 +17,17 @@ To customize an existing language or to load a new language, put one or more `.j
## Contributing a new language ## Contributing a new language
### Using the basic editor
- Visit [https://listmonk.app/i18n](https://listmonk.app/i18n) - Visit [https://listmonk.app/i18n](https://listmonk.app/i18n)
- To make changes to an existing language, use the "Load language" option on the top right. - Click on `Createa new language`, or to make changes to an existing language, use `Load language`.
- To create a new language, use the "Load language" option on the top right and select "Default". - Translate the text in the text fields on the UI.
- Translate the text in the input fields on the UI. - Once done, use the `Download raw JSON` to download the language file.
- Once done, use the `Switch to raw JSON` and copy the JSON data and save it to a file named `xx.json`, where `xx` is the two letter code of the language. - Send a pull request to add the file to the [i18n directory on the GitHub repo](https://github.com/knadh/listmonk/tree/master/i18n).
- Send a pull request to add the file to the [i18n directory on the GitHub repo](https://github.com/knadh/listmonk/tree/master/i18n). If you are not familiar with pull requests, share the file by creating a new "issue" (comment) on the [GitHub issues page](https://github.com/knadh/listmonk/issues)
### Using InLang (external service)
- Visit [https://inlang.com/editor/github.com/knadh/listmonk](https://inlang.com/editor/github.com/knadh/listmonk)
- To make changes and push them, you need to log in to GitHub using OAuth and fork the project from the UI.
- Translate the text in the input fields on the UI. You can use the filters to see only the necessary translations.
- Once you're done, push the changes from the UI and click on "Open a pull request." This will take you to GitHub, where you can write a PR message.

View file

@ -13,7 +13,11 @@
<h1 class="title">{{ values["_.name"] }}</h1> <h1 class="title">{{ values["_.name"] }}</h1>
<div class="controls"> <div class="controls">
<div class="import block"> <div class="import block">
<a v-if="!isRawVisible" href="#" @click.prevent="onToggleRaw">Switch to raw JSON</a> <a href="#" @click.prevent="onToggleRaw">
<template v-if="!isRawVisible">Switch to raw JSON</template>
<template v-else>Switch to editor</template>
</a>
<a href="#" @click.prevent="onDownloadJSON">Download raw JSON</a>
<a v-else href="#" @click.prevent="onToggleRaw">Switch to editor</a> <a v-else href="#" @click.prevent="onToggleRaw">Switch to editor</a>
</div> </div>
@ -33,7 +37,7 @@
</div> </div>
<div class="selector block"> <div class="selector block">
Load language Load existing language
<select v-model="loadLang" @change="onLoadLanguage"> <select v-model="loadLang" @change="onLoadLanguage">
<option value="en">Default (en)</option> <option value="en">Default (en)</option>
<option value="ca"> Català (ca) </option> <option value="ca"> Català (ca) </option>
@ -60,6 +64,9 @@
<option value="zh-CN"> 简体中文 (zh-CN) </option> <option value="zh-CN"> 简体中文 (zh-CN) </option>
<option value="zh-TW"> 繁體中文(zh-TW) </option> <option value="zh-TW"> 繁體中文(zh-TW) </option>
</select> </select>
&nbsp;&nbsp;&nbsp;
<a href="#" @click.prevent="onNewLang">+ Create new language</a>
</div> </div>
</div> </div>
</header> </header>

View file

@ -48,7 +48,7 @@ var app = new Vue({
// Is there cached localStorage data? // Is there cached localStorage data?
if (localStorage.data) { if (localStorage.data) {
try { try {
this.loadData(JSON.parse(localStorage.data)); this.populateData(JSON.parse(localStorage.data));
} catch (e) { } catch (e) {
console.log("Bad JSON in localStorage: " + e.toString()); console.log("Bad JSON in localStorage: " + e.toString());
} }
@ -57,7 +57,7 @@ var app = new Vue({
}); });
}, },
loadData(data) { populateData(data) {
// Filter out all keys from data except for the base ones // Filter out all keys from data except for the base ones
// in the base language. // in the base language.
const vals = this.keys.reduce((a, key) => { const vals = this.keys.reduce((a, key) => {
@ -69,6 +69,15 @@ var app = new Vue({
this.saveData(); this.saveData();
}, },
loadLanguage(lang) {
return fetch(BASEURL + lang + ".json").then(response => response.json()).then(data => {
this.populateData(data);
}).catch((e) => {
console.log(e);
alert("error fetching file: " + e.toString());
});
},
saveData() { saveData() {
localStorage.data = JSON.stringify(this.values); localStorage.data = JSON.stringify(this.values);
}, },
@ -87,7 +96,7 @@ var app = new Vue({
this.rawData = JSON.stringify(this.values, Object.keys(this.values).sort(), 4); this.rawData = JSON.stringify(this.values, Object.keys(this.values).sort(), 4);
} else { } else {
try { try {
this.loadData(JSON.parse(this.rawData)); this.populateData(JSON.parse(this.rawData));
} catch (e) { } catch (e) {
alert("error parsing JSON: " + e.toString()); alert("error parsing JSON: " + e.toString());
return false; return false;
@ -102,12 +111,33 @@ var app = new Vue({
return false; return false;
} }
fetch(BASEURL + this.loadLang + ".json").then(response => response.json()).then(data => { this.loadLanguage(this.loadLang);
this.loadData(data); },
}).catch((e) => {
console.log(e); onNewLang() {
alert("error fetching file: " + e.toString()); if (!confirm("Creating a new language will overwrite your local changes. Continue?")) {
}); return false;
}
let data = { ...this.base };
data["_.code"] = "iso-code-here"
data["_.name"] = "New language"
this.populateData(data);
},
onDownloadJSON() {
// Create a Blob using the content, mimeType, and optional encoding
const blob = new Blob([JSON.stringify(this.values, Object.keys(this.values).sort(), 4)], { type: "" });
// Create an anchor element with a download attribute
const link = document.createElement('a');
link.download = `${this.values["_.code"]}.json`;
link.href = URL.createObjectURL(blob);
// Append the link to the DOM, click it to start the download, and remove it
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} }
}, },

View file

@ -12,6 +12,10 @@ h1, h2, h3, h4, h5 {
margin: 0 0 15px 0; margin: 0 0 15px 0;
} }
a {
color: #0055d4;
}
.container { .container {
padding: 30px; padding: 30px;
} }
@ -19,6 +23,10 @@ h1, h2, h3, h4, h5 {
.header { .header {
align-items: center; align-items: center;
margin-bottom: 30px; margin-bottom: 30px;
}
.header a {
display: inline-block;
margin-right: 15px;
} }
.header .controls { .header .controls {
display: flex; display: flex;