<p>Completely, 100% untested, but I think you'd be able to pull it off with a pretty simple <a href="https://pluginfactory.io/" rel="nofollow">plugin</a> that added a new <a href="http://twig.sensiolabs.org/doc/filters/index.html" rel="nofollow">Twig filter</a> (check the Twig Extensions switch).</p>

<p>From a template you'd something like:</p>

<pre><code>{{ myDate|myDateFilter('es') }}
</code></pre>

<p>Then your plugin's <code>myDateFilter</code> method would do something like:</p>

<pre><code>public function myDateFilter(\Twig_Environment $env, $date, $locale)
{
    $localeData = craft()-&gt;i18n-&gt;getLocaleData($locale);
    $dateFormatter = $localeData-&gt;getDateFormatter();

    return $dateFormatter-&gt;formatDateTime($date-&gt;getTimestamp());
}
</code></pre>

CC BY-SA 3.0
2016-09-02T12:23:07.273Z
Brilliant - thanks so much.

In the end I used the dateFormatter's format() function instead of formatDateTime(), but otherwise your untested code was bang on
mijewe
CC BY-SA 3.0
2016-09-02T02:40:10.477Z
2016-09-02T02:40:10.477Z
Brad Bell
<p>I've got a generic newsletter template. Now some of our newsletters are in Spanish. It's overkill and more complicated for the admins to bother with settings up different locales, so instead I'd like to set the locale from the entry (currently using a dropdown).</p>

<p>All good, and I can use the |translate(language="") filter to achieve this using the value from the dropdown field.</p>

<p>Now the problem is that when formatting a date, it remains in English because it translated using the current locale, which is English.</p>

<p>I'm wondering whether I can manually override the locale in the template. <code>{% set locale = 'es' %}</code>
or something.</p>

<p>Or if I can force the date to format using a specific locale.</p>

<p>Cheers.</p>

CC BY-SA 3.0
2016-09-01T03:45:14.930Z
Just to be clear, you're trying to avoid adding a new locale under Settings->Locales in the control panel?
Brad Bell
CC BY-SA 3.0
2016-09-01T08:41:30.883Z
That's right. Newsletters are already linked to a category which is intrinsically linked to a language, so having to use locales properly would be confusing and doubling up on effort for the admin.
mijewe
<localization><datetime><language>
Is it possible to use set the locale manually in a template?
# Is it possible to use set the locale manually in a template?

(Asked by: username_0 on 2016-08-31)

I've got a generic newsletter template. Now some of our newsletters are in Spanish. It's overkill and more complicated for the admins to bother with settings up different locales, so instead I'd like to set the locale from the entry (currently using a dropdown).

All good, and I can use the |translate(language="") filter to achieve this using the value from the dropdown field.

Now the problem is that when formatting a date, it remains in English because it translated using the current locale, which is English.

I'm wondering whether I can manually override the locale in the template. `{% set locale = 'es' %}`
or something.

Or if I can force the date to format using a specific locale.

Cheers.

**Tags:** localization, datetime, language

> **Comments:**
> **username_1**: Just to be clear, you're trying to avoid adding a new locale under Settings->Locales in the control panel?  
> **username_0**: That's right. Newsletters are already linked to a category which is intrinsically linked to a language, so having to use locales properly would be confusing and doubling up on effort for the admin.  

---

## 1 answers

### Answer by username_1 on 2016-09-02. Score: 3 | Accepted answer

Completely, 100% untested, but I think you'd be able to pull it off with a pretty simple plugin that added a new Twig filter (check the Twig Extensions switch).
From a template you'd something like:
{{ myDate|myDateFilter('es') }}

Then your plugin's myDateFilter method would do something like:
public function myDateFilter(\Twig_Environment $env, $date, $locale)
{
    $localeData = craft()->i18n->getLocaleData($locale);
    $dateFormatter = $localeData->getDateFormatter();

    return $dateFormatter->formatDateTime($date->getTimestamp());
}

> **Comments:**
> **username_0**: Brilliant - thanks so much.  In the end I used the dateFormatter's format() function instead of formatDateTime(), but otherwise your untested code was bang on  

---

