i18n.js 5.2 KB

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