Translating strings without req/res object
See original GitHub issueHi,
I use i18n for translation on my backend web server, and I’m having some issues regarding translating strings that are not related to the user initiating the request. A typical use case is when user 1 (the one sending a request to the backend service) likes a photo, and I should send a localised push message to the publisher of the photo (user 2).
I’m storing all users’ preferred locale in my database (received from i18n.getLocale()), so I’ve ensured access to it. My problem seems to be that I don’t have access to i18n from my push controller, as the req/res objects are not available here.
My solution this far is as follows:
var i18n = require('i18n');
i18n.configure({
directory: __dirname + '/../locales'
});
function viewsAlert(username, title, views) {
userController.getLocale(null, username, function(err, locale) {
i18n.setLocale(locale);
var message = i18n.__({phrase: "PUSH_VIEWS", locale: "en"});
console.log(message);
However this doesn’t work, and log output is:
undefined
Do anyone have any suggestions for how I can use stored locales to send localised push messages with i18n?
Many thanks!
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
no worries 😃 I will add a syntax check in one of next releases
OMG, you are totally correct that there was an error in the JSON files. What a stupid mistake, but thanks a lot for helping out!