-
-
Notifications
You must be signed in to change notification settings - Fork 764
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
Fix 1181: Missing namespaces if they are not in the defaultLanguage (but exist in the fallbackLng) #1197
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/isaachinman/next-i18next/4Q5Q3ff2bBG7GgEgxxzcksomhzFE |
I'm working on the code to make it pass on codeclimate and multi-build 😁 I don't know if all remarks of codeclimate are justified, (Similar blocks for expected variable in tests) but I'll do my best :) |
Done 😘 |
src/config/createConfig.test.ts
Outdated
}) | ||
|
||
it('gets namespaces from current language + fallback (as object) when ns is not provided', ()=>{ | ||
const fallbackLng = {default: ['fr'], 'en-US': ['en']} as unknown |
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.
const fallbackLng = {default: ['fr'], 'en-US': ['en']} as unknown | |
const fallbackLng = { default: ['fr'], 'en-US': ['en'] } as unknown |
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.
Do you think that these rules could be added in the linter?
src/config/createConfig.ts
Outdated
) | ||
combinedConfig.ns = getAllNamespaces(path.resolve(process.cwd(), `${serverLocalePath}/${lng}`)) | ||
if (!combinedConfig.ns && typeof lng !== 'undefined') { | ||
const unique = (list:string[]) => Array.from(new Set<string>(list)) |
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.
const unique = (list:string[]) => Array.from(new Set<string>(list)) | |
const unique = (list: string[]) => Array.from(new Set<string>(list)) |
src/config/createConfig.ts
Outdated
.reduce( | ||
(flattenNamespaces, namespaces) => | ||
[...flattenNamespaces,...namespaces], | ||
[] | ||
) |
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.
Is there a way to achieve this without using reduce
?
src/config/createConfig.ts
Outdated
if (typeof fallbackLng === 'string') | ||
return unique([lng, fallbackLng]) | ||
|
||
if (Array.isArray(fallbackLng)) | ||
return unique([lng, ...fallbackLng]) | ||
|
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.
Let's use curly braces for any if
statements.
src/config/createConfig.ts
Outdated
if (typeof fallbackLng === 'object') { | ||
const flattenedFallbacks = Object | ||
.values(fallbackLng) | ||
.reduce(((all, fallbackLngs) => [...all,...fallbackLngs]),[]) |
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.
.reduce(((all, fallbackLngs) => [...all,...fallbackLngs]),[]) | |
.reduce(((all, fallbackLngs) => [ ...all, ...fallbackLngs ]),[]) |
src/config/createConfig.ts
Outdated
const flattenedFallbacks = Object | ||
.values(fallbackLng) | ||
.reduce(((all, fallbackLngs) => [...all,...fallbackLngs]),[]) | ||
return unique([lng, ...flattenedFallbacks]) |
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.
return unique([lng, ...flattenedFallbacks]) | |
return unique([ lng, ...flattenedFallbacks ]) |
src/serverSideTranslations.ts
Outdated
|
||
const DEFAULT_CONFIG_PATH = './next-i18next.config.js' | ||
|
||
const getFallBackLocales = (fallbackLng: false | FallbackLng) => { |
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.
const getFallBackLocales = (fallbackLng: false | FallbackLng) => { | |
const getFallbackLocales = (fallbackLng: false | FallbackLng) => { |
if (typeof fallbackLng === 'object') { | ||
return Object | ||
.values(fallbackLng) | ||
.reduce((all, locales) => [...all, ...locales],[]) |
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.
Again, can we use a different method than reduce
?
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.
Did a loop. There is array.flat()
but it's not available in node 10 :(
src/serverSideTranslations.ts
Outdated
if (Array.isArray(fallbackLng)) { | ||
return fallbackLng | ||
} | ||
if (typeof fallbackLng === 'object') { |
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.
Won't this condition also enter for null
?
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 right, but I wonder if the fallbackLng can have the null value. I'll add an extra condition :)
src/serverSideTranslations.ts
Outdated
.reduce( | ||
(allNamespaces, namespaces) => [...allNamespaces,...namespaces], | ||
[] |
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.
Again, possible to use something other than reduce
?
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.
of course :)
Done everything 🥳 Cyclomatic complexity to 5 is a challenge 😁 |
Hi @isaachinman , do I have to do something more for this PR? |
Nope, looks reasonable to me! Thanks for all your hard work @Nikoms! |
Thank youuu, I'll wait for the "official" new version. I believe 8.3.1 ? |
Fix for #1181
Tell me if I need to modify things 🤓
In
createConfig.test.ts
, I had to add aas unknown
forfallbackLng
(line 64). Don't know why, because it's a valid value.Cheers!