i18n.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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() && !( cmd in this.localNames ) ) {
  26. this.localNames[cmd] = aliases[cmd][0];
  27. }
  28. aliases[cmd].forEach( alias => {
  29. if ( alias.trim() && !( alias in this.aliases ) ) this.aliases[alias] = cmd;
  30. } );
  31. } );
  32. Object.keys(defaultAliases).forEach( cmd => {
  33. if ( defaultAliases[cmd][0].trim() && !( cmd in this.localNames ) ) {
  34. this.localNames[cmd] = defaultAliases[cmd][0];
  35. }
  36. defaultAliases[cmd].forEach( alias => {
  37. if ( alias.trim() && !( alias in this.aliases ) ) 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. * @returns {{names: {en: 'English'}, map: {english: 'en'}}}
  119. * @static
  120. */
  121. static allLangs(isRcGcDw = false) {
  122. if ( isRcGcDw ) return i18n.RcGcDw;
  123. return i18n.allLangs;
  124. }
  125. }
  126. /**
  127. * Parse gender text.
  128. * @param {String} gender - The gender.
  129. * @param {String[]} args - The possible text.
  130. * @returns {String}
  131. */
  132. function gender(gender, args) {
  133. var text = args[0];
  134. switch ( gender ) {
  135. case 'male':
  136. if ( args.length > 0 ) text = args[0];
  137. break;
  138. case 'female':
  139. if ( args.length > 1 ) text = args[1];
  140. break;
  141. case 'unknown':
  142. default:
  143. if ( args.length > 2 ) text = args[2];
  144. }
  145. return text;
  146. }
  147. /**
  148. * Parse plural text.
  149. * @param {String} lang - The language code.
  150. * @param {Number} number - The amount.
  151. * @param {String[]} args - The possible text.
  152. * @returns {String}
  153. */
  154. function plural(lang, number, args) {
  155. // https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
  156. var text = args[args.length - 1];
  157. switch ( lang ) {
  158. case 'fr':
  159. case 'hi':
  160. if ( number <= 1 ) text = getArg(args, 0);
  161. else text = getArg(args, 1);
  162. break;
  163. case 'pl':
  164. if ( number === 1 ) text = getArg(args, 0);
  165. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  166. text = getArg(args, 1);
  167. }
  168. else text = getArg(args, 2);
  169. break;
  170. case 'ru':
  171. if ( args.length > 2 ) {
  172. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  173. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  174. text = getArg(args, 1);
  175. }
  176. else text = getArg(args, 2);
  177. }
  178. else {
  179. if ( number === 1 ) text = getArg(args, 0);
  180. else text = getArg(args, 1);
  181. }
  182. break;
  183. case 'bn':
  184. case 'de':
  185. case 'en':
  186. case 'es':
  187. case 'ja':
  188. case 'nl':
  189. case 'pt-br':
  190. case 'th':
  191. case 'tr':
  192. case 'ja':
  193. case 'zh-hans':
  194. case 'zh-hant':
  195. default:
  196. if ( number === 1 ) text = getArg(args, 0);
  197. else text = getArg(args, 1);
  198. }
  199. return text;
  200. }
  201. /**
  202. * Get text option.
  203. * @param {String[]} args - The list of options.
  204. * @param {Number} index - The preferred option.
  205. * @returns {String}
  206. */
  207. function getArg(args, index) {
  208. return ( args.length > index ? args[index] : args[args.length - 1] );
  209. }
  210. module.exports = Lang;