Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var locale = request.params.locale;
var phrase = request.params.phrase;
var result;
if( request.query.plural ) {
var singular = phrase;
var plural = request.query.plural;
// Make sure the information is added to the catalog if it doesn't exist yet.
var translated = i18n.__n( {
singular : singular,
plural : plural,
count : request.query.count,
locale : locale
} );
// Retrieve the translation object from the catalog and return it.
var catalog = i18n.getCatalog( locale );
result = singular.split( configuration.objectNotation ).reduce( function( object, index ) {
return object[ index ];
}, catalog );
} else {
result = i18n.__( { phrase : phrase, locale : locale } );
}
response.send( result );
}
};
router.get('/', (req, res) => {
// Send the list of selectable locales
res.render('lang', {
languages: Object.keys(i18n.getCatalog())
});
})
.get('/:lang', (req, res) => {
const appLanguage = appLocale.substr(0, 2);
let resultLocale = 'en';
// Looking for locale match
const fullMatch = Object.keys(i18n.getCatalog())
.some(key => {
const match = key.replace(/-/g, '_').toLowerCase() === appLocale.replace(/-/g, '_').toLowerCase();
if (match) {
resultLocale = key;
}
return match;
});
// Looking for language match
if (!fullMatch) {
Object.keys(i18n.getCatalog())
.some(key => {
const match = key.toLowerCase() === appLanguage.toLowerCase();
if (match) {
resultLocale = key;
}
return match;
});
}
i18n.setLocale(resultLocale);
};
app.use('*', function(req, res, next) {
var catalogs = i18n.getCatalog();
app.locals.lang = catalogs[req.locale];
next();
});
}
module.exports = function (req, res, next) {
const language = req.chargeData.language || 'en'
i18n.setLocale(req, language)
res.locals.translationStrings = JSON.stringify(i18n.getCatalog(language))
res.locals.language = language
next()
}
module.exports.command = (message) => {
if (message.input && Object.keys(i18n.getCatalog()).includes(message.input)) {
r.table('i18n')
.insert({
id: message.inbox,
lang: message.input
}, {
conflict: 'update'
})
.run(r.conn, (err) => {
if (err) {
message.channel.createMessage(message.__('err_generic'));
} else {
message.setLocale(message.input);
message.channel.createMessage(message.__('locale_set', { locale: message.input }));
}
});
} else {
router.get('/', (req, res) => {
res.render('lang.pug', {
title: 'Languages',
languages: Object.keys(i18n.getCatalog())
});
})
.get('/:lang', (req, res) => {
command: async (message) => {
if (message.mss.input && Object.keys(i18n.getCatalog()).includes(message.mss.input)) {
await r.table('i18n')
.insert({
id: message.author.id,
lang: message.mss.input
}, {
conflict: 'update'
});
message.setLocale(message.mss.input);
message.channel.createMessage(message.__('locale_set', { locale: message.__(`lang_${message.mss.input.replace(/-/g, '_')}`) }));
} else {
message.channel.createMessage(`${message.__('locale_incorrect')}\n${Object.keys(i18n.getCatalog()).map(lang => `\`${lang}\` - ${message.__(`lang_${lang.replace(/-/g, '_')}`)}`).join('\n')}`);
}
}
}, {
function getCatalog(locale) {
locale = normalizeLanguage(locale);
return i18n.getCatalog(locale);
}
command: (message) => {
if (message.mss.input && Object.keys(i18n.getCatalog()).includes(message.mss.input)) {
r.table('i18n')
.insert({
id: message.author.id,
lang: message.mss.input
}, {
conflict: 'update'
})
.run();
message.setLocale(message.mss.input);
message.channel.createMessage(message.__('locale_set', { locale: message.__(`lang_${message.mss.input}`) }));
} else {
message.channel.createMessage(`${message.__('locale_incorrect')}\n${Object.keys(i18n.getCatalog()).map(lang => `\`${lang}\` - ${message.__(`lang_${lang}`)}`).join('\n')}`);
}
}
}, {