i18n.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const {defaultSettings} = require('./default.json');
  2. var i18n = require('../i18n/allLangs.json');
  3. Object.keys(i18n.allLangs.names).forEach( lang => i18n[lang] = require('../i18n/' + lang + '.json') );
  4. const defaultAliases = ( i18n?.[defaultSettings.lang]?.aliases || {} );
  5. /**
  6. * A langauge.
  7. * @class
  8. */
  9. class Lang {
  10. /**
  11. * Creates a new language.
  12. * @param {String} [lang] - The language code.
  13. * @param {String} [namespace] - The namespace for the language.
  14. * @constructs Lang
  15. */
  16. constructor(lang = defaultSettings.lang, namespace = '') {
  17. this.lang = lang;
  18. this.namespace = namespace;
  19. this.fallback = ( i18n?.[lang]?.fallback.slice() || [] );
  20. this.localNames = {};
  21. this.aliases = {};
  22. let aliases = ( i18n?.[lang]?.aliases || {} );
  23. Object.keys(aliases).forEach( cmd => {
  24. if ( !( cmd in this.localNames ) ) this.localNames[cmd] = aliases[cmd][0];
  25. aliases[cmd].forEach( alias => {
  26. if ( !( alias in this.aliases ) ) this.aliases[alias] = cmd;
  27. } );
  28. } );
  29. Object.keys(defaultAliases).forEach( cmd => {
  30. if ( !( cmd in this.localNames ) ) this.localNames[cmd] = defaultAliases[cmd][0];
  31. defaultAliases[cmd].forEach( alias => {
  32. if ( !( alias in this.aliases ) ) this.aliases[alias] = cmd;
  33. } );
  34. } );
  35. }
  36. /**
  37. * Get a localized message.
  38. * @param {String} message - Name of the message.
  39. * @param {String[]} args - Arguments for the message.
  40. * @returns {String}
  41. */
  42. get(message = '', ...args) {
  43. if ( this.namespace.length ) message = this.namespace + '.' + message;
  44. let keys = ( message.length ? message.split('.') : [] );
  45. let lang = this.lang;
  46. let text = i18n?.[lang];
  47. let fallback = 0;
  48. for (let n = 0; n < keys.length; n++) {
  49. if ( text ) {
  50. text = text?.[keys[n]];
  51. }
  52. else if ( fallback < this.fallback.length ) {
  53. lang = this.fallback[fallback];
  54. fallback++;
  55. text = i18n?.[lang];
  56. n = 0;
  57. }
  58. else {
  59. n = keys.length;
  60. }
  61. }
  62. if ( typeof text === 'string' ) {
  63. args.forEach( (arg, i) => {
  64. text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  65. } );
  66. text = text.replace( /{{\s*PLURAL:\s*(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  67. return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  68. } );
  69. }
  70. return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  71. }
  72. // /**
  73. // * Get a localized message.
  74. // * @param {String} message - Name of the message.
  75. // * @param {String[]} args - Arguments for the message.
  76. // * @returns {String}
  77. // */
  78. // get(message = '', ...args) {
  79. // if ( this.namespace.length ) message = this.namespace + '.' + message;
  80. // let lang = this.lang;
  81. // let text = i18n?.[lang]?.[message];
  82. // let fallback = 0;
  83. // while ( !text ) {
  84. // if ( fallback < this.fallback.length ) {
  85. // lang = this.fallback[fallback];
  86. // fallback++;
  87. // text = i18n?.[lang]?.[message];
  88. // }
  89. // else break;
  90. // }
  91. // if ( typeof text === 'string' ) {
  92. // args.forEach( (arg, i) => {
  93. // text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  94. // } );
  95. // text = text.replace( /{{\s*PLURAL:\s*(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  96. // return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  97. // } );
  98. // }
  99. // return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  100. // }
  101. /**
  102. * Get names for all langauges.
  103. * @param {Boolean} isRcGcDw - Get the langauge for RcGcDw?
  104. * @returns {Object}
  105. * @static
  106. */
  107. static allLangs(isRcGcDw = false) {
  108. if ( isRcGcDw ) return i18n.RcGcDw;
  109. return i18n.allLangs;
  110. }
  111. }
  112. /**
  113. * Parse plural text.
  114. * @param {String} lang - The language code.
  115. * @param {Number} number - The amount.
  116. * @param {String[]} args - The possible text.
  117. * @returns {String}
  118. */
  119. function plural(lang, number, args) {
  120. var text = args[args.length - 1];
  121. switch ( lang ) {
  122. case 'fr':
  123. if ( number <= 1 ) text = getArg(args, 0);
  124. else text = getArg(args, 1);
  125. break;
  126. case 'pl':
  127. if ( number === 1 ) text = getArg(args, 0);
  128. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  129. text = getArg(args, 1);
  130. }
  131. else text = getArg(args, 2);
  132. break;
  133. case 'ru':
  134. if ( args.length > 2 ) {
  135. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  136. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  137. text = getArg(args, 1);
  138. }
  139. else text = getArg(args, 2);
  140. }
  141. else {
  142. if ( number === 1 ) text = getArg(args, 0);
  143. else text = getArg(args, 1);
  144. }
  145. break;
  146. case 'de':
  147. case 'en':
  148. case 'nl':
  149. case 'pt':
  150. case 'tr':
  151. case 'zh':
  152. default:
  153. if ( number === 1 ) text = getArg(args, 0);
  154. else text = getArg(args, 1);
  155. }
  156. return text;
  157. }
  158. /**
  159. * Get text option.
  160. * @param {String[]} args - The list of options.
  161. * @param {Number} index - The preferred option.
  162. * @returns {String}
  163. */
  164. function getArg(args, index) {
  165. return ( args.length > index ? args[index] : args[args.length - 1] );
  166. }
  167. module.exports = Lang;