i18n.js 7.6 KB

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