The bug description:
account_activated email language depends on website language - not registered user preferred language (as expected).

so, if user registers on russian version of the website, and his preferred language is set to russian,
but admin uses english version of the website to approve the account, the approved user will get email in English.

Steps to reproduce:
1) Install drupal, set up registration mode to "user can register; approval required", enable english and russian languages for interface
2) enable i18n_variable module, activate translation for account-related emails
3) translate user_activated email body and subject to russian
4) open website as anonymous, using russian language, create new account
5) open website as admin, using english language, approve new user

result: new user will get email saying that his account was activated. in English!
(though email tokens will be generated for Russian language)

the research: the bug seems to be located in _user_mail_text function.


function _user_mail_text($key, $language = NULL, $variables = array(), $replace = TRUE) {
  $langcode = isset($language) ? $language->language : NULL;

  if ($admin_setting = variable_get('user_mail_' . $key, FALSE)) {
    // An admin setting overrides the default string.
    $text = $admin_setting;
  }
  else {
...
}

if ($replace) {
    // We do not sanitize the token replacement, since the output of this
    // replacement is intended for an e-mail message, not a web browser.
    return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  }

  return $text;
}

so, variable_get('user_mail_' . $key, FALSE) will return English version of variable since admin uses website in English, while
token_replace() gets $language (which is the correct language of registered user)

Comments

Rechi’s picture

I can confirm that issue.
I'm not a developer but maybe this could be a start:

...
if ($admin_setting = variable_get('user_mail_' . $key, FALSE)) {
    // An admin setting overrides the default string.
   
   // edit: If i18n_variable is active, use its function to get translation  
   if(module_exists('i18n_variable')){
        if($admin_setting = i18n_variable_get('user_mail_' .$key,$langcode,False)){
             $text = $admin_setting;
        }
    }
      
    $text = $admin_setting;
  }
  else {
...
Rechi’s picture

Sorry that breaks the config form.
This could work:

if ($admin_setting = variable_get('user_mail_' . $key, FALSE)) {
    // An admin setting overrides the default string.
         
    // edit: If i18n_variable is active, use its function to get translation
    if(module_exists('i18n_variable')){
        if($admin_setting_i18n = i18n_variable_get('user_mail_' .$key,$langcode,False)){
             $admin_setting = $admin_setting_i18n;
        }
    }
      
    $text = $admin_setting;
claudio.rossini@rarolab.it’s picture

it works! thanks!

Rechi’s picture

Don't hack the core.
Use: mail_edit instead.
Just found that module a few days ago. Sorry for the core hacking.

truyenle’s picture

mail_edit does work it the link inside email still not aware of language. We need to have multilink module http://drupal.org/project/multilink to have these links arrive in the right languages.

THanks

qwckbrwnfx’s picture

subscribe

qwckbrwnfx’s picture

Issue summary: View changes

better clarifications

PascalAnimateur’s picture

Here's a patch based on #2 that fixes the problem for Drupal 7.39 using i18n_variable.

tomyinhauser’s picture

Why not translating the result from variable_get with t(), just as it does when no overrides were made on the default Drupal configuration?

  // An admin setting overrides the default string.
  $text = t($admin_setting, array(), array('langcode' => $langcode));
rakesh.gectcr’s picture

rakesh.gectcr’s picture

Status: Active » Needs review
PascalAnimateur’s picture

Status: Needs review » Needs work

The point of using i18n_variable is being able to override these strings per-language. So there's no need to return the translated text value via t() (which should never be used to translate user-entered strings, see https://groups.drupal.org/node/1827 for an explanation).
Simply check the variables you'd like to translate in /admin/config/regional/i18n/variable and the form for these settings should have language tabs to configure the value per-language.

rakesh.gectcr’s picture

Status: Needs work » Needs review
StatusFileSize
new751 bytes

That really making sense. I am reverting back the patch

tomyinhauser’s picture

Thanks for the clarification, PascalAnimateur!
It is clear that t() has its limitations, and that i18n has proved to be a better alternative; it wouldn't be in D8 core otherwise. But using a contrib module inside D7 core? That sounds a little bit strange to me.

PascalAnimateur’s picture

It only uses i18n_variable if it's enabled (see the module_exists call in the if statement). Don't want to sound pessimistic, but the patch probably won't be committed to 7.x core..

Version: 7.14 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

poker10’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#1197400: Use user's preferred language in system emails

Thanks for reporting this.

I think this is a duplicate of #1197400: Use user's preferred language in system emails and as you can see there, it is replicable also without the i18n module. Lets keep the older issue open.