adding javascript module to connect the frontend to the translation endpoint
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests/3 Pipeline was successful
ci/woodpecker/push/functional-tests/1 Pipeline was successful
ci/woodpecker/push/functional-tests/2 Pipeline was successful
ci/woodpecker/push/functional-tests/4 Pipeline was successful
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests/3 Pipeline was successful
ci/woodpecker/push/functional-tests/1 Pipeline was successful
ci/woodpecker/push/functional-tests/2 Pipeline was successful
ci/woodpecker/push/functional-tests/4 Pipeline was successful
This commit is contained in:
parent
10819b864f
commit
68c6d16232
2 changed files with 54 additions and 0 deletions
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"name": "@digicomp/flow-translation-endpoint",
|
||||||
|
"description": "Connect to Flow's merged and parsed xliff contents",
|
||||||
|
"main": "translate.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"i18n"
|
||||||
|
],
|
||||||
|
"author": "Ferdinand Kuhl",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
// storage for promises with the same ids so the same request is not issued twice
|
||||||
|
const translations = {};
|
||||||
|
|
||||||
|
export function translateIdPatterns(idPatterns)
|
||||||
|
{
|
||||||
|
if (!translations[idPatterns.join(',')]) {
|
||||||
|
translations[idPatterns.join(',')] = getJSON(idPatterns);
|
||||||
|
}
|
||||||
|
return translations[idPatterns.join(',')];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayFromPaths(units)
|
||||||
|
{
|
||||||
|
const result = {};
|
||||||
|
for (const [key, value] of Object.entries(units)) {
|
||||||
|
let cur = result;
|
||||||
|
const parts = key.split('.'),
|
||||||
|
lastPart = parts.pop();
|
||||||
|
|
||||||
|
parts.forEach(function(elem){
|
||||||
|
cur[elem] = cur[elem] || {};
|
||||||
|
cur = cur[elem];
|
||||||
|
});
|
||||||
|
cur[lastPart] = value;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getJSON(idPatterns)
|
||||||
|
{
|
||||||
|
const uri = new URL(document.documentElement.dataset.xliffUri, document.location);
|
||||||
|
uri.searchParams.set(document.documentElement.dataset.xliffParameter, idPatterns);
|
||||||
|
const response = await fetch(uri, {headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Accept-Language': document.documentElement.lang,
|
||||||
|
}});
|
||||||
|
if (!response.ok) {
|
||||||
|
return Promise.reject('Unexpected server response');
|
||||||
|
}
|
||||||
|
return await response.json();
|
||||||
|
}
|
Loading…
Reference in a new issue