Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

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

Solved by Karmalakas View solution

Started by Norbert 2 years ago · 7 replies · 205 views
2 years ago

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! 😊

👍 1
2 years ago

First thing that comes to mind - custom form action 🤔 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

2 years ago

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.

2 years ago

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 🙄

2 years ago Solution

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:

    YAML
    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:

    YAML
    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:

    YAML
    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:

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

Just keep in mind theme template extension if you try this

👍 2
2 years ago

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

2 years ago

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.

1 year ago

I applied the simplest solution, which is using frontmatter.yaml in each directory. In my case, this is sufficient because I manage the site and edit it by modifying files directly. If editing through the admin panel is also required, the best solution seems to be the one suggested by @Karmalakas. I haven't used Flex Objects, so I have no knowledge about them.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 8 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago