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

Implement i18n's getLocaleByPath function #9504

Merged
merged 6 commits into from Jan 2, 2024
Merged

Conversation

matiboux
Copy link
Contributor

@matiboux matiboux commented Dec 22, 2023

Changes

This PR gives a new implementation for the getLocaleByPath function.
It also fixes a parameter name in another getLocaleByPath function, one that is exposed to users.

According to the Internationalization guide, the following behavior is expected:

For a given configuration:

{
    i18n: {
        defaultLocale: "en",
        locales: ["en", "es", "fr", {
            path: "portugues",
            codes: ["pt-AO", "pt", "pt-BR"]
        }]
    }
}

The function is expected to behave as:

import { getLocaleByPath } from "astro:i18n";
console.log(getLocaleByPath("en")) // will log "en"
console.log(getLocaleByPath("fr")) // will log "fr"
console.log(getLocaleByPath("portugues")) // will log "pt-AO"

However, the behavior for the current implementation is:

import { getLocaleByPath } from "astro:i18n";
console.log(getLocaleByPath("en")) // will log "pt-AO"
console.log(getLocaleByPath("fr")) // will log "pt-AO"
console.log(getLocaleByPath("portugues")) // will log "pt-AO"

Explaining the current behavior:

  • The current implementation ignores non-object locales (such as "en", "es", "fr").
  • The current implementation ignores the given path argument.
  • The current implementation returns the first object locale, regardless of the path.

The new implementation I'm proposing:

  • Supports non-object locales, assuming locale == path == code.
  • Tries to find a match to the path argument.
  • Fallbacks to returning undefined if no match was found.

Testing

I don't think this function is tested. I did not add tests.

I just implemented the function in a project of mine, configuring i18n to match the example I gave in the above section, which is the same as the example in the documentation.

Docs

I aimed to match the existing documentation, so no changes are needed.

Copy link

changeset-bot bot commented Dec 22, 2023

🦋 Changeset detected

Latest commit: 7a39ff4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 22, 2023
@github-actions github-actions bot added the docs pr A PR that includes documentation for review label Dec 22, 2023
@matiboux
Copy link
Contributor Author

I painfully generated a changeset, after installing dependencies in a new environment: curl, pnpm, installing packages, git, and finally running the pnpm exec command.

I chose a "minor" bump, because a behavior changed, while still being a fix to match the v4 documentation.

For this new commit, one workflow about the changeset ("Check mergeability / check") failed with HTTP 403 error (see workflow logs). I'll let you investigate why...

@matiboux
Copy link
Contributor Author

matiboux commented Dec 24, 2023

More context for review:

For a working example using my implementation, I finished working on the website where I needed it.
TLDR: Basically, I wanted to make a switch between configured locales and detect the locale for a given URL.

Off-topic:

From my experience, possible suggestions for future general i18n improvement:

  • having a method or context to list all configured locales, like getLocales or Astro.locales.
  • having a method to get or infer the locale from an URL, like getLocaleByUrl.
  • having locale info returned from getAbsoluteLocaleUrlList for list items.

I made clunky functions and used getLocaleByPath to do some of that stuff for my use case, but proper Astro functions could help. Anyway, still context, but getting off-topic. This should probably go into an issue after this, unless no change is possible there.

@matthewp
Copy link
Contributor

From the description this sounds like a bug fix, if so shouldn't this be a minor?

Also, given you have some examples that are not working correctly now, can you add tests?

@matiboux
Copy link
Contributor Author

From the description this sounds like a bug fix, if so shouldn't this be a minor?

Did you mean "patch"? You're right, I see patches are for "backward compatible bug fixes".
I struggled to get my mind around "backward compatible" as I changed a behavior, but my changes aim to fix the code so it works as described in the documentation so it should qualify as patch.

Also, given you have some examples that are not working correctly now, can you add tests?

Sure, looking into it.

@matiboux
Copy link
Contributor Author

Following the latest commits:

  • Updated the changeset,
  • Added the "expected behavior" example I gave in the PR description as a new E2E test.

The CI/CD checks for tests are successful.

Ready for review!

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this!

@ematipico ematipico merged commit 8cc3d6a into withastro:main Jan 2, 2024
13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jan 2, 2024
@matiboux matiboux deleted the patch-1 branch January 2, 2024 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr A PR that includes documentation for review pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants