@grammyjs/fluent
Version:
Fluent localization system integration for grammY Telegram bot framework
33 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFluent = void 0;
const locale_negotiator_1 = require("./locale-negotiator");
const fallbackLocale = 'en';
function useFluent(options) {
const { fluent, defaultLocale = fallbackLocale, localeNegotiator = locale_negotiator_1.defaultLocaleNegotiator, } = options;
return async function fluentMiddleware(context, next) {
let translate;
const translateWrapper = ((messageId, context) => translate(messageId, context));
Object.assign(context, {
fluent: {
instance: fluent,
renegotiateLocale: negotiateLocale,
useLocale,
},
translate: translateWrapper,
t: translateWrapper,
});
await negotiateLocale();
await next();
async function negotiateLocale() {
const locale = (await (localeNegotiator === null || localeNegotiator === void 0 ? void 0 : localeNegotiator(context)) ||
defaultLocale);
useLocale(locale);
}
function useLocale(locale) {
translate = fluent.withLocale(locale);
}
};
}
exports.useFluent = useFluent;
//# sourceMappingURL=middleware.js.map