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

fix(relation-widget): i18n options #6299

Merged

Conversation

demshy
Copy link
Member

@demshy demshy commented Mar 15, 2022

Summary
Options in relation widget were not translated for collection or file based relations. I passed the locale into the component so that the function that parses these options can use it accordingly.

fixes #4491

Test plan
As I'm new here, I didn't feel quite comfortable adding props to the main editor blocks, but didn't find another way to access locale of the component that is rendered. If there is another (better) way, please inform me and I will update this accordingly.

Screenshot 2022-03-15 at 17 51 55
Screenshot 2022-03-15 at 17 53 41

Config:

collections:
  - name: taxonomies
    label: Taxonomies
    format: json
    i18n:
      structure: single_file
      locales: [en, de]
    files: [
      {label: Travel type ideas, name: travelTypes, file: data/travelTypes.json, i18n: true, fields: [
        {name: title, label: title, widget: string, i18n: true},
        {name: items, label: items, widget: list, label_singular: item, i18n: true, fields: [
          {name: title, label: title, widget: string, i18n: true},
          {name: items, label: items, widget: list, label_singular: item, i18n: true, fields: [
            {name: title, label: title, widget: string},
            {name: id, label: id, widget: string, pattern: ['^[a-z0-9-]+$', 'Only lowercase letters, numbers, and hyphens allowed'], hint: 'IMPORTANT: All translations of this item must have the same ID'},
          ]},
        ]},
      ]},
    ]
  - name: travel-ideas
    label: Travel Ideas (experiences)
    label_singular: travel idea
    folder: content/experiences/travel-ideas
    create: true
    i18n: true
    fields: [
      {name: title, label: Title, widget: string, i18n: true},
      {name: travelType, label: Travel idea type, widget: relation, collection: taxonomies, file: travelTypes, search_fields: ["items.*.title"], display_fields: ["items.*.title"], value_field: "items.*.id", i18n: duplicate},
    ]

content in data/travelTypes.json

{
  "en": {
    "title": "Travel types EN",
    "items": [
      {
        "title": "Art and Culture",
        "id": "art-and-culture"
      },
      {
        "title": "Food and drink",
        "id": "food-and-drink"
      },
      {
        "title": "Outdoors",
        "id": "outdoors"
      }
    ]
  },
  "de": {
    "title": "Travel types DE",
    "items": [
      {
        "title": "Kunst und Kultur",
        "id": "art-and-culture"
      },
      {
        "title": "Essen und Trinken",
        "id": "food-and-drink"
      },
      {
        "title": "Draußen",
        "id": "outdoors"
      }
    ]
  }
}

Checklist

  • I have read the contribution guidelines.
  • Code is formatted via running yarn format.
  • Tests are passing via running yarn test.
  • The status checks are successful (continuous integration). Those can be seen below.

A picture of a cute animal (not mandatory but encouraged)
a69429a0f5a4e808171d1be9751b6a83--cute-baby-animals-farm-animals

@demshy demshy marked this pull request as ready for review March 15, 2022 16:56
@demshy demshy requested a review from a team March 15, 2022 16:56
@bytrangle
Copy link
Collaborator

bytrangle commented Mar 21, 2022

@demshy Can you give more details in your description about how to set up the config file so that one can get the same result as your second screenshot? To be more specific, what goes under the Travel Idea Type collection and what the relation widget setup looks like?

@demshy
Copy link
Member Author

demshy commented Mar 22, 2022

Hi, of course, I completely forgot about this. I resolved this an extension of another issue (#6264) which can be closed so I used the test case from that issue.

Anyhow, I have added the collection config and example json data file to the description.

@bytrangle
Copy link
Collaborator

And this is what the i18n support in the relation and object widgets should look like with your proposed changes, correct?

i18n-support-for-relation-and-object-widget

@demshy
Copy link
Member Author

demshy commented Mar 24, 2022

Yes, this was the idea. Currently english texts would be displayed on both sides.

Copy link
Collaborator

@bytrangle bytrangle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job in retrieving options in the correct locale. However, the locale prop is being passed through several intermediary components that don't explicitly use them, namely EditorControlPane and EditorControl.

What do you think about using the context API to let Widget subscribe to locale value from EditorControlPane?

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @demshy, this works great and sorry for the very late review.

@bytrangle great point on using context API. I think this PR is consistent with the rest of the codebase, so I'm happy to ship it as is to fix the issue. We can re-visit the usage of context API in the codebase as a general approach, WDYT?

@erezrokah erezrokah dismissed bytrangle’s stale review April 12, 2022 14:31

We can re-visit context API in a separate PR

@erezrokah erezrokah enabled auto-merge (squash) April 12, 2022 14:31
@erezrokah erezrokah merged commit 4bf6464 into decaporg:master Apr 12, 2022
@demshy
Copy link
Member Author

demshy commented Apr 12, 2022

Hey guys, sorry for my late response, but this was not a blocker for us and I had to attend to other matters for a while.
Thanks to both of you for taking your time. I think we have another locale related issue coming up very soon and I will try to go with @bytrangle's suggestion there. I'll make sure to tag you both to see what you think.

@bytrangle
Copy link
Collaborator

@erezrokah I'm fine with your suggestion.

@bytrangle
Copy link
Collaborator

@demshy Do you mind if I work on the context API? I'm already working on it locally and it's still fresh in my mind. It's alright if you want to work on it.

@demshy
Copy link
Member Author

demshy commented Apr 13, 2022

@bytrangle of course, that would be great! I will try to delay our thing until you're done.
I think we have something about translatable lists widget coming up, but I don't know the details yet.

@bplmp
Copy link

bplmp commented May 16, 2023

Was this fix deployed to a release? I'm running netlify-cms 2.10.192 but I'm having issues with the i18n options in the relation widget. It is only showing the default language title in the select box, even when changing locale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relation widget not working with new i18n multilanguage feature
4 participants