i18n.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  74. return gender(type, cases.split(/\s*\|\s*/));
  75. } );
  76. if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  77. return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  78. } );
  79. }
  80. return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  81. }
  82. // /**
  83. // * Get a localized message.
  84. // * @param {String} message - Name of the message.
  85. // * @param {String[]} args - Arguments for the message.
  86. // * @returns {String}
  87. // */
  88. // get(message = '', ...args) {
  89. // if ( this.namespace.length ) message = this.namespace + '.' + message;
  90. // let lang = this.lang;
  91. // let text = i18n?.[lang]?.[message];
  92. // let fallback = 0;
  93. // while ( !text ) {
  94. // if ( fallback < this.fallback.length ) {
  95. // lang = this.fallback[fallback];
  96. // fallback++;
  97. // text = i18n?.[lang]?.[message];
  98. // }
  99. // else break;
  100. // }
  101. // if ( typeof text === 'string' ) {
  102. // args.forEach( (arg, i) => {
  103. // text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  104. // } );
  105. // if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  106. // return gender(type, cases.split(/\s*\|\s*/));
  107. // } );
  108. // if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  109. // return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  110. // } );
  111. // }
  112. // return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  113. // }
  114. /**
  115. * Get names for all languages.
  116. * @param {Boolean} isRcGcDw - Get the languages for RcGcDw?
  117. * @returns {Object}
  118. * @static
  119. */
  120. static allLangs(isRcGcDw = false) {
  121. if ( isRcGcDw ) return i18n.RcGcDw;
  122. return i18n.allLangs;
  123. }
  124. }
  125. /**
  126. * Parse gender text.
  127. * @param {String} gender - The gender.
  128. * @param {String[]} args - The possible text.
  129. * @returns {String}
  130. */
  131. function gender(gender, args) {
  132. var text = args[0];
  133. switch ( gender ) {
  134. case 'male':
  135. if ( args.length > 0 ) text = args[0];
  136. break;
  137. case 'female':
  138. if ( args.length > 1 ) text = args[1];
  139. break;
  140. case 'unknown':
  141. default:
  142. if ( args.length > 2 ) text = args[2];
  143. }
  144. return text;
  145. }
  146. /**
  147. * Parse plural text.
  148. * @param {String} lang - The language code.
  149. * @param {Number} number - The amount.
  150. * @param {String[]} args - The possible text.
  151. * @returns {String}
  152. */
  153. function plural(lang, number, args) {
  154. // https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
  155. var text = args[args.length - 1];
  156. switch ( lang ) {
  157. case 'fr':
  158. case 'hi':
  159. if ( number <= 1 ) text = getArg(args, 0);
  160. else text = getArg(args, 1);
  161. break;
  162. case 'pl':
  163. if ( number === 1 ) text = getArg(args, 0);
  164. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  165. text = getArg(args, 1);
  166. }
  167. else text = getArg(args, 2);
  168. break;
  169. case 'ru':
  170. if ( args.length > 2 ) {
  171. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  172. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  173. text = getArg(args, 1);
  174. }
  175. else text = getArg(args, 2);
  176. }
  177. else {
  178. if ( number === 1 ) text = getArg(args, 0);
  179. else text = getArg(args, 1);
  180. }
  181. break;
  182. case 'de':
  183. case 'en':
  184. case 'es':
  185. case 'nl':
  186. case 'pt-br':
  187. case 'tr':
  188. case 'ja':
  189. case 'zh-hans':
  190. case 'zh-hant':
  191. default:
  192. if ( number === 1 ) text = getArg(args, 0);
  193. else text = getArg(args, 1);
  194. }
  195. return text;
  196. }
  197. /**
  198. * Get text option.
  199. * @param {String[]} args - The list of options.
  200. * @param {Number} index - The preferred option.
  201. * @returns {String}
  202. */
  203. function getArg(args, index) {
  204. return ( args.length > index ? args[index] : args[args.length - 1] );
  205. }
  206. module.exports = Lang;