Breadcrumbs is not multilingual

default discord avatar
mumincelallast year
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
    last year

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

    generateLabel

    handler and return the localized value.

  • default discord avatar
    mumincelallast year

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

  • discord user avatar
    jacobsfletch
    last year

    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
    mumincelallast year

    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
    last year

    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.