Sample code in different programming languages

Hi, This is my first post on the forum. I’m updating the API documentation for 1QBit and would like to end up with sample code with a selector for different languages. For example:

Does anyone have a recommendation of how to set up the data structure in Contentful to achieve this? Should I create a Long Text field for each language? Should I have one big Long Text field and include all the sample code there distinguished by code block language, for example, ```python?

Naomi

1 Like

Hello and welcome to our forums!

We have faced a similar issue when creating the example apps: Link to the example app, written in java.

We solved it by having a field for every language:

By using this content model:

and it results to this json response:

  "fields": {
    "title": {
      "en-US": "Fetch all entries > code"
    },
    "curl": {
      "en-US": "curl -H 'Authorization: Bearer '$CDA_TOKEN 'https://cdn.contentful.com/spaces/'$SPACE_ID'/entries/'"
    },
    "dotNet": {
      "en-US": "var entry = await client.GetEntries<dynamic>();\n\nConsole.WriteLine(entry.ToString());"
    },
    "javascript": {
      "en-US": "client.getEntries()\n.then((response) => console.log(response))"
    },
    "java": {
      "en-US": "final CDAArray all =\n    client\n        .fetch(CDAEntry.class)\n        .all();\n\nSystem.out.println(all);\n"
    },

Feel free to check out the source code of the apps here: https://github.com/contentful/the-example-app.java (Other languages are also available: use py, rb, php, nodjs, net,kotlin or csharp intead of java)

I hope I could help you and am looking forward to hear from you again,
Mario

Thanks, @mario. Have you also explored the use of locales before choosing this method by any chance?

Never mind. I was playing with locales and I just found out that you can only select human languages when creating a new one. So, it won’t work for code languages.