i18next: handle JSON errors in translation files

Hi,

Thanks for this lib !

Is there a way to catch JSON typo client side ?

I mean, if i forget a coma, i18next will not parse the file, logical. I can see it using debug

loaded: locales/fr/translation.json 
failed loading: locales/en/translation.json

Is there an init option to handle errors ?

Something like

{
   debug:false,
   onLoadError:function(err,file) {
   }
}

Cheers

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 17 (14 by maintainers)

Most upvoted comments

That’s it ! thanks

        customLoad: function(lng, ns, options, loadComplete) {
            var url = '/locales/'+lng+'/'+ns+'.json';
            $.ajax({
                url:url,
                success:function(res) {
                    loadComplete(null,res);
                },
                error:function(err) {
                    if (err.status == 200) {
                        // file loaded but invalid json, stop waste time !
                        console.log('There is a typo in '+url);
                    } else if (err.status == 404) {
                        // do nothing in my case
                    } else {
                        alert(err.status+' when loading '+url);
                    }
                    loadComplete(err,null);
                }
            });