Best way to handle shared data with multilingual descriptions in Grav CMS?

Hi everyone,

I’m building a multilingual site in Grav CMS with 7 languages. One section of the site displays information about statuettes, and each item has shared properties (e.g., size, price, availability) as well as descriptions and names that are specific to each language.

Currently, I keep all data in separate language files, but this creates a lot of duplication. For example, changing the price requires updating all 7 files. I’d like to centralize shared data while keeping descriptions and names in the language files.

Has anyone implemented something similar or found a best practice for this type of setup?

Thanks in advance! :blush:

1 Like

First thing that comes to mind - custom form action :thinking: Maybe it could save these 3 fields to other languages automatically. For example, when you edit item in language 3 and save it, custom action would take these 3 fields and update in languages 1, 2 and 4-7

There’s a good chance there’s a better way to do it, but that’s what I would probably try with my limited knowledge

One idea I’m considering is to handle the merging either through Twig or a custom plugin. Here’s how it could work:

  1. Use the base .md file (e.g., art.md) for shared data like status or price.
  2. Add a language-specific .md file (e.g., art.de.md) with only localized overrides like title or description.
  3. Merge the base file with the language-specific file dynamically during page rendering.

Questions

  1. Is this approach reasonable in terms of performance and maintainability?
  2. Would you recommend handling this in Twig, or is a plugin the better option?
  3. Are there potential pitfalls I should watch out for with this kind of merging?

I’d love to hear your thoughts or suggestions for better ways to handle this.

Right… I completely forgot you can have a common frontmatter.yaml (even though I use it myself in one of my projects). That would probably be the best way to go

NB: this requires manual YAML file generation and upload. Or someone could make a plugin for that :roll_eyes:

Here’s another solution suggested by Pamtbaau


Try the following in a Grav+Admin installation

  • Create a new custom tab called “Configuration/Pricing” in Admin by creating blueprint /user/blueprints/config/pricing.yaml containing:

    title: Pricing
    form:
        validation: loose
        fields:
    
          prices:
            type: array
            label: PRICING.LABEL
            help: PRICING.PRICES_HELP
            placeholder_key: PRICING.PRICE_KEY
            placeholder_value: PRICING.PRICE_VALUE
            required: true            
    
  • Create file for each language like, /user/languages/en.yaml containing:

    PRICING:
      LABEL: Prices
      PRICES_HELP: Enter an article ID and Price
      PRICE_KEY: Article key
      PRICE_VALUE: Price
    
  • Edit prices in Admin in tab “Configuration/Pricing” and save prices. The created config file will look like:

    prices:
      1: '111'
      2: '222'
    
  • In template /user/themes/quark/templates/default.html.twig, add the following to get the price of article with key = 2:

    {{ config.pricing['prices'][2] }}
    

Just keep in mind theme template extension if you try this

1 Like

i was going to say " that s easy to do with twig and flex-objects " but i saw many good answers already sent here :smiley:

Thank you for the suggestions. I will conduct tests on the proposed solutions and select the best one for this case. Then, I will follow up on the topic and mark the chosen solution.