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: Add NO_PREFIX strategy + setLocale function #409

Merged
merged 1 commit into from
Aug 27, 2019

Conversation

richie3366
Copy link

For those who only want to use SEO, lazy locale load, cookie store and/or browser-language detection without having to deal with sub-domains nor route prefixes nor route aliases, the no_prefix Strategy will allow nuxt-i18n to fit their needs.

I've also added a setLazyLocale function in app.i18n to let developers call it programatically.

@codecov
Copy link

codecov bot commented Aug 22, 2019

Codecov Report

Merging #409 into master will decrease coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #409      +/-   ##
==========================================
- Coverage    81.1%   81.03%   -0.07%     
==========================================
  Files           4        3       -1     
  Lines         127      116      -11     
  Branches       23       26       +3     
==========================================
- Hits          103       94       -9     
+ Misses         20       18       -2     
  Partials        4        4
Impacted Files Coverage Δ
src/index.js 86.04% <100%> (+2.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63ed8df...996b7ae. Read the comment docs.

@rchl
Copy link
Collaborator

rchl commented Aug 22, 2019

  • I think we should have a warning (console.warn) in localePath function that would trigger when locale argument is different than current locale. Basically there is no way to handle that with no-prefix strategy so we should warn that passing locale is unsupported in that case.
  • Same for switchLocalePath - it won't work as expected when passed non-default locale.
  • I believe the differentDomains option is not compatible with no-prefix strategy. One will break functionality of the other if both are set. Would be nice to have warning for that too (in module's index.js).

Copy link
Collaborator

@rchl rchl left a comment

Choose a reason for hiding this comment

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

You are a godsend. I needed that and was going to look into it myself soon. :)

src/helpers/routes.js Outdated Show resolved Hide resolved
src/helpers/routes.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
@rchl
Copy link
Collaborator

rchl commented Aug 22, 2019

Also this code should be disabled with no-prefix strategy:
https://github.com/nuxt-community/nuxt-i18n/blob/ff6937bf8bebd3d98d51abf2098046b5323b13f7/src/templates/middleware.js#L102-L104
It would also be problematic if you would follow my advice and trigger warning when calling localePath with non-default locale argument.

@rchl
Copy link
Collaborator

rchl commented Aug 22, 2019

Please also add two new APIs here:
https://github.com/nuxt-community/nuxt-i18n/blob/ff6937bf8bebd3d98d51abf2098046b5323b13f7/types/vue.d.ts#L45-L54

@richie3366
Copy link
Author

Thanks for your appreciation & for all your comments @rchl!

Some of the things you noticed were caused by the fact I had implemented them 6 months ago (without making a PR because I thought it may be undesired since it's kinda the core/main functionnality of this module) in a different context, and I applied the same changes without checking if it could have a better fit.

I'll look further into your comments during the day (CEST timezone) and I'll do my best to answer &/or comply to them.

@richie3366
Copy link
Author

@rchl: I think I've made all the requested changes. Do you advise to rewrite my commit (& force push) or to create an additional one?

@rchl
Copy link
Collaborator

rchl commented Aug 23, 2019

Feel free to add new commits. It's easier to review that way. I can rewrite stuff when merging. Thanks

Copy link
Author

@richie3366 richie3366 left a comment

Choose a reason for hiding this comment

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

  • I think we should have a warning (console.warn) in localePath function that would trigger when locale argument is different than current locale. Basically there is no way to handle that with no-prefix strategy so we should warn that passing locale is unsupported in that case.
  • Same for switchLocalePath - it won't work as expected when passed non-default locale.
  • I believe the differentDomains option is not compatible with no-prefix strategy. One will break functionality of the other if both are set. Would be nice to have warning for that too (in module's index.js).

I'm getting a lot of warnings* at init & every time I navigate. Maybe I did it wrong? Or maybe we simply shouldn't interfere at all with localePath & swithLocalePath? Or not calling them in the lib?

* not getting the differentDomains warn though

@rchl
Copy link
Collaborator

rchl commented Aug 23, 2019

Can't comment without seeing the code but my idea for warnings was to trigger them only when locale is passed AND it doesn't match current locale. Is that how you did it?

@richie3366
Copy link
Author

Can't comment without seeing the code but my idea for warnings was to trigger them only when locale is passed AND it doesn't match current locale. Is that how you did it?

I compared the values like you said:

  • localePath: when locale differs from current locale:
    if (!locale) return // <- was already there

    if(STRATEGY === STRATEGIES.NO_PREFIX && locale !== this.$i18n.locale) {
      console.warn('[<%= options.MODULE_NAME %>] localePath is unsupported when using no_prefix strategy', locale, this.$i18n.locale)
    }
  • switchLocalePath: when locale differs from default locale:
    if(STRATEGY === STRATEGIES.NO_PREFIX && locale && locale !== defaultLocale) {
      console.warn('[<%= options.MODULE_NAME %>] switchLocalePath is unsupported when using no_prefix strategy', locale, defaultLocale)
    }

This is what I have in my console:
Screenshot at 15-19-14

@rchl
Copy link
Collaborator

rchl commented Aug 23, 2019

Ok. Feel free to either investigate more or leave it out. I can look later.

@richie3366
Copy link
Author

Ok. Feel free to either investigate more or leave it out. I can look later.

You seem to have a good knowledge about the mechs/code of this lib, I think you'll find the fix quicker
(and maybe cleaner) than me

@richie3366
Copy link
Author

Whoops, forgot to run the tests before committing, something broke in between, sorry for that.

src/helpers/routes.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
types/vue.d.ts Outdated Show resolved Hide resolved
types/vue.d.ts Outdated Show resolved Hide resolved
@HapLifeMan
Copy link

The majority of your request changes are problems that would not occur with a stricter ESLint config, what do you think about complete the .eslintrc.js file @rchl? That would improve a lot every contribution to this repo and save a lot of time for each contributor and reviewer. 🙂

@rchl
Copy link
Collaborator

rchl commented Aug 23, 2019

@HapLifeMan If you can figure out a config that would work in files that contain lodash templates then I'm all ears. I've failed before. :)

@rchl
Copy link
Collaborator

rchl commented Aug 24, 2019

As for the warnings, it's due to seo-head.js adding alternate links for all locales. That code needs to be disabled for NO_PREFIX locale as we have no alternate links then. I think it's OK to keep the code that adds og:locale:alternate.

src/plugins/main.js Outdated Show resolved Hide resolved
@HapLifeMan
Copy link

I've updated files according to your last request changes!

About the ESLint, we have a very strict ESLint config where we are working and this is not my repo so I wouldn't want to oblige contributors to follow our personal ESLint rules without your permission.
But I'm pretty sure you can work with this plugin. If you're open to ESLint config suggestions that may involve a lot of syntax changes, let me know and I'll be happy to share it here 🙂

@rchl
Copy link
Collaborator

rchl commented Aug 24, 2019

This repo is already using pretty strict rules that would catch all the issues I've pointed out. Problem is that all files with templates are ignored. I've looked at that plugin before and had some problems but maybe those problems don't apply here. I'll check again later.

Will look at your changes a bit later when I'm home. Thanks :)

@HapLifeMan
Copy link

You're welcome, I said that because we have a 60 rules ESLint config 😉

@rchl
Copy link
Collaborator

rchl commented Aug 24, 2019

@HapLifeMan this project is extending this config: https://github.com/standard/eslint-config-standard/blob/master/eslintrc.json
So it has quite a bit of rules. :)

src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
src/plugins/routing.js Outdated Show resolved Hide resolved
@HapLifeMan
Copy link

Updated! Oh I didn't saw the extends, sorry about that 🙈

rchl
rchl previously approved these changes Aug 24, 2019
@rchl
Copy link
Collaborator

rchl commented Aug 24, 2019

I'm now happy with those changes.

Some remaining things that can be done separately:

  • problem I've mentioned in one of the comments:

There is also an issue with this API (setLocale) when using other than no-prefix strategies. It will change locale but route URL will stay unchanged (with previous locale prefix). I'm not sure what's the best way to handle that - two options I see are: call nuxt's redirect or replace route using VueRouter.replace(). Both can be destructive but I guess that should be OK... I can handle that myself later.

  • improving test coverage. Testing needs some cleanup...

@rchl rchl dismissed their stale review August 24, 2019 20:50

found new problem

@rchl rchl self-requested a review August 24, 2019 20:50
Copy link
Collaborator

@rchl rchl left a comment

Choose a reason for hiding this comment

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

Found some more problems while starting to look into tests:

  • setLocale API only works with lazy loading enabled. I'll have a look at that. FIXED
  • Generation of routes actually does something that should in theory also be done for no_prefix strategy - it generates routes with different name if user has specified name override. It would be problematic to make that work as we would need to then apply proper locale for that route regardless of what is currently used. I don't think there is enough context anywhere to figure that out... Maybe this should not be supported for now as it would require quite a lot of work probably...

@rchl
Copy link
Collaborator

rchl commented Aug 26, 2019

Status:

@richie3366
Copy link
Author

richie3366 commented Aug 26, 2019

HapLifeMan's commit : await was needed to fix a bug in our project, reason: vue-i18n was loading before nuxt-i18n had finished to init & load the default locale

My recent commits: cf. commit descriptions

Forgot to mention: my first commit of the day also implemented the auto-redirect to localePath when using setLocale programatically :)

src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
src/plugins/main.js Show resolved Hide resolved
src/plugins/main.js Outdated Show resolved Hide resolved
@rchl
Copy link
Collaborator

rchl commented Aug 26, 2019

HapLifeMan's commit : await was needed to fix a bug in our project, reason: vue-i18n was loading before nuxt-i18n had finished to init & load the default locale

Clearly a right thing to do. My bad for forgetting it.

@rchl rchl closed this Aug 26, 2019
@rchl rchl reopened this Aug 26, 2019
Copy link
Collaborator

@rchl rchl left a comment

Choose a reason for hiding this comment

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

I'll keep it unapproved for now as I'm still blocked with tests.

src/plugins/main.js Show resolved Hide resolved
@richie3366
Copy link
Author

I'll keep it unapproved for now as I'm still blocked with tests.

Okay, if you need anything else from me, feel free to ask!

@rchl rchl changed the title feat: Add NO_PREFIX strategy + setLazyLocale function feat: Add NO_PREFIX strategy + setLocale function Aug 26, 2019
rchl pushed a commit to Pickaw/nuxt-i18n that referenced this pull request Aug 26, 2019
@rchl rchl force-pushed the new-no-prefix branch 2 times, most recently from e8c5828 to 34f264e Compare August 26, 2019 18:35
@rchl rchl self-requested a review August 26, 2019 21:02
Copy link
Collaborator

@rchl rchl left a comment

Choose a reason for hiding this comment

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

I'm happy with this now. :)

Added a bugfix to wrongly stringified STRATEGIES, added tests and did some small refactorings.

Will rebase and merge this tomorrow after you agree that it's ready. :)

Thank you both for your work.

Copy link
Collaborator

@paulgv paulgv 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 so much for working on this @richie3366! This will be an awesome improvement!

@@ -39,6 +39,12 @@ module.exports = function (userOptions) {
}
}

if (!Object.values(STRATEGIES).includes(options.strategy)) {
// eslint-disable-next-line no-console
console.error('[' + options.MODULE_NAME + '] Invalid "strategy" option "' + options.strategy + '" (must be one of: ' + Object.values(STRATEGIES).join(', ') + ').')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding this check!

@HapLifeMan
Copy link

Will rebase and merge this tomorrow after you agree that it's ready. :)
Thank you both for your work.

Thank you for your suggestions and improvements! This is really a good thing to have an active community open to new features like that. You can rebase & merge 😄

Also:

 - chore: remove constants.js from coverage
   The coverage was patching the default beforeLanguageSwitch & onLanguageSwitched
   null functions, but tests failed because the patched functions referenced
   unknown (out of scope?) variables.

   + I think that making coverage on a constant-declarations file is kinda
   pointless since all the lines are always covered by design.

 - fix(tests): don't use v-for and v-if together - warning fix

 - refactor: hoist constants outside of exported function

 - refactor: don't return messages from loadLanguageAsync

Co-authored-by: Thomas Reichling <thomas.reichling@gmail.com>
Co-authored-by: Rafał Chłodnicki <rchl2k@gmail.com>
@rchl rchl merged commit 998011e into nuxt-modules:master Aug 27, 2019
@paulgv
Copy link
Collaborator

paulgv commented Aug 27, 2019

It's a bit late for me to notice this, but it looks like this PR might have broken something in the module, my first guess would be that it's related to the code that was removed from the middleware.

To reproduce the bug I'm seeing:

  • Run yarn dev:basic in the module's directory
  • Visit http://localhost:3000/
  • Click on About us
  • Click on Français to switch locales

I would expect to end up on http://localhost:3000/fr/a-propos but I'm actually redirected to http://localhost:3000/fr. The lang switcher's link itself is correct, so I assume that something in the middleware overrides the route and redirects to the homepage for whatever reason.

@richie3366 @rchl WDYT? Is there any way we can fix this quickly? Should we revert these changes for the time being?

@rchl
Copy link
Collaborator

rchl commented Aug 28, 2019

@paulgv have you tried fix in #418?

@rchl
Copy link
Collaborator

rchl commented Aug 28, 2019

Actually I can still reproduce with the fix. Will look more into it.

Let's move this issue over to the #418 PR.

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

4 participants