1

For a Single section's entry type that has the title field hidden, I'd like to translate the generated title, as per the entry's locale.

I've currently got the following in the entry type's "Title Format" field:

{section.name|t(language=locale)}

However, this doesn't translate the section.name as per the entry's locale, it appears to use the current CP locale instead (i.e., if the CP is currently rendered in English, it'll use the English translation for entries in all locales). However, if I use something like

{section.name|t(language=locale)} ({locale})

the latter locale value (inside the parenthesis) does reflect the entry's locale (it'll print something like Section Name (no) for the Norwegian version. So that's a bit odd.

Also, I've tested with a hard coded string for the t filter's language parameter (i.e. {section.name|t(language="no")}), which works as expected.

Not sure what's going on – though I'm guessing that the locale variable passed to the translation filter is somehow null (in which case Craft will use the current locale). How can I translate an entry type's Title Format based on the entry's locale?

2

Figured out something that works, though I can't explain why:

{{ object.section.name|t(language=object.locale) }}

Update

Here's how to do it for Craft 3:

{{ object.section.name|t('site', language=object.site.language) }}
  • I think the issue with the short syntax is that you can only use the variable directly at the beginning, so {locale} works and {'foo' ~ locale} does not. If I understood correctly, short syntax parsing got improved for Craft 3, although I still prefer regular Twig. – carlcs Mar 14 '18 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.