0

I have this JS function

function trans(key, replace = {})
{
  let language = 'de';
  if(window.currentLanguage !== undefined) {
    language = window.currentLanguage;
  }
  let translation = key.split('.').reduce((t, i) => t[i] || null, window.translations[language]);

  for (var placeholder in replace) {
      translation = translation.replace(`:${placeholder}`, replace[placeholder]);
  }

  return translation;
}

It works fine in Chrome, Firefox, Opera etc. But in IE (Mode 11) it throws this error:

")" expected

Actually, there is no error, otherwise it would not work in Chrome etc. And I also checked all brackets. Each opening bracket is being closed. How can I re-write this function, that it works in IE (Mode 11) also?

halfer
  • 19,824
  • 17
  • 99
  • 186
dns_nx
  • 3,651
  • 4
  • 37
  • 66
  • 1
    IE probably doesn't support parameter default values. – Barmar Nov 05 '19 at 17:57
  • On what line does IE throw the error? – dezman Nov 05 '19 at 17:57
  • I don't think IE supports template strings, either. – VLAZ Nov 05 '19 at 17:57
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Browser_Compatibility - probably only one of your problems. I don't get why people write code for IE and don't expect and read up on the 23847562348576234785 pitfalls that it has :S – ASDFGerte Nov 05 '19 at 17:58
  • https://babeljs.io/docs/en/babel-plugin-transform-parameters – dezman Nov 05 '19 at 17:58
  • This code contains four features that are not supported in IE. You need to familiarize yourself with the new syntactical features that have been added from ES6 onward and understand that they are not supported in IE. – JLRishe Nov 05 '19 at 17:59
  • currently IE doesn't support default value, you can check browser compability on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters you can change your code to ES5 to fix it – Raqael Fisabillah Ramadhan Nov 05 '19 at 18:23
  • Ok. Thanks for your comments. I re-wrote the function now for IE compatibility with your help! – dns_nx Nov 05 '19 at 19:23

0 Answers0