-
Notifications
You must be signed in to change notification settings - Fork 462
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
|
There was a problem hiding this 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. :)
Also this code should be disabled with no-prefix strategy: |
Please also add two new APIs here: |
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. |
@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? |
Feel free to add new commits. It's easier to review that way. I can rewrite stuff when merging. Thanks |
There was a problem hiding this 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 whenlocale
argument is different than current locale. Basically there is no way to handle that withno-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 withno-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
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:
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)
}
if(STRATEGY === STRATEGIES.NO_PREFIX && locale && locale !== defaultLocale) {
console.warn('[<%= options.MODULE_NAME %>] switchLocalePath is unsupported when using no_prefix strategy', locale, defaultLocale)
} |
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 |
Whoops, forgot to run the tests before committing, something broke in between, sorry for that. |
The majority of your request changes are problems that would not occur with a stricter ESLint config, what do you think about complete the |
@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. :) |
As for the warnings, it's due to seo-head.js adding |
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. |
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 :) |
You're welcome, I said that because we have a 60 rules ESLint config 😉 |
@HapLifeMan this project is extending this config: https://github.com/standard/eslint-config-standard/blob/master/eslintrc.json |
Updated! Oh I didn't saw the |
I'm now happy with those changes. Some remaining things that can be done separately:
|
There was a problem hiding this 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:
FIXEDsetLocale
API only works with lazy loading enabled. I'll have a look at that.- 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...
Status:
|
HapLifeMan's commit : My recent commits: cf. commit descriptions Forgot to mention: my first commit of the day also implemented the auto-redirect to localePath when using |
Clearly a right thing to do. My bad for forgetting it. |
There was a problem hiding this 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.
Okay, if you need anything else from me, feel free to ask! |
e8c5828
to
34f264e
Compare
There was a problem hiding this 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.
There was a problem hiding this 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(', ') + ').') |
There was a problem hiding this comment.
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!
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>
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:
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? |
Actually I can still reproduce with the fix. Will look more into it. Let's move this issue over to the #418 PR. |
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 inapp.i18n
to let developers call it programatically.