<?php
/**
 * @file
 * User mail translation module.
 */

/**
 * Implements hook_mail_alter().
 */
function i18n_user_mail_alter(&$message) {
  if ($message['module'] == 'user') {
    $message['language'] = language_default();
    if (isset($message['params']['account'])) {
      $user_preferred = user_preferred_language($message['params']['account']);
      if (isset($user_preferred)) {
        $message['language'] = $user_preferred;
      }
    }
    $language = (isset($message['language']) ? $message['language'] : language_default());
    $variables = array('user' => $message['params']['account']);
    $key = $message['key'];

    $components = array('subject', 'body');
    foreach ($components as $component) {
      $text = i18n_variable_get('user_mail_' . $key . '_' . $component, $language->language, FALSE);
      if ($text) {
        $text = token_replace($text, $variables, array('language' => $language, 'callback' => 'i18n_user_user_mail_tokens', 'sanitize' => FALSE));

        switch ($component) {
          case 'subject':
            $message[$component] = $text;
            break;

          case 'body':
            $message[$component] = array($text);
            break;
        }
      }
    }
  }
}

/**
 * Overrides user_mail_tokens().
 *
 * @see i18n_user_user_tokens_alter()
 * @see user_mail_tokens()
 */
function i18n_user_user_mail_tokens(&$replacements, $data, $options) {
  if (isset($data['user'])) {
    $replacements['[user:one-time-login-url]'] = i18n_user_user_pass_reset_url($data['user']);
    $replacements['[user:cancel-url]'] = i18n_user_user_cancel_url($data['user']);
  }
}

/**
 * Overrides user_pass_reset_url().
 * Generates a unique and localized URL for a user to login and reset their password.
 *
 * @see user_pass_reset_url().
 */
function i18n_user_user_pass_reset_url($account) {
  $timestamp = REQUEST_TIME;
  return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array('absolute' => TRUE, 'language' => i18n_language($account->language)));
}

/**
 * Overrides user_pass_cancel_url().
 * Generates a localized URL to confirm an account cancellation request.
 *
 * @see i18n_user_user_cancel_url()
 */
function i18n_user_user_cancel_url($account) {
  $timestamp = REQUEST_TIME;
  return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array('absolute' => TRUE, 'language' => i18n_language($account->language)));
}
