How to specify a language for a XenForo_Phrase

AndyB

Well-known member
I have an add-on called Weekly Digest:

https://xenforo.com/community/resources/weekly-digest.3777/

It runs from a XenForo Cron once a week. Everything is fine, except for those forums which run other languages besides English.

What I would like to know is in the PHP which the Cron runs, I have several phrases, for example:

PHP:
// get subject
$subject = new XenForo_Phrase('weeklydigest_subject');

is there a way I can specify which language to select for this phrase? I know I could do a direct query on the database, but it would be nice if there was an easier way.

Thank you.
 
You don't need to specify a language for the phrase.

As Mike already described, you need to pass the user's language into the Mail object.
 
Hi Chris,

Thank you for looking at my question.

Lets say for a moment that I'm not sending an email at all. Is there a way to specify specify which language a phrase would use? Of course outside a Cron this is automatic, but run from a Cron one needs to specify or else it defaults to the master phrase.
 
The only possible thing you would ever need to do in a Cron is send an email or something similar. So it's a rather moot point, but you could in theory set it using the setLanguageId method of the phrase object before the phrase is rendered. Regardless, it still won't help you with your particular issue.
 
Thank you, Chris.

This works perfect for my needs.

PHP:
XenForo_Phrase::setLanguageId($languageId)
 
In case anyone else stumbles across this thread, I will just quote the correct solution:

That just looks like it's sending the English (or master) language version. It's not really some phrases -- it's the whole thing.

As a developer, you control the language that is used to send the mail. It defaults to the language in use by the current page. If you're sending mails to a particular user, you should be setting the mail object to their language.

You don't need to specify a language for the phrase.

As Mike already described, you need to pass the user's language into the Mail object.
 
Thank you, Chris.

This works perfect for my needs.

PHP:
XenForo_Phrase::setLanguageId($languageId)
This may work if all users use the same language but will not work correctly if you need to change languages between users though FYI.
 
Back
Top Bottom