Finalize weblate flow

This commit is contained in:
pietervdvn 2021-05-19 16:15:12 +02:00
parent 2b1c53dc23
commit 14b8beacdc
4 changed files with 61 additions and 1197 deletions

View file

@ -84,6 +84,8 @@ To develop or deploy a version of MapComplete, have a look [to the guide](Docs/D
The core strings of MapComplete are translated on [Hosted Weblate](https://hosted.weblate.org/projects/mapcomplete/core/). The core strings of MapComplete are translated on [Hosted Weblate](https://hosted.weblate.org/projects/mapcomplete/core/).
[![Translation status](https://hosted.weblate.org/widgets/mapcomplete/-/multi-blue.svg)](https://hosted.weblate.org/engage/mapcomplete/)
A theme has translations into the preset.json (`assets/themes/themename/themename.json`). To add a translation: A theme has translations into the preset.json (`assets/themes/themename/themename.json`). To add a translation:
0. Fork this repository 0. Fork this repository

File diff suppressed because it is too large Load diff

View file

@ -1,53 +0,0 @@
import ScriptUtils from "./ScriptUtils";
import {readFileSync, writeFileSync} from "fs";
class TranslationPart {
contents: Map<string, TranslationPart | string> = new Map<string, TranslationPart | string>()
add(language: string, obj: any){
for (const key in obj) {
const v = obj[key]
if(!this.contents.has(key)){
this.contents.set(key, new TranslationPart())
}
const subpart = this.contents.get(key) as TranslationPart
if(typeof v === "string"){
subpart.contents.set(language, v)
}else{
subpart.add(language, v)
}
}
}
toJson(): string{
const parts = []
for (let key of Array.from(this.contents.keys()) ){
const value = this.contents.get(key);
if(typeof value === "string"){
parts.push(`\"${key}\": \"${value}\"`)
}else{
parts.push(`\"${key}\": ${(value as TranslationPart).toJson()}`);
}
}
return JSON.stringify(JSON.parse(`{${parts.join(",")}}`), null, " ");
}
}
const translations = ScriptUtils.readDirRecSync("./langs")
.filter(path => path.indexOf(".json") > 0)
const allTranslations = new TranslationPart()
for (const translationFile of translations) {
const contents = JSON.parse(readFileSync(translationFile, "utf-8"));
let language = translationFile.substring(translationFile.lastIndexOf("/") + 1)
language = language.substring(0, language.length-5)
allTranslations.add(language, contents)
}
writeFileSync("./assets/translations.json", allTranslations.toJson())

View file

@ -1,5 +1,44 @@
import * as fs from "fs"; import * as fs from "fs";
import {Utils} from "../Utils"; import {Utils} from "../Utils";
import ScriptUtils from "./ScriptUtils";
import {readFileSync, writeFileSync} from "fs";
class TranslationPart {
contents: Map<string, TranslationPart | string> = new Map<string, TranslationPart | string>()
add(language: string, obj: any){
for (const key in obj) {
const v = obj[key]
if(!this.contents.has(key)){
this.contents.set(key, new TranslationPart())
}
const subpart = this.contents.get(key) as TranslationPart
if(typeof v === "string"){
subpart.contents.set(language, v)
}else{
subpart.add(language, v)
}
}
}
toJson(): string{
const parts = []
for (let key of Array.from(this.contents.keys()) ){
const value = this.contents.get(key);
if(typeof value === "string"){
parts.push(`\"${key}\": \"${value}\"`)
}else{
parts.push(`\"${key}\": ${(value as TranslationPart).toJson()}`);
}
}
return JSON.stringify(JSON.parse(`{${parts.join(",")}}`), null, " ");
}
}
function isTranslation(tr: any): boolean { function isTranslation(tr: any): boolean {
for (const key in tr) { for (const key in tr) {
@ -31,7 +70,7 @@ function transformTranslation(obj: any, depth = 1) {
} }
function genTranslations() { function genTranslations() {
const translations = JSON.parse(fs.readFileSync("./assets/translations.json", "utf-8")) const translations = JSON.parse(fs.readFileSync("./assets/generated/translations.json", "utf-8"))
const transformed = transformTranslation(translations); const transformed = transformTranslation(translations);
let module = `import {Translation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`; let module = `import {Translation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`;
@ -43,4 +82,23 @@ function genTranslations() {
} }
// Read 'lang/*.json', writes to 'assets/generated/translations.json'
function compileTranslationsFromWeblate(){
const translations = ScriptUtils.readDirRecSync("./langs")
.filter(path => path.indexOf(".json") > 0)
const allTranslations = new TranslationPart()
for (const translationFile of translations) {
const contents = JSON.parse(readFileSync(translationFile, "utf-8"));
let language = translationFile.substring(translationFile.lastIndexOf("/") + 1)
language = language.substring(0, language.length-5)
allTranslations.add(language, contents)
}
writeFileSync("./assets/generated/translations.json", allTranslations.toJson())
}
compileTranslationsFromWeblate();
genTranslations() genTranslations()