i18n.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 language.
  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() || [defaultSettings.lang] );
  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. if ( !text ) {
  53. if ( fallback < this.fallback.length ) {
  54. lang = this.fallback[fallback];
  55. fallback++;
  56. text = i18n?.[lang];
  57. n = -1;
  58. }
  59. else {
  60. n = keys.length;
  61. }
  62. }
  63. }
  64. if ( typeof text === 'string' ) {
  65. args.forEach( (arg, i) => {
  66. text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  67. } );
  68. text = text.replace( /{{\s*PLURAL:\s*(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  69. return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  70. } );
  71. }
  72. return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  73. }
  74. // /**
  75. // * Get a localized message.
  76. // * @param {String} message - Name of the message.
  77. // * @param {String[]} args - Arguments for the message.
  78. // * @returns {String}
  79. // */
  80. // get(message = '', ...args) {
  81. // if ( this.namespace.length ) message = this.namespace + '.' + message;
  82. // let lang = this.lang;
  83. // let text = i18n?.[lang]?.[message];
  84. // let fallback = 0;
  85. // while ( !text ) {
  86. // if ( fallback < this.fallback.length ) {
  87. // lang = this.fallback[fallback];
  88. // fallback++;
  89. // text = i18n?.[lang]?.[message];
  90. // }
  91. // else break;
  92. // }
  93. // if ( typeof text === 'string' ) {
  94. // args.forEach( (arg, i) => {
  95. // text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  96. // } );
  97. // text = text.replace( /{{\s*PLURAL:\s*(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  98. // return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  99. // } );
  100. // }
  101. // return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  102. // }
  103. /**
  104. * Get names for all languages.
  105. * @param {Boolean} isRcGcDw - Get the languages for RcGcDw?
  106. * @returns {Object}
  107. * @static
  108. */
  109. static allLangs(isRcGcDw = false) {
  110. if ( isRcGcDw ) return i18n.RcGcDw;
  111. return i18n.allLangs;
  112. }
  113. }
  114. /**
  115. * Parse plural text.
  116. * @param {String} lang - The language code.
  117. * @param {Number} number - The amount.
  118. * @param {String[]} args - The possible text.
  119. * @returns {String}
  120. */
  121. function plural(lang, number, args) {
  122. var text = args[args.length - 1];
  123. switch ( lang ) {
  124. case 'fr':
  125. if ( number <= 1 ) text = getArg(args, 0);
  126. else text = getArg(args, 1);
  127. break;
  128. case 'pl':
  129. if ( number === 1 ) text = getArg(args, 0);
  130. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  131. text = getArg(args, 1);
  132. }
  133. else text = getArg(args, 2);
  134. break;
  135. case 'ru':
  136. if ( args.length > 2 ) {
  137. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  138. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  139. text = getArg(args, 1);
  140. }
  141. else text = getArg(args, 2);
  142. }
  143. else {
  144. if ( number === 1 ) text = getArg(args, 0);
  145. else text = getArg(args, 1);
  146. }
  147. break;
  148. case 'de':
  149. case 'en':
  150. case 'nl':
  151. case 'pt':
  152. case 'tr':
  153. case 'zh':
  154. default:
  155. if ( number === 1 ) text = getArg(args, 0);
  156. else text = getArg(args, 1);
  157. }
  158. return text;
  159. }
  160. /**
  161. * Get text option.
  162. * @param {String[]} args - The list of options.
  163. * @param {Number} index - The preferred option.
  164. * @returns {String}
  165. */
  166. function getArg(args, index) {
  167. return ( args.length > index ? args[index] : args[args.length - 1] );
  168. }
  169. module.exports = Lang;