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

feat: fall back if translation is missing #3312

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

babakfp
Copy link

@babakfp babakfp commented Dec 9, 2023

This PR helps to fall back to a different locale if a page in the current locale is missing. This feature can be enabled by setting the localesFallback option to true. All locales will fall back to the default locale (root), which can be changed by using the localesDefaultFallback option (and setting the value to the key of a locale). The fallback option is available for each locale to have its custom locale to fall back to.

Example 1

{
    locales: {
        root: { lang: 'en' },
        fa:   { lang: 'fa' },
    },
    localesFallback: true,
}
  1. If /fa/a was not found, fall back to /a.
  2. If /a was not found, show 404.
  • root is the default locale to fall back to.
  • If localesFallback option was set to false, things would continue to work like this PR never happened.

Example 2

You can set a custom locale to fall back to for each locale.

{
    locales: {
        en: { lang: 'en', },
        fa: { lang: 'fa', fallback: 'en' },
    },
    localesFallback: true,
}
  1. if /fa/a was not found, fall back to /en/a.
  2. if /en/a was not found, show 404.
  • root can have its fallback option too.
  • A locale can't fall back to itself.

Example 3

Change the default locale that all locales can fall back to.

{
    locales: {
        en: { lang: 'en' },
        fa: { lang: 'fa' },
    },
    localesDefaultFallback: 'en',
    localesFallback: true,
}
  1. if /fa/a was not found, fall back to /en/a.
  2. if /en/a was not found, show 404.
  • A locale can't fall back to itself.

Example 4

{
    locales: {
        en: { lang: 'en' },
        fa: { lang: 'fa', fallback: 'tr' },
        tr: { lang: 'tr', fallback: 'en' },
    },
    localesFallback: true,
}
  1. if /fa/a was not found, fall back to /tr/a.
  2. if /tr/a was not found, fall back to /en/a.
  3. if /en/a was not found, show 404.

Helpful errors

  • If the fallback option was set to 'root', you will get an error, because it's the default value and there is no reason to do it.
    • The error message is: "Invalid VitePress Config: A locale (the-locale-key), cannot fall back to (root).".
  • If the fallback option was set to its locale key, you will get an error, because this will create a loop.
    • The error message is: "Invalid VitePress Config: A locale (the-locale-key), cannot have a fallback to itself."
  • If the fallback option was set to a locale key that didn't exist, you will get an error, because, I guess if you have a locale, it should be configured in VitePress Config too.
    • The error message is: "Invalid VitePress Config: A locale (the-locale-key), cannot have a fallback to a non-existing locale."

I fixed the issue reported here.

@babakfp babakfp marked this pull request as draft December 9, 2023 13:55
@hamishwillee
Copy link

hamishwillee commented Dec 12, 2023

This is brilliant. The only thing that occurs to me is whether search needs to consider the fact that any "not found" topics should be taken from the fallback trees.

@altrusl
Copy link
Contributor

altrusl commented Jan 12, 2024

I support this PR
Often in multilanguage websites there are some on purpose missing section translations but Google Search Console and other SEO tools downgrade rating for 404 pages
Language selector should be disabled on those pages or it should fall back to existing translations

@liana-p
Copy link
Contributor

liana-p commented Jan 31, 2024

I'm also very interested in this being merged. I was just in the process of starting to localise a docs website and realised there's no good way to work around the language picker 404ing if the page doesn't exist in the other language.

It could also help reuse sidebars/navbars.

@IMB11
Copy link

IMB11 commented Feb 18, 2024

It seems that index pages in sub-folders are not working.

When going to es_es/players/index.md, it redirects to the english page. You can test this in the closed PR above this comment.

@github-actions github-actions bot added the stale label Apr 12, 2024
@babakfp babakfp marked this pull request as draft April 23, 2024 05:21
@babakfp babakfp marked this pull request as ready for review April 25, 2024 04:05
@babakfp
Copy link
Author

babakfp commented Apr 25, 2024

It seems that index pages in sub-folders are not working.

Thank you for letting me know. I fixed it in the recent commits. Feel free to check it out, it's ready for review.

@github-actions github-actions bot removed the stale label Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants