I have the following function:
//translate strings
function translate(lng, str)
{
var translation = '';
csv().from(__dirname + '/../application/_common/lng/' + lng + '.csv', {delimiter: ';'})
.transform( function(row, index) {
if(row[0] == str) {
return row[1];
}
})
.on('data', function(data) {
translation = data;
})
.on('end', function() { return translation; });
}
and I want to get the value found in the csv into a variable like this:
var translation = translate(‘en’, ‘translate_me’);
The problem is that the function does not return anything!
Well, you don’t return anything, you only return things from inner functions, but the outer function doesn’t. If CSV is synchonous, do this:
If CSV is _asynchronous, it gets a little bit harder. Do this:
Now you would call it like this: