i18n.js 4.5 KB

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