Hi, I'm trying to create a way to implement translations for fields in Payload. What can I do to achieve that?
Example:
Consider as an example the following Json representing a Player.
"Player": {
"name" : [
{ "id" : 1, "language" : "english", "key" : "name", "value" : "John" }, // Translation relationship
{ "id" : 2, "language" : "portuguese", "key" : "name", "value" : "João" }, // Translation relationship
],
"age" : 18,
"description" : [
{ "id" : 3, "language" : "english", "key" : "description", "value" : "Hello World!" }, // Translation relationship
{ "id" : 4, "language" : "portuguese", "key" : "description", "value" : "Olá Mundo!" }, // Translation relationship
]
}
How could I achieve a representation of this with as many languages and translations I wish?
I was looking into defining the Collection with the fields that can be translated as an array of relationships. But I'm not sure if that's even possible.
const languageFields: Field[] = [
{ name: 'name', type: 'text' },
{ name: 'isDefault', type: 'checkbox' }
]
const Languages: CollectionConfig = {
slug: 'languages',
auth: true,
admin: { useAsTitle: 'name' },
fields: languageFields,
}
const translationFields: Field[] = [
{ name: 'language', type: 'relationship', relationTo: 'languages', required: true },
{ name: 'key' , type: 'text' , },
{ name: 'value' , type: 'text' , },
]
const Translations: CollectionConfig = {
slug: 'translations',
auth: true,
admin: { useAsTitle: 'name' },
fields: translationFields,
};
const playerFields: Field[] = [
{ name: 'name', type: 'array', fields: [{ name: 'translation', type: 'relationship', relationTo: 'translations' }] },
{ name: 'age' , type: 'number' },
{ name: 'description', type: 'array', fields: [{ name: 'translation', type: 'relationship', relationTo: 'translations' }] }
]
const Players: CollectionConfig = {
slug: 'players',
auth: true,
admin: { useAsTitle: 'name' },
fields: playerFields,
}
export { Languages, Translations, Players }
This is my attempt. Not sure if this makes even sense. Could someone help me with this?