Skip to content

Commit

Permalink
Merge pull request #803 from zirreal/robonomics-wiki-test
Browse files Browse the repository at this point in the history
gridsome.server update
  • Loading branch information
zirreal authored Mar 11, 2024
2 parents 182eab7 + ef2729a commit 9f5d41b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 90 deletions.
81 changes: 81 additions & 0 deletions functions/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// This helps translate md blog posts
module.exports = function (fs, path) {

// all locales that we need for translations
const fullLocales = [
{
abbr: 'ru',
full: 'Russian'
},
{
abbr: 'zh',
full: 'Chinese'
},
{
abbr: 'ja',
full: 'Japanese'
},
{
abbr: 'ko',
full: 'Korean'
},
{
abbr: 'de',
full: 'German'
},
{
abbr: 'es',
full: 'Spanish'
},
{
abbr: 'it',
full: 'Italian'
},
{
abbr: 'fr',
full: 'French'
},
{
abbr: 'pt',
full: 'Portuguese'
},
{
abbr: 'uk',
full: 'Ukrainian'
},
{
abbr: 'ar',
full: 'Arabic'
},
{
abbr: 'el',
full: 'Greek'
}
];

// NOTE - before post translation you need to copy default (english) post to all locales folders.

// code below translates blog post (if translation exists) for specific locale
fullLocales.map(l => {

if(!fs.existsSync(`docs/${l.abbr}/${path}.md`, 'utf-8')) {
console.log('file not exists')
} else {
let data = fs.readFileSync(`docs/${l.abbr}/${path}.md`, 'utf-8');
const post = JSON.parse(fs.readFileSync(`./translations/docs/${l.full}.json`));
const strings = Object.keys(post);

// this part replaces english text with the needed one
strings.forEach(el => {
if(data.includes(el)) {
data = data.replace(el, post[el]);
}
});

// and this part rewrites file and adding translated text instead of default one
fs.writeFile(`docs/${l.abbr}/${path}.md`, data, 'utf8', function (err) {
if (err) return console.log(err);
});
}
})
}
96 changes: 6 additions & 90 deletions gridsome.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,10 @@ const createImage = require("node-html-to-image");
const generateHtml = require("./functions/generateHtml");
const locales = ["ru", "zh", "es", "ko", "de", "ja", "pt", "el", "it", "fr", "uk", "ar"];

let allPossiblePaths = [];

// all docs locales for translation
let arDocsFile = fs.readFileSync('./translations/docs/Arabic.json');
let arDocs = JSON.parse(arDocsFile);

let zhDocsFile = fs.readFileSync('./translations/docs/Chinese.json');
let zhDocs = JSON.parse(zhDocsFile);

let ruDocsFile = fs.readFileSync('./translations/docs/Russian.json');
let ruDocs = JSON.parse(ruDocsFile);

let frDocsFile = fs.readFileSync('./translations/docs/French.json');
let frDocs = JSON.parse(frDocsFile);

let esDocsFile = fs.readFileSync('./translations/docs/Spanish.json');
let esDocs = JSON.parse(esDocsFile);

let itDocsFile = fs.readFileSync('./translations/docs/Italian.json');
let itDocs = JSON.parse(itDocsFile);

let jaDocsFile = fs.readFileSync('./translations/docs/Japanese.json');
let jaDocs = JSON.parse(jaDocsFile);

let koDocsFile = fs.readFileSync('./translations/docs/Korean.json');
let koDocs = JSON.parse(koDocsFile);
// for docs ai translations
const translateDoc = require("./functions/translations");

let ukDocsFile = fs.readFileSync('./translations/docs/Ukrainian.json');
let ukDocs = JSON.parse(ukDocsFile);

let elDocsFile = fs.readFileSync('./translations/docs/Greek.json');
let elDocs = JSON.parse(elDocsFile);

let deDocsFile = fs.readFileSync('./translations/docs/German.json');
let deDocs = JSON.parse(deDocsFile);

let ptDocsFile = fs.readFileSync('./translations/docs/Portuguese.json');
let ptDocs = JSON.parse(ptDocsFile);
let allPossiblePaths = [];


const defaultOptions = {
Expand All @@ -68,10 +34,6 @@ const defaultOptions = {
};

module.exports = function (api) {
// api.loadSource(({ addCollection }) => {
// // Use the Data store API here: https://gridsome.org/docs/data-store-api/
// })

// Keeping this for easy modifications from gridsome-plugin-blog-cover
const options = { ...defaultOptions };
api.loadSource(async (actions) => {
Expand All @@ -91,8 +53,7 @@ module.exports = function (api) {
locale = "en";
}

// creating images for only one locale at a time so it won't crash
// if(imgLang === 'en') {

const output = `${options.outputDir}${imgName}-${locale}.png`;
// Only generate images for files that don't exist already
console.log("Generating Missing Cover Images");
Expand All @@ -109,7 +70,6 @@ module.exports = function (api) {
console.log(`The image ${imgName} already exists!`)
}
});
// }
}
});

Expand Down Expand Up @@ -137,57 +97,14 @@ module.exports = function (api) {
api.createManagedPages( ({ createPage }) => {

allPossiblePaths.forEach(node => {
// keys for all locales for translation
const allAr = Object.keys(arDocs);
const allDe = Object.keys(deDocs);
const allEl = Object.keys(elDocs);
const allEs = Object.keys(esDocs);
const allFr = Object.keys(frDocs);
const allIt = Object.keys(itDocs);
const allJa = Object.keys(jaDocs);
const allKo = Object.keys(koDocs);
const allPt = Object.keys(ptDocs);
const allRu = Object.keys(ruDocs);
const allUk = Object.keys(ukDocs);
const allZh = Object.keys(zhDocs);

const path = node.path.slice(0, -1).split("/").pop();

// code below translates blog post (if translation exists) for specific locale (e.g arabic)
// change path and object for another locale to crate translation
// let data = fs.readFileSync(`docs/ar/${path}.md`, 'utf-8');
// this part replaces english text with the needed one
// allAr.forEach(el => {
// if(data.includes(el)) {
// data = data.replace(el,arDocs[el]);
// }
// });

// and this part rewrites file and adding translated text instead of default one
// fs.writeFile(`docs/ar/${path}.md`, data, 'utf8', function (err) {
// if (err) return console.log(err);
// });

// to automatically cope files to all locales
// locales.forEach(l => {
// fs.copyFile(`docs/${path}.md`, `docs/${l}/${path}.md`, (err) => {
// if (err) throw err;
// console.log('file ws copied');
// });
// })
// for docs translations
// translateDoc(fs, path)
})
})

// use it for plugin
// api.createPages(({ createPage }) => {
// createPage({
// path: '/ru/',
// component: './src/pages/Index.vue'
// })
// })




api.createPages(({createPage}) => {

Expand Down Expand Up @@ -236,7 +153,6 @@ module.exports = function (api) {
for (const rule of redirects) {
rules.push(`/summary/:title\t${rule.to}\t${rule.status}`)
}
// fs.appendFileSync('./dist/_redirects', rules.join('\n'))
}
})
}

0 comments on commit 9f5d41b

Please sign in to comment.