question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Translating strings without req/res object

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mashpiecommented, Jul 12, 2016

no worries 😃 I will add a syntax check in one of next releases

0reactions
axelfrancommented, Jul 11, 2016

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using i18next translations without having the "key" as strings
Well, you can parse your object to JSON using JSON.stringify(<your object>); and then pass it to i18next :) But this requires that your've...
Read more >
Translating strings with links · Issue #223 · ngx-translate/core
Fortunately, it's a pretty easy fix, as all it needs a little adapting to make rawResourceString an Observable instead of a string ....
Read more >
Converting JSON into Javascript Objects with JSON.parse()
JSON parsing is the process of converting a JSON object to Javascript ... JSON.parse() requires one parameter: the flat JSON string to be ......
Read more >
Parsing arguments and building values — Python 3.11.1 ...
Convert a Unicode object to a C pointer to a character string. ... Requires that the Python object is a bytes object, without...
Read more >
How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found