i18n.js 3.9 KB

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