i18n.js 7.4 KB

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