berk / tr8n

This repository has moved to:
https://github.com/tr8n/tr8n
MIT License
280 stars 37 forks source link

Allow using tr() in background jobs (e.g. delayed_job) #39

Closed hinrik closed 12 years ago

hinrik commented 12 years ago

E.g. when sending emails.

AfzalMasood11 commented 10 years ago

Hi

@hinrik @berk I send emails through background jobs using resque gem. In email's template I am using tr() for localization. Problem is, email does not translate. User will get all the text in English. Even it does not add phrases of email template.

Thanks in advance.

berk commented 10 years ago

Are you setting current_language in Tr8n::Config based on the user preference. Since you are doing it in the background, you need to manually set it before the email is generated.

If the email is sent out in default language (English), it won’t register phrases - for performance reasons.

On Jun 27, 2014, at 5:54 AM, Afzal Masood notifications@github.com wrote:

Hi

@hinrik @berk I send emails through background jobs using resque gem. In email's template I am using tr() for localization. Problem is, email does not translate. User will get all the text in English. Even it does not add phrases of email template.

Thanks in advance.

— Reply to this email directly or view it on GitHub.

AfzalMasood11 commented 10 years ago

@berk I am very Thankful for your response. Yes, I am setting current_language in Tr8n::Config based on the user preference. If I check Tr8n::Config.current_language.locale in self.perform method of "resque" then it gives me "en-US"

Now I am not sure how to change language, If I set language like following in resque's self.perform method

Tr8n::Config.current_language.locale = "de" or like this Thread.current[:tr8n_current_language] = Tr8n::Language.for("de")

then resque does not deliver email and show following error at resque admin view: undefined method translation_key_for_key' for nil:NilClass at tr8n-973fb5277bfe/app/models/tr8n/translation_key.rb:79:infind_or_create'

Second, If I set Tr8n::Config.current_language.locale = "de" in mailer method then it sends email again in English and do not add phrases of emails content.

Third, If I use tr('Hello', "Greetings", {:locale => "de"}) then it also does nothing.