Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMS events don't update i18n content values #4729

Closed
wslyvh opened this issue Dec 15, 2020 · 7 comments
Closed

CMS events don't update i18n content values #4729

wslyvh opened this issue Dec 15, 2020 · 7 comments
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality

Comments

@wslyvh
Copy link
Contributor

wslyvh commented Dec 15, 2020

I'm trying to use CMS event listeners with i18n enabled.
I realize both are beta features and might not be in a state that is sufficient for my use-case.

What I'd like to achieve is to automatically translate content from the default lang into the other language(s). I was thinking of:

  1. catch a preSave (or prePublish) CMS event, using cms.registerEvents
  2. take the data (or certain fields) from the entry using entry.get('data')
  3. use an API service (Google translate) to translate these value(s)
  4. saving the translated values for the i18n content object using entry.get('data').set('desc', 'translated desc')

I'm able to catch and read all content data for both languages, following the docs at
https://www.netlifycms.org/docs/beta-features/#registering-to-cms-events

The updates, however, are only saved for the original (en) content.

Here's an example of the event handler

CMS.registerEventListener({
    name: 'preSave',
    handler: ({ entry }) => {
        const description = entry.get('data').get('description');
        const descripcion = entry.get('i18n').get('es').get('data').get('description');
        console.log("Current values", description, descripcion);

        return entry.get('i18n').get('es').get('data').set('description', 'Saving my new Spanish description');
    }
});

Results of saving my page from the CMS.
image
As you can see, the description is updated on the en/page.md and nothing was added on the es/page.md

For completion also the relevant parts of my config.yml, although I think this matters much.

i18n:
  structure: multiple_folders
  locales: [en, es]
  default_locale: en

collections:
  - name: 'pages'
    folder: 'src/content/pages'
    create: true
    i18n: true
    fields:
      - { name: 'title', label: 'Title', widget: 'string', i18n: true }
      - { name: 'description', label: 'Description', widget: 'text', i18n: true, required: false }

Applicable Versions:
"gatsby-plugin-netlify-cms": "^4.4.0"
https://unpkg.com/netlify-cms@2.10.76/dist/netlify-cms.js
Git provider: GIthub
OS: Mac, Catalina
Browser version Brave
yarn version v1.22.10

Is it possible to use these features together?
Am I using them correctly?
Or would there be any alternatives for automatically translating original CMS content using an external API?

@wslyvh wslyvh added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Dec 15, 2020
@erezrokah
Copy link
Contributor

Hi @wslyvh, you're correct this is not supported yet.
May I suggested that you run the translation during the build process? That is, when querying the data during Gatsby's build you can call the translation API and add the missing information

@wslyvh
Copy link
Contributor Author

wslyvh commented Dec 16, 2020

Got it. Thanks for the confirmation.

We were considering to use CMS events, to avoid bloating the build process too much. But I think that would work, yes.

Is there an easy way to update the actual MD files, so that the translated content would still show up in the CMS?
Or would you recommend to use onCreateNode//createNodeFields to add the translations?

@erezrokah
Copy link
Contributor

If the content/translation is autogenerated I recommend doing it in the build process and not the CMS.

You could still commit the changes back to the repo during the build but I wouldn't got that way since it can create other issues (push access to builds, triggering another build during a build, etc.)

If you really want to update the content from the CMS you could change the MD file directly using the GitHub API (or whatever backend you're using).

@wslyvh
Copy link
Contributor Author

wslyvh commented Dec 16, 2020

That's what I was thinking. Thanks for that.

Maybe one more question on CMS events + i18n, are there any plans and/or timelines to support that?

@erezrokah
Copy link
Contributor

We don't have a timeline for it, but a contribution will be great

@wslyvh
Copy link
Contributor Author

wslyvh commented Dec 18, 2020

Got it. Thanks again.

I'll have a look at contributing, but I think in this case it'll likely be quicker for our current use-case to find an alternative solution.

@martinjagodic
Copy link
Member

Fixed in #7227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

No branches or pull requests

3 participants