0

I have a Jekyll website with a Netlify CMS admin page. The website supports some different languages, which I have in a _data/languages.yml file for Jekyll to use. Then in the Netlify backend there is a select widget with those same languages.

However, this means I have some duplication of this data, both my languages.yml file and the config.yml file of Netlify have the same list of languages. So I was wondering, is there a way to only have this list of languages once?

My _data/languages.yml file looks something like this:

- value: "nl"
  label: "NL"
- value: "en"
  label: "EN"
- value: "fr"
  label: "FR"
- value: "de"
  label: "DE"
- value: "ja"
  label: "Japanese"
- value: "zh"
  label: "Mandarin"

And in the Netlify config.yml there is:

...
        label: 'Language'
        name: 'language'
        widget: 'select'
        options: 
          - { label: "Dutch", value: "nl" }
          - { label: "English", value: "en" }
          - { label: "French", value: "fr" }
          - { label: "German", value: "de" }
          - { label: "Japanese", value: "ja" }
          - { label: "Chinese", value: "zh" }
...

What I thought of (and why it doesn't work):

  1. Use liquid tags inside the config.yml -> This is not supported, and Netlify just complains that the config.yml now starts with --- --- (the frontmatter)
  2. Include the languages.yml inside config.yml -> You can't "import" yml files in another one sadly

Are there any other ways that do work?

The Oddler
  • 6,314
  • 7
  • 51
  • 94

1 Answers1

1

You can make your languages a file collection, which will then allow you to use a relation widget instead of the select widget with the hardcoded languages you're currently using.

If you don't want your admin user to be able to edit this language list, you can use the hide: true option in your language collection.

qbeauperin
  • 591
  • 4
  • 21
  • Brilliant! I'm going to try this, and it's ok that they can add languages. – The Oddler Jan 12 '23 at 10:07
  • Question, it looks like I'll have to make a different file for each language, is that correct? If not, how do I set up the relation widget to look at the list in a single file? – The Oddler Jan 12 '23 at 11:06
  • It should work without different files, as then it would just be a folder collection. Just need to figure out the correct syntax for the relation widget :D – The Oddler Jan 12 '23 at 11:21
  • Found it! There is an example for how to reference a file collection in the relation widget docs: https://www.netlifycms.org/docs/widgets/#relation – The Oddler Jan 12 '23 at 11:23