i18n.js 6.0 KB

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