i18n.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
  128. var text = args[args.length - 1];
  129. switch ( lang ) {
  130. case 'fr':
  131. if ( number <= 1 ) text = getArg(args, 0);
  132. else text = getArg(args, 1);
  133. break;
  134. case 'pl':
  135. if ( number === 1 ) 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. break;
  141. case 'ru':
  142. if ( args.length > 2 ) {
  143. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  144. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  145. text = getArg(args, 1);
  146. }
  147. else text = getArg(args, 2);
  148. }
  149. else {
  150. if ( number === 1 ) text = getArg(args, 0);
  151. else text = getArg(args, 1);
  152. }
  153. break;
  154. case 'de':
  155. case 'en':
  156. case 'es':
  157. case 'nl':
  158. case 'pt':
  159. case 'tr':
  160. case 'ja':
  161. case 'zh-hans':
  162. case 'zh-hant':
  163. default:
  164. if ( number === 1 ) text = getArg(args, 0);
  165. else text = getArg(args, 1);
  166. }
  167. return text;
  168. }
  169. /**
  170. * Get text option.
  171. * @param {String[]} args - The list of options.
  172. * @param {Number} index - The preferred option.
  173. * @returns {String}
  174. */
  175. function getArg(args, index) {
  176. return ( args.length > index ? args[index] : args[args.length - 1] );
  177. }
  178. module.exports = Lang;