i18next / i18next-gitbook

165 stars 172 forks source link

Fix erroneous code in error interpolation example #145

Closed lzilioli closed 3 years ago

lzilioli commented 3 years ago

The code directly below my code change declares 2 const variables, with values 404 and 502, respectively. Without this fix, the code sample would log undefined since 502 is not a key in the provided map.

Checklist

jamuhl commented 3 years ago

There is no error -> that samples shows you can pass an array of keys which resolves in order -> in case of 502 to the unspecific error: https://jsfiddle.net/hug87t2n/1/

lzilioli commented 3 years ago

Got it. I believe it could be tweaked for clarity, but don't feel strongly.

It's not clear to me what the const error 502 is needed for. I see it gets passed as an argument, however it doesn't render anywhere visibly in the sample that you linked.

Thanks for your time. Awesome library!

jamuhl commented 3 years ago

the const error 502 is something you get eg. from your server response....

lzilioli commented 3 years ago

I understand that. What I did not understand when I opened the PR is what the i18n library would be doing with the string "error.502" When the only visible output does not contain such a string anywhere.

After re-reading a few times, I now understand that this example is showing that you can fallback to a different key in the event your desired key is not defined. I still believe that could be a bit more clear.

Something like:


You can fall back to a generic string if the one you are looking for is not available

 {
     "error": {
         "fallback": "An unknown error occurred",
         "502": "Something went wrong.",
         "404": "The page was not found."
     }
 }

Sample

const fallback = 'error.fallback';
i18next.t([`error.404`, fallback]) // -> "The page was not found"​
i18next.t([`error.502`, fallback]) // -> "Something went wrong"
i18next.t([`error.500`, fallback]) // -> "An unknown error occurred"
jamuhl commented 3 years ago

Still practical the first one is build dynamically from the error status and the fallback is static string. So your sample might be easier to understand for you - but is more away from what you do in reality

const error = '502'; // eg. coming from res.status of a HTTP request

lzilioli commented 3 years ago

You can fall back to a generic string if the one you are looking for is not available

 {
     "error": {
         "fallback": "An unknown error occurred",
         "502": "Something went wrong.",
         "404": "The page was not found."
     }
 }

Sample

const fallback = 'error.fallback';
let error = 404; // e.g. 404, 502, 500
i18next.t([`error.${error}`, fallback]) // -> error: 404                    output: "The page was not found"​
i18next.t([`error.${error}`, fallback]) // -> error: 502                    output: "Something went wrong."
i18next.t([`error.${error}`, fallback]) // -> error: 500, 403, etc.   output: "An unknown error occurred​"

Fixed the extant compiler error too this time ;)