[Question] Translating default component names (Body, Text, Row, etc.)?
Question
I want to change the names of the default components that you can drag and drop onto the canvas, so that it shows different names in the layer manager and other places, mainly because of translation into other language. Examples: Body, Text, Row, Link, Image, etc.
How would I do that?

Answers (3)
It seems the display name for a component in the canvas and the layer manager is the block's type id, with the first letter upper-cased. From what I can tell, then only way to change this is to make a new type extending the old one, with a different id, and then deleting the old type to keep things clean. Then, all blocks utilising the old type need to be changed to the new type, and their labels changed too. Here's some example code changing some of those default component names:
editor.on('load', () => {
const translateObject = {
'text': 'translatedText',
'image': 'translatedImage',
'body':'translatedBody',
'row':'translatedRow',
'link':'translatedLink',
};
const bm = editor.BlockManager;
const dm = editor.DomComponents;
Object.keys(translateObject).forEach((translationKey) => {
const translationVal = translateObject[translationKey];
const affectedBlocks = bm.getAll().filter((b) => {
return b.attributes.content.type === translationKey;
});
affectedBlocks.forEach((block) => {
block.attributes.content.type = translationVal;
block.set('label', translationVal);
});
const oldType = dm.getType(translationKey);
dm.addType(translationVal, {
model: oldType.model,
view: oldType.view
});
dm.removeType(translationKey);
});
});
You can update their names in this way. IMPORTANT: place the code in a plugin otherwise, you risk to the see updated names only with newly added components
editor.DomComponents.addType('image', {
model: {
defaults: { name: 'MY IMAGE', }
}
});
Thanks. This works nicely when adding new components, but say I load a preset html+css using editor.setComponents(html) and then start editing that later. The preset's HTML tags get the various component names (Text, Box, Image etc.) assigned to them automatically by GrapesJS. How would I go about editing those default names? I can't seem to find a way.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1537
Duplicate component not style
Hi, First of all, thanks a lot for the awesome work ❤️ !!! I've a question about the duplication tool so here the steps to follow :I drag&d...
Issue #2552
Only allow one component to be dropped
Sorry for the newbie question, but say I have a text field component and I only want to allow the user to drop a specific number of them in...
Issue #2129
[Question] how to append a button inside default modal and catch it's click event
I want a form inside a modal (using the default modal ), on click of the submit button make an ajax call. I have created a new component wh...
Issue #1875
[QUESTION] ID changes when importing html or passing html in components prop in config.
Hi, I built a component and block that I can drag and drop in the canvas. My issue is that some styles rely on the ID. I know this might no...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.