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

Fixed failure to apply a default language after a request with specified language #5

Conversation

bauerbrun0
Copy link
Contributor

Description:

This pull request addresses the issue where the application, after a request with a specified language, fails to revert to a default language when the subsequent request does not specify a language. The current behavior retains the previous language instead of falling back to a default language, eg. the one set in initOptions.lng.

Changes Made:

  1. Code Modification:

    • Updated the logic in onBeforeHandle; If a language is not detected and the new useLngAsDefault option is true, it falls back to the default language specified in initOptions.lng.

      // src/index.ts
      .onBeforeHandle(async ctx => {
          const detectedLanguage = await options.detectLanguage(ctx)
          if (detectedLanguage) {
              await _instance.changeLanguage(detectedLanguage)
              return
          }
      
          const { options: { lng }, language: currentLanguage } = _instance
      
          if (options.useLngAsDefault && lng && currentLanguage !== lng) {
              await _instance.changeLanguage(lng)
          }
      })
  2. New Option:

    • Introduced a new option useLngAsDefault (defaults to true) to control whether the application should use the default language when a language is not specified/detected in a request.

      const app = new Elysia()
          .use(i18next({ instance, useLngAsDefault: true }))
          .get('/', ({ t }) => t('greeting'))
  3. Test Cases:

    • Added a new test case to validate that the application falls back to the default language after a request with a specified language.
    • Added a test case to verify that opting out from using the default language (useLngAsDefault: false) works as intended.
  4. Documentation:

    • Updated the README to include information about the new option useLngAsDefault.

Thank you for reviewing this pull request!

@eelkevdbos
Copy link
Owner

Hey! Sorry for the wait, I'll have a look at your contribution today. I have been messing around a bit with the language (detection) and the behaviour is not yet where I want it to be, but maybe this solves those issues.

@eelkevdbos
Copy link
Owner

eelkevdbos commented Feb 8, 2024

The test case you have submitted is valid, thank you for that contribution!

I'd like to go for a slightly different solution to the one that you have implemented. To ensure complete isolation from other requests, I'd like to clone a fresh instance (with configured defaults) of the library on each request.

The rationale behind it is that I could see settings persist other than the current language and that would create a whole range of settings to make sure that the defaults are restored / used on subsequent requests.

Added benefit of cloning is that I am also eyeing the use of i18n in an async context, for example, rendering translations (for the currently set language) in jsx components (kitajs/html#31).

@morigs
Copy link

morigs commented Jul 24, 2024

@eelkevdbos Do you still have plans on implementing this? Also, looking forward for #9

Please give me know if there a way to help

@eelkevdbos
Copy link
Owner

@eelkevdbos Do you still have plans on implementing this? Also, looking forward for #9

Please give me know if there a way to help

Sorry for the wait, I have a tentative implementation ready.

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.

3 participants