Transform multi-locale response to match client-side i18n format #46
Replies: 4 comments 6 replies
-
My mapping-function looks something like that: translate () {
const translations = {};
const slug = typeof arguments[0] === 'string' ? arguments[0] : 'content';
for (const json of arguments) {
if (Array.isArray(json)) {
for (const entry of json) {
translations[entry.locale] = translations[entry.locale] || {};
translations[entry.locale][slug] = translations[entry.locale][slug] || [];
translations[entry.locale][slug].push(entry);
}
} else if (typeof json === 'object') {
const entry = json;
const stayObject = arguments.length === 1;
const localizations = entry.localizations;
translations[entry.locale] = translations[entry.locale] || {};
// delete entry.localizations;
if (stayObject) {
translations[entry.locale][slug] = entry;
} else {
translations[entry.locale][slug] = translations[entry.locale][slug] || [];
translations[entry.locale][slug].push(entry);
}
for (const entry of localizations) {
translations[entry.locale] = translations[entry.locale] || {};
if (stayObject) {
translations[entry.locale][slug] = entry;
} else {
translations[entry.locale][slug].push(entry);
}
}
} else {
console.log('Argument was neither an object nor an array: ', json);
};
}
console.log(translations);
return translations;
}
async fetchTranslation (slug) {
const response = await fetch(slug);
return this.translate(response.data);
} and then use like: const content = await fetchTranslation('/content-type?locale=all');
for (const [lang, message] of Object.entries(content)) {
this.$i18n.setLocaleMessage(lang, message);
} |
Beta Was this translation helpful? Give feedback.
-
I'll have to look into this, I do not think it will be integrated natively as it seems to be product specific use case which I tend not to add. What I am thinking at the moment is to add some pre/post and during processing hooks that can modify the transformation. That should allow for somewhat user configurable transformations. |
Beta Was this translation helpful? Give feedback.
-
Ok, cool. Thank you very much!Inside the transform service, do i have access to the request ctx? This way i could only transform the response if a specific parameter is set for example, or include some other conditions. RegardsraphaeldasAm 06.11.2022 07:33 schrieb daedalus ***@***.***>:
Thanks,
It should not be to difficult to do as I have done a I will try and release an implementation for it over the next week or so but cannot guaruntee that timeline.
If you wish to take a crack at it in the meantime the pre and post response transform hook modification would happen before/after this line
https://github.com/ComfortablyCoding/strapi-plugin-transformer/blob/master/server/services/transform-service.js#L94 . The hooks themselves would be done under their own top level property in te settings (or at least that is the plan atm).
e.g.
module.exports = ({ env }) => ({
// ..
'transformer': {
enabled: true,
config: {
// ...
hooks :{
'preResponseTransform';()=>{}
'postResponseTransform':()=>{}
}
// ...
}
},
// ..
});
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi. Any updates regarding this feature request? :-) |
Beta Was this translation helpful? Give feedback.
-
Hello
I'm a happy user of your plugin to simplify the strapi response. (Natively it is too nested in my opinion!)
Another difficulty is the transformation of answers with translations in the localizations property.
I use vue-i18n to integrate this translations in my web app. Therefor i have to transform the strapi response to this format:
For now I have a function to align the response to the above format.
But it's quite inefficient to do this step client-side for every request.
It would be much more convenient to do this transform step server side, directly inside strapi.
So, would this be something that could be integrated into this transformer plugin?
Or would this function be better off in a new separate plugin?
Thank you for your answer
Best Regards
raphaeldas
Beta Was this translation helpful? Give feedback.
All reactions