vitepress icon indicating copy to clipboard operation
vitepress copied to clipboard

themeConfig.i18nRouting should accept a function

Open JingMatrix opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe.

The property themeConfig.i18nRouting is complicated, a boolean value is not sufficient for general usage case.

Describe the solution you'd like

Please allow users to pass a function for the property themeConfig.i18nRouting, so that they can decide how 18nRouting is done.

Describe alternatives you've considered

No response

Additional context

No response

Validations

JingMatrix avatar Feb 01 '24 12:02 JingMatrix

Any suggestions on what should the API look like? We can allow pure functions. Will something like this work:

i18nRouting: (data: VitePressData<DefaultTheme.Config>, hash: string) => string

brc-dd avatar Feb 01 '24 20:02 brc-dd

That is a nice idea: the return value serves as the dynamic routing result. It would be perfect, if the target locale config could be included into the arguments.

You may export your current (two) implements as (two) examplar configs.

JingMatrix avatar Feb 01 '24 20:02 JingMatrix

It would be perfect, if the target locale config could be included into the arguments

Ah yeah right. You can access current locale from data.localeIndex.value and we can provide target locale as argument.

brc-dd avatar Feb 01 '24 20:02 brc-dd

I'm interested in tackling this issue. I'm proposing the following API:

    i18nRouting?:
      | boolean
      | ((
          data: VitePressData<DefaultTheme.Config>,
          hash: string,
          targetLocale: string
        ) => string)

To illustrate its usage, here's an example implementation of a custom routing function that replicates the current default behavior:

  themeConfig: {
    i18nRouting: (data, hash, targetLocale) => {
      const langDir = (locale: string) =>
        locale === 'root' ? '/' : `/${locale}/`
      const barePath = data.page.value.relativePath
        .slice(langDir(data.localeIndex.value).length - 1)
        .slice(0, -3) // get rid of '.md'
      return `${langDir(targetLocale)}${barePath}${hash}`
    },
    …
  }

Let me know your thoughts!

etuardu avatar Feb 29 '24 13:02 etuardu

Are you sure, using a function is more advantegous, than defining the target pages in a frontmatter, like in the proposed solution to #3532 ?

IMHO defining a translated target for each page is more transparent and provides better control, i.e. if a translated-link changes. The configuration is simply closer to each page.

polenter avatar Mar 24 '24 06:03 polenter

For me, it IS tedious to add configurations for each single pages, while a handy structure is already present in the content folder.

Moreover, it is easier for users to adopt this configuration method.

JingMatrix avatar Mar 24 '24 07:03 JingMatrix

Is sb working on this issue?

polenter avatar May 07 '24 04:05 polenter

Is sb working on this issue?

I actually have a PR ready, which implements the interface I suggested, but I was waiting a mantainer's reaction before sending it.

etuardu avatar May 07 '24 11:05 etuardu