0

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.

Vilarix
  • 109
  • 2
  • 4
    This strikes me as more of a requirements gathering issue than a software design issue. Either 1) your users can tell you what language they want, 2) you can guess which language they want, 3) they want one language but you have no way of knowing which one, or 4) they want all the languages you have available. Each of those cases would have a different optimal representation. – Ixrec Oct 31 '15 at 19:27

2 Answers2

1

Consider a situation where some items are available in different languages, and some are not, and different from record to record. And there might be items where different languages don't make sense. For example, if you have author, title, translator, the author is language independent, but there is no English translator in this case.

You might consider this way: You have an array of books. Each book is a dictionary. Each key could be directly a key of the dictionary (for example author). Then you may have more dictionaries per language, in this case "en" and "fr", where "en" has the key "title", while "fr" has the keys "title" and "translator".

The user has a list of languages in order of preference, say "it" and "en". Since "it" is not available, you take the author directly, the "title" from "en" and there is no translator. Note that you pick only one language; english title with french translator would be nonsense.

gnasher729
  • 44,814
  • 4
  • 64
  • 126
1

It depends on what you are going to model.

If you take books as an example, each book is uniquely identifyable via ISBN. In this case, the Lord of the rings and its german translation Herr der Ringe are two different entities - and as such have different ISB numbers. Of course you would say, that the german version is only the translation, but it is a separate entity. On the other hand, if you tell someone you read the german edition of a book, and the other read the english version, you read two representations of the "same book" (philosophy aside).

If you are modelling a bookstore it makes totally sense to treat them differently. E.g. you make a "link" to other editions

{
    "Title":"Lord of the rings",
    "Price":"€ 10.49",
    "Paperback":"529 pages",
    "Language":"English",
    "_links":{
          "self":{
              "href":"/books/123"
          },
          "foreign language editions":[
            {
              "href":"/books/459",
              "title":"Der Herr der Ringe",
              "lang":"de"
            },
            {
              "href":"/books/3232",
              "title":"Le Seigneur des anneaux : la communauté de l'anneau",
              "lang"
            }
          ]
      },
}

A different use case would be, if you have the same article with different translations: say, you have screws which are called Schrauben in german. In this case, it would make no sense saying, that the names refer do different things. You would model two different representations of one article in two languages, but referring to the same article. Perhaps your route is /articles/12345 you could add a query parameter to show a different translation: /articles/12345?lang=de which results in a german representation of the same article. Another possibility is, setting the Accept-Language in the HTTP-Header: Accept-Language: en-US.

is a translation a property or a completely different entity?

A translation is either a different entity or a different representation of an entity.

Thomas Junk
  • 9,533