123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- const {defaultSettings} = require('../util/default.json');
- const {escapeText} = require('./util.js');
- const i18n = {
- en: require('./i18n/en.json'),
- bn: require('./i18n/bn.json'),
- de: require('./i18n/de.json'),
- es: require('./i18n/es.json'),
- fr: require('./i18n/fr.json'),
- hi: require('./i18n/hi.json'),
- it: require('./i18n/it.json'),
- ja: require('./i18n/ja.json'),
- ko: require('./i18n/ko.json'),
- nl: require('./i18n/nl.json'),
- pl: require('./i18n/pl.json'),
- pt: require('./i18n/pt-br.json'),
- 'pt-br': require('./i18n/pt-br.json'),
- ru: require('./i18n/ru.json'),
- th: require('./i18n/th.json'),
- tr: require('./i18n/tr.json'),
- zh: require('./i18n/zh-hans.json'),
- 'zh-hans': require('./i18n/zh-hans.json'),
- 'zh-hant': require('./i18n/zh-hant.json')
- };
- /**
- * A language.
- * @class
- */
- class Lang {
- /**
- * Creates a new language.
- * @param {String} [lang] - The language code.
- * @param {String} [namespace] - The namespace for the language.
- * @constructs Lang
- */
- constructor(lang = defaultSettings.lang, namespace = '') {
- if ( !( typeof lang === 'string' && lang in i18n ) ) lang = defaultSettings.lang;
- this.lang = lang;
- this.namespace = namespace;
- this.fallback = ( i18n?.[lang]?.fallback.slice() || [defaultSettings.lang] ).filter( fb => fb.trim() );
- }
- /**
- * Get a localized message.
- * @param {String} message - Name of the message.
- * @param {Boolean} escaped - If the message should be HTML escaped.
- * @param {(String|import('cheerio'))[]} args - Arguments for the message.
- * @returns {String}
- */
- get(message = '', escaped = false, ...args) {
- if ( this.namespace.length ) message = this.namespace + '.' + message;
- let keys = ( message.length ? message.split('.') : [] );
- let lang = this.lang;
- let text = i18n?.[lang];
- let fallback = 0;
- for (let n = 0; n < keys.length; n++) {
- if ( text ) {
- text = text?.[keys[n]];
- if ( typeof text === 'string' ) text = text.trim()
- }
- if ( !text ) {
- if ( fallback < this.fallback.length ) {
- lang = this.fallback[fallback];
- fallback++;
- text = i18n?.[lang];
- n = -1;
- }
- else {
- n = keys.length;
- }
- }
- }
- if ( typeof text === 'string' ) {
- if ( escaped ) text = escapeText(text);
- args.forEach( (arg, i) => {
- if ( escaped && typeof arg !== 'string' ) {
- text = text.replaceSave( new RegExp( `\\[([^\\]]+)\\]\\(\\$${i + 1}\\)`, 'g' ), (m, linkText) => {
- return arg.html(linkText);
- } );
- }
- text = text.replaceSave( new RegExp( `\\$${i + 1}`, 'g' ), arg );
- } );
- if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
- return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
- } );
- }
- return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
- }
- }
- /**
- * Parse plural text.
- * @param {String} lang - The language code.
- * @param {Number} number - The amount.
- * @param {String[]} args - The possible text.
- * @returns {String}
- */
- function plural(lang, number, args) {
- // https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
- var text = args[args.length - 1];
- switch ( lang ) {
- case 'fr':
- case 'hi':
- if ( number <= 1 ) text = getArg(args, 0);
- else text = getArg(args, 1);
- break;
- case 'pl':
- if ( number === 1 ) text = getArg(args, 0);
- else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
- text = getArg(args, 1);
- }
- else text = getArg(args, 2);
- break;
- case 'ru':
- if ( args.length > 2 ) {
- if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
- else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
- text = getArg(args, 1);
- }
- else text = getArg(args, 2);
- }
- else {
- if ( number === 1 ) text = getArg(args, 0);
- else text = getArg(args, 1);
- }
- break;
- case 'bn':
- case 'de':
- case 'en':
- case 'es':
- case 'ja':
- case 'nl':
- case 'pt-br':
- case 'th':
- case 'tr':
- case 'ja':
- case 'ko':
- case 'zh-hans':
- case 'zh-hant':
- default:
- if ( number === 1 ) text = getArg(args, 0);
- else text = getArg(args, 1);
- }
- return text;
- }
- /**
- * Get text option.
- * @param {String[]} args - The list of options.
- * @param {Number} index - The preferred option.
- * @returns {String}
- */
- function getArg(args, index) {
- return ( args.length > index ? args[index] : args[args.length - 1] );
- }
- module.exports = Lang;
|