forked from evershopcommerce/evershop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server side translation evershopcommerce#305
- Loading branch information
Showing
18 changed files
with
123 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { | ||
loadCsvTranslationFiles | ||
} = require('../../webpack/loaders/loadTranslationFromCsv'); | ||
|
||
var csvData = undefined; | ||
|
||
/** | ||
* This function is used to translate the text form server side, like from middleware. For templating use the _ function | ||
*/ | ||
module.exports.translate = function translate(enText, values = {}) { | ||
const translatedText = | ||
csvData[enText] !== undefined ? csvData[enText] : enText; | ||
// Check if the data is null, undefined or empty object | ||
if (!values || Object.keys(values).length === 0) { | ||
return translatedText; | ||
} else { | ||
const template = `${translatedText}`; | ||
return template.replace(/\${(.*?)}/g, (match, key) => | ||
values[key.trim()] !== undefined ? values[key.trim()] : match | ||
); | ||
} | ||
}; | ||
|
||
module.exports.loadCsv = async function loadCsv() { | ||
// Only load the csv files once | ||
if (csvData === undefined) { | ||
csvData = await loadCsvTranslationFiles(); | ||
} else { | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const fs = require('fs'); | ||
const csv = require('csv-parser'); | ||
|
||
async function readCsvFile(filePath) { | ||
return new Promise((resolve, reject) => { | ||
const results = {}; | ||
fs.createReadStream(filePath) | ||
.pipe(csv({ headers: false })) | ||
.on('data', (data) => { | ||
// Skip the first row (headers) | ||
if (!data[0].startsWith('#')) { | ||
results[data[0]] = data[1]; | ||
} | ||
}) | ||
.on('end', () => { | ||
resolve(results); | ||
}) | ||
.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports.readCsvFile = readCsvFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { | ||
loadCsv | ||
} = require('@evershop/evershop/src/lib/locale/translate/translate'); | ||
|
||
module.exports = async () => { | ||
await loadCsv(); | ||
}; |
7 changes: 5 additions & 2 deletions
7
packages/evershop/src/modules/base/pages/global/[notification]notFound[response].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters