i18n.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if ( typeof lang !== 'string' ) lang = defaultSettings.lang;
  18. this.lang = lang;
  19. this.namespace = namespace;
  20. this.fallback = ( i18n?.[lang]?.fallback.slice() || [defaultSettings.lang] ).filter( fb => fb.trim() );
  21. this.localNames = {};
  22. this.aliases = {};
  23. let aliases = ( i18n?.[lang]?.aliases || {} );
  24. Object.keys(aliases).forEach( cmd => {
  25. if ( aliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
  26. this.localNames[cmd] = aliases[cmd][0];
  27. }
  28. aliases[cmd].forEach( alias => {
  29. if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
  30. } );
  31. } );
  32. Object.keys(defaultAliases).forEach( cmd => {
  33. if ( defaultAliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
  34. this.localNames[cmd] = defaultAliases[cmd][0];
  35. }
  36. defaultAliases[cmd].forEach( alias => {
  37. if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
  38. } );
  39. } );
  40. }
  41. /**
  42. * Get a localized message.
  43. * @param {String} message - Name of the message.
  44. * @param {String[]} args - Arguments for the message.
  45. * @returns {String}
  46. */
  47. get(message = '', ...args) {
  48. if ( this.namespace.length ) message = this.namespace + '.' + message;
  49. let keys = ( message.length ? message.split('.') : [] );
  50. let lang = this.lang;
  51. let text = i18n?.[lang];
  52. let fallback = 0;
  53. for ( let n = 0; n < keys.length; n++ ) {
  54. if ( text ) {
  55. text = text?.[keys[n]];
  56. if ( typeof text === 'string' ) text = text.trim()
  57. }
  58. if ( !text ) {
  59. if ( fallback < this.fallback.length ) {
  60. lang = this.fallback[fallback];
  61. fallback++;
  62. text = i18n?.[lang];
  63. n = -1;
  64. }
  65. else {
  66. n = keys.length;
  67. }
  68. }
  69. }
  70. if ( typeof text === 'string' ) {
  71. args.forEach( (arg, i) => {
  72. text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  73. } );
  74. if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  75. return gender(type, cases.split(/\s*\|\s*/));
  76. } );
  77. if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  78. return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  79. } );
  80. }
  81. return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  82. }
  83. // /**
  84. // * Get a localized message.
  85. // * @param {String} message - Name of the message.
  86. // * @param {String[]} args - Arguments for the message.
  87. // * @returns {String}
  88. // */
  89. // get(message = '', ...args) {
  90. // if ( this.namespace.length ) message = this.namespace + '.' + message;
  91. // let lang = this.lang;
  92. // let text = i18n?.[lang]?.[message];
  93. // let fallback = 0;
  94. // while ( !text ) {
  95. // if ( fallback < this.fallback.length ) {
  96. // lang = this.fallback[fallback];
  97. // fallback++;
  98. // text = i18n?.[lang]?.[message];
  99. // }
  100. // else break;
  101. // }
  102. // if ( typeof text === 'string' ) {
  103. // args.forEach( (arg, i) => {
  104. // text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  105. // } );
  106. // if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  107. // return gender(type, cases.split(/\s*\|\s*/));
  108. // } );
  109. // if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  110. // return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  111. // } );
  112. // }
  113. // return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  114. // }
  115. /**
  116. * Get names for all languages.
  117. * @param {Boolean} isRcGcDw - Get the languages for RcGcDw?
  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 'bn':
  183. case 'de':
  184. case 'en':
  185. case 'es':
  186. case 'it':
  187. case 'ja':
  188. case 'ko':
  189. case 'nl':
  190. case 'pt-br':
  191. case 'th':
  192. case 'tr':
  193. case 'vi':
  194. case 'zh-hans':
  195. case 'zh-hant':
  196. default:
  197. if ( number === 1 ) text = getArg(args, 0);
  198. else text = getArg(args, 1);
  199. }
  200. return text;
  201. }
  202. /**
  203. * Get text option.
  204. * @param {String[]} args - The list of options.
  205. * @param {Number} index - The preferred option.
  206. * @returns {String}
  207. */
  208. function getArg(args, index) {
  209. return ( args.length > index ? args[index] : args[args.length - 1] );
  210. }
  211. module.exports = Lang;