Breadcrumbs is not multilingual

default discord avatar
mumincelal12 months ago
10

I am using nested docs plugin and trying to use breadcrumbs with multilingual support. Here is my setup:



export const Breadcrumbs: Field = {
  name: 'breadcrumbs',
  label: 'Breadcrumbs',
  type: 'array',
  fields: [
    {
      name: 'page',
      label: 'Page',
      type: 'relationship',
      relationTo: 'pages',
      maxDepth: 0,
      admin: {
        disabled: true
      }
    },
    {
      type: 'row',
      fields: [
        {
          name: 'url',
          label: 'URL',
          type: 'text',
          localized: true,
          admin: {
            width: '50%'
          }
        },
        {
          name: 'label',
          label: 'Label',
          type: 'text',
          localized: true,
          admin: {
            width: '50%'
          }
        }
      ]
    }
  ],
  admin: {
    disabled: false
  }
};


localization: {
  locales: ['en', 'tr'],
  defaultLocale: 'tr',
  fallback: true
},
i18n: {
  debug: false,
  supportedLngs: ['en', 'tr'],
  fallbackLng: 'tr'
},
nestedDocs({
      collections: [Post.slug, EnBlog.slug, TrBlog.slug, Page.slug],
      parentFieldSlug: 'parent',
      breadcrumbsFieldSlug: 'breadcrumbs',
      generateLabel: (_, page) => (page.name as string) || '',
      generateURL: (pages) =>
        pages.reduce((url, page) => `${url}/${page.slug}`, '')
    })


ONLY THE LATEST ONE IS SAVED when I try to generate breadcrumbs automatically with both languages. It is not working as expected.



How can I make the breadcrumbs multilingual?



@jacobsfletch , @jmikrut may be you have an idea.

  • discord user avatar
    jacobsfletch
    12 months ago

    Without looking I wonder if you just need to read the locale from your

    generateLabel

    handler and return the localized value.

  • default discord avatar
    mumincelal12 months ago

    Sorry, I am new on Payload. What do you mean? How can I do that?

  • discord user avatar
    jacobsfletch
    12 months ago

    The

    generateLabel

    function you have defined in your config is returning

    page.name

    but instead should return something like

    page.name[locale]


    It might look slightly different than this, I’m not looking at my computer right now, but is the general idea

  • default discord avatar
    mumincelal12 months ago

    Thank you, I will try



    locale is dynamic in here. How can I get the name with locale? There is no locale field in nested-docs plugin. Could you share an example code?



    I solved this.



    export const Breadcrumbs: Field = { name: 'breadcrumbs', label: 'Breadcrumbs', type: 'array', localized: true, // Added this one fields: [ { name: 'page', label: 'Page', type: 'relationship', relationTo: 'pages', maxDepth: 0, admin: { disabled: true } }, { type: 'row', fields: [ { name: 'url', label: 'URL', type: 'text', localized: true, admin: { width: '50%' } }, { name: 'label', label: 'Label', type: 'text', localized: true, admin: { width: '50%' } } ] } ], admin: { disabled: false } };
  • discord user avatar
    jacobsfletch
    11 months ago

    Nice!



    Looking at the source code now, you shouldn't have to create your own breadcrumbs field here, instead you can set

    localization: true

    on your plugin config and the supplied breadcrumbs field will automatically be localized just as you have:

    https://github.com/payloadcms/plugin-nested-docs/blob/main/src/index.ts#L25
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.