github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/client/src/app/utils/lang.js (about)

     1  import TextMap from './text-map';
     2  import {formatString} from './string';
     3  
     4  export class LangHelper extends TextMap {
     5      constructor(name, langData) {
     6          super(langData);
     7          this._name = name;
     8      }
     9  
    10      change(name, langData) {
    11          this._data = langData;
    12          this._name = name;
    13      }
    14  
    15      get name() {
    16          return this._name;
    17      }
    18  
    19      error(err) {
    20          if (!err) {
    21              if (DEBUG) {
    22                  console.collapse('LANG.error', 'redBg', '<Unknown Error>', 'redPale');
    23                  console.error(err);
    24                  console.groupEnd();
    25              }
    26              return '<Unknown Error>';
    27          }
    28          if (typeof err === 'string') {
    29              return this.string(err.startsWith('error.') ? err : `error.${err}`, err);
    30          }
    31          if (Array.isArray(err)) {
    32              return err.map(this.error).join(';');
    33          }
    34          let message = '';
    35          if (err.code) {
    36              message += this.string(`error.${err.code}`, `${err.message || ''}[${err.code}]`);
    37          } else if (err.message) {
    38              message = this.string(`error.${err.message}`, err.message);
    39          }
    40          if (message) {
    41              let formatParams = err.formats || err.extras;
    42              if (formatParams) {
    43                  if (typeof formatParams === 'object' && !Array.isArray(formatParams)) {
    44                      message = formatString(message, formatParams);
    45                  } else {
    46                      if (!Array.isArray(formatParams)) {
    47                          formatParams = [formatParams];
    48                      }
    49                      message = formatString(message, ...formatParams);
    50                  }
    51              }
    52          }
    53          return message;
    54      }
    55  }