I am wondering what would be the most appropriate method to handle translations in JSON files in terms of arrays and objects.
Considering an object has properties related to itself, is a translation a property or a completely different entity? In which case using an array would be more appropriate.
Let’s take a JSON file describing a movie, using objects:
"title": {
"en": "The Lord of the Rings: The Fellowship of the Ring",
"fr": "Le Seigneur des anneaux : la communauté de l'anneau"
}
Using arrays:
"title": [
{
"lang": "en",
"content": "The Lord of the Rings: The Fellowship of the Ring"
},
{
"lang": "fr",
"content": "Le Seigneur des anneaux : la communauté de l'anneau"
}
]
We should take into account the selected language of the application:
If the user’s application language is fr
, we have to check if title.fr
exists, if it doesn’t we’d need a predefined list of language codes to check their existence in the title
object.
Using arrays we’d need to loop through all its values to check if one of them is in the desired language, but if it doesn’t, the fallback is trivial compared to the previous method.