i18n.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. else if ( lang === 'allLangs' || !i18n.hasOwnProperty(lang) ) {
  19. if ( i18n.allLangs.map.hasOwnProperty(lang.toLowerCase()) ) {
  20. lang = i18n.allLangs.map[lang.toLowerCase()];
  21. }
  22. else lang = defaultSettings.lang;
  23. }
  24. this.lang = lang;
  25. this.namespace = namespace;
  26. this.fallback = ( i18n?.[lang]?.fallback.slice() || [defaultSettings.lang] ).filter( fb => fb.trim() );
  27. this.localNames = {};
  28. this.aliases = {};
  29. let aliases = ( i18n?.[lang]?.aliases || {} );
  30. Object.keys(aliases).forEach( cmd => {
  31. if ( aliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
  32. this.localNames[cmd] = aliases[cmd][0];
  33. }
  34. aliases[cmd].forEach( alias => {
  35. if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
  36. } );
  37. } );
  38. Object.keys(defaultAliases).forEach( cmd => {
  39. if ( defaultAliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
  40. this.localNames[cmd] = defaultAliases[cmd][0];
  41. }
  42. defaultAliases[cmd].forEach( alias => {
  43. if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
  44. } );
  45. } );
  46. }
  47. /**
  48. * Get a localized message.
  49. * @param {String} message - Name of the message.
  50. * @param {String[]} args - Arguments for the message.
  51. * @returns {String}
  52. */
  53. get(message = '', ...args) {
  54. if ( this.namespace.length ) message = this.namespace + '.' + message;
  55. let keys = ( message.length ? message.split('.') : [] );
  56. let lang = this.lang;
  57. let text = i18n?.[lang];
  58. let fallback = 0;
  59. for ( let n = 0; n < keys.length; n++ ) {
  60. if ( text ) {
  61. text = text?.[keys[n]];
  62. if ( typeof text === 'string' ) text = text.trim();
  63. }
  64. if ( !text ) {
  65. if ( fallback < this.fallback.length ) {
  66. lang = this.fallback[fallback];
  67. fallback++;
  68. text = i18n?.[lang];
  69. n = -1;
  70. }
  71. else {
  72. n = keys.length;
  73. }
  74. }
  75. }
  76. if ( typeof text === 'string' ) {
  77. args.forEach( (arg, i) => {
  78. text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  79. } );
  80. if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  81. return gender(type, cases.split(/\s*\|\s*/));
  82. } );
  83. if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  84. return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  85. } );
  86. }
  87. return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  88. }
  89. // /**
  90. // * Get a localized message.
  91. // * @param {String} message - Name of the message.
  92. // * @param {String[]} args - Arguments for the message.
  93. // * @returns {String}
  94. // */
  95. // get(message = '', ...args) {
  96. // if ( this.namespace.length ) message = this.namespace + '.' + message;
  97. // let lang = this.lang;
  98. // let text = i18n?.[lang]?.[message];
  99. // let fallback = 0;
  100. // while ( !text ) {
  101. // if ( fallback < this.fallback.length ) {
  102. // lang = this.fallback[fallback];
  103. // fallback++;
  104. // text = i18n?.[lang]?.[message];
  105. // }
  106. // else break;
  107. // }
  108. // if ( typeof text === 'string' ) {
  109. // args.forEach( (arg, i) => {
  110. // text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
  111. // } );
  112. // if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
  113. // return gender(type, cases.split(/\s*\|\s*/));
  114. // } );
  115. // if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
  116. // return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
  117. // } );
  118. // }
  119. // return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
  120. // }
  121. /**
  122. * Get names for all languages.
  123. * @param {Boolean} isRcGcDw - Get the languages for RcGcDw?
  124. * @static
  125. */
  126. static allLangs(isRcGcDw = false) {
  127. if ( isRcGcDw ) return i18n.RcGcDw;
  128. return i18n.allLangs;
  129. }
  130. }
  131. /**
  132. * Parse gender text.
  133. * @param {String} gender - The gender.
  134. * @param {String[]} args - The possible text.
  135. * @returns {String}
  136. */
  137. function gender(gender, args) {
  138. var text = args[0];
  139. switch ( gender ) {
  140. case 'male':
  141. if ( args.length > 0 ) text = args[0];
  142. break;
  143. case 'female':
  144. if ( args.length > 1 ) text = args[1];
  145. break;
  146. case 'unknown':
  147. default:
  148. if ( args.length > 2 ) text = args[2];
  149. }
  150. return text;
  151. }
  152. /**
  153. * Parse plural text.
  154. * @param {String} lang - The language code.
  155. * @param {Number} number - The amount.
  156. * @param {String[]} args - The possible text.
  157. * @returns {String}
  158. */
  159. function plural(lang, number, args) {
  160. // https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
  161. var text = args[args.length - 1];
  162. switch ( lang ) {
  163. case 'fr':
  164. case 'hi':
  165. if ( number <= 1 ) text = getArg(args, 0);
  166. else text = getArg(args, 1);
  167. break;
  168. case 'pl':
  169. if ( number === 1 ) text = getArg(args, 0);
  170. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  171. text = getArg(args, 1);
  172. }
  173. else text = getArg(args, 2);
  174. break;
  175. case 'ru':
  176. case 'uk':
  177. if ( args.length > 2 ) {
  178. if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
  179. else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
  180. text = getArg(args, 1);
  181. }
  182. else text = getArg(args, 2);
  183. }
  184. else {
  185. if ( number === 1 ) text = getArg(args, 0);
  186. else text = getArg(args, 1);
  187. }
  188. break;
  189. case 'bn':
  190. case 'de':
  191. case 'en':
  192. case 'es':
  193. case 'it':
  194. case 'ja':
  195. case 'ko':
  196. case 'nl':
  197. case 'pt-br':
  198. case 'th':
  199. case 'sv':
  200. case 'tr':
  201. case 'vi':
  202. case 'zh-hans':
  203. case 'zh-hant':
  204. default:
  205. if ( number === 1 ) text = getArg(args, 0);
  206. else text = getArg(args, 1);
  207. }
  208. return text;
  209. }
  210. /**
  211. * Get text option.
  212. * @param {String[]} args - The list of options.
  213. * @param {Number} index - The preferred option.
  214. * @returns {String}
  215. */
  216. function getArg(args, index) {
  217. return ( args.length > index ? args[index] : args[args.length - 1] );
  218. }
  219. module.exports = Lang;