Mitch did you manage to figure this one out?
What was your solution?
I had exactly the same issue, where the 'else' statement was cutting my loop short, so I did this instead:
export const getLocale = (userConfig, knownLocales) => {
for (let i = 0; i < knownLocales.length; i++) {
if (userConfig.locale === knownLocales[i]) {
return userConfig.locale;
}
if (i === knownLocales.length - 1) {
return "en";
}
}
};
So basically, if i
increments as far as the final value in the array ("fr"
in this case)), return the string 'en'.
And no, this doesn't 'cut off' the possibility of returning the last value of the knownLocales array if it happens to match the userConfig.locale, because it will run AFTER the for loop has checked for that. I tested just to make sure 🙂
...I was hoping to come up with a more elegant solution, but this is the best I could come up with for now!
Would love to hear what you did!