Answers.Body: <p>You can translate string in PHP and Twig via translate function</p>
  
  <pre><code>public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft')
  </code></pre>
  
  <h3>Twig</h3>
  
  <pre><code>{{ 'my string'|t() }}
  </code></pre>
  
  <h3>PHP</h3>
  
  <pre><code>Craft::t('my string');
  </code></pre>
  
  <p>So all you need to do is to wrap every string in your templates within the translate filter and pass the preferred local as an argument. For example </p>
  
  <pre><code>{{ 'my latest news'|t([], null, currentUser.locale) }}
  </code></pre>
  
  <p>If no parameter is set, Craft will use <code>craft()-&gt;getLanguage()</code> by default so you can do the following as well</p>
  
  <pre class="lang-PHP prettyprint-override"><code>$tmpLocale = craft()-&gt;getLanguage();
  $user = craft()-&gt;userSession-&gt;getUser();
  craft()-&gt;setLanguage($user-&gt;locale);
  $content = craft()-&gt;templates-&gt;render(
     '_emails/notification',
     [
         'userInfo' =&gt; $this-&gt;generateUserInfo($newUser),
     ]
  ); 
  $craft-&gt;setLanguage($tmpLocale);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-29T14:02:05.803Z
Answers.Text: damn not sure how, but I obviously overlooked the get/set language on the general craft object. Think I'll go with that approach as it's quite a bit of templates to modify otherwhise. Thanks!
Answers.UserDisplayName: mrsmn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-29T13:40:33.073Z
Answers.LastActivityDate: 2018-08-29T13:40:33.073Z
Answers.UserDisplayName: Robin Schambach
Body: <h2>Usecase:</h2>
  
  <p>I'm sending out custom emails based on certain events to my users. These users have chosen a certain prefered locale in their profile settings and I'd like for those emails to be in that locale instead of in the locale of the control panel at the time of triggering the events. </p>
  
  <p>So what I'm looking for is a way to set the relevant locale before rendering the email template with twig but I cant seem to find a way. </p>
  
  <hr>
  
  <h2>Relevant code</h2>
  
  <pre><code>$mailer = craft()-&gt;email;
  $mailToBeSent = new EmailModel();
  
  craft()-&gt;templates-&gt;setTemplatesPath(CRAFT_TEMPLATES_PATH);
  $mailToBeSent-&gt;toEmail = 'some.email@domain.com';
  
  $mailToBeSent-&gt;subject = $subject;
  
  $content = craft()-&gt;templates-&gt;render(
     '_emails/notification',
      [
          'userInfo' =&gt; $this-&gt;generateUserInfo($newUser),
      ]
  
  );
  
  $mailToBeSent-&gt;htmlBody = $content;
  $mailer-&gt;sendEmail($mailToBeSent);
  </code></pre>
  
  <hr>
  
  <p>Let me know more information is needed or if something is unclear.</p>
  
Tags: <localization><emails><craft2>
Title: Sending a custom email in the users prefered locale
ThreadText: # Sending a custom email in the users prefered locale
  
  (Asked by: username_0 on 2018-08-29)
  
  ## Usecase:
  
  I'm sending out custom emails based on certain events to my users. These users have chosen a certain prefered locale in their profile settings and I'd like for those emails to be in that locale instead of in the locale of the control panel at the time of triggering the events.
  
  So what I'm looking for is a way to set the relevant locale before rendering the email template with twig but I cant seem to find a way.
  
  ---
  
  ## Relevant code
  
  ```
  $mailer = craft()->email;
  $mailToBeSent = new EmailModel();
  
  craft()->templates->setTemplatesPath(CRAFT_TEMPLATES_PATH);
  $mailToBeSent->toEmail = 'some.email@domain.com';
  
  $mailToBeSent->subject = $subject;
  
  $content = craft()->templates->render(
     '_emails/notification',
      [
          'userInfo' => $this->generateUserInfo($newUser),
      ]
  
  );
  
  $mailToBeSent->htmlBody = $content;
  $mailer->sendEmail($mailToBeSent);
  
  ```
  
  ---
  
  Let me know more information is needed or if something is unclear.
  
  **Tags:** localization, emails, craft2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-29. Score: 4 | Accepted answer
  
  You can translate string in PHP and Twig via translate function
  
  ```
  public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft')
  
  ```
  
  ### Twig
  
  ```
  {{ 'my string'|t() }}
  
  ```
  
  ### PHP
  
  ```
  Craft::t('my string');
  
  ```
  
  So all you need to do is to wrap every string in your templates within the translate filter and pass the preferred local as an argument. For example
  
  ```
  {{ 'my latest news'|t([], null, currentUser.locale) }}
  
  ```
  
  If no parameter is set, Craft will use `craft()->getLanguage()` by default so you can do the following as well
  
  ```PHP
  $tmpLocale = craft()->getLanguage();
  $user = craft()->userSession->getUser();
  craft()->setLanguage($user->locale);
  $content = craft()->templates->render(
     '_emails/notification',
     [
         'userInfo' => $this->generateUserInfo($newUser),
     ]
  ); 
  $craft->setLanguage($tmpLocale);
  
  ```
  
  > **Comments:**
  > **username_0**: damn not sure how, but I obviously overlooked the get/set language on the general craft object. Think I'll go with that approach as it's quite a bit of templates to modify otherwhise. Thanks!  
  
  ---
  
  