Jump to content

Translation and Twig


tpr
 Share

Recommended Posts

I have installed these modules to get Twig support on a multilanguage site:

  • Template Twig Replace
  • (Template Twig Replace Language Support)
  • ProcessLanguageTranslatorTwigSupport
Everything went fine until I tried to use "__" functions, eg.
{{ __('my_string') }}

The string showed up in the Language translator inside PW and I could translate it.

However, I couldn't display it on the site. What am I missing?

Ps. I was able to access these translations with plain PHP and with another templating system.

Link to comment
Share on other sites

  • 3 weeks later...

Hi tpr

I've just passed through the same problem.
 
The solution is to give the location/name of your twig file as the second parameter. Ex:

{{ __('my_string', 'home') }}

for templates/home.twig
 

or

{{ __('my_string', 'includes/header') }}

for templates/includes/header.twig

 

  • Like 2
Link to comment
Share on other sites

Thanks, I will check that next time. Now I'm using Latte and made a handy translation helper function that adds the file path (= textdomain in PW, as I learned) as the second parameter automatically (just like what you suggested). Doing so I can have all custom string translations in one file (_strings.php).

Link to comment
Share on other sites

I could get twig files into the admin translator but I couldn't echo them. I'm not sure whether I tried adding the path as the second parameter but probably not.

Unfortunately I can't check this now because I'm using another template engine.

What I do now is a simple function - optional context/textdomain get their default values in the function if not set:

t('String to translate', 'optional context', 'optional textdomain')

    

This works fine but because it doesn't contain "__" it won't appear in the admin translator.

It' a nuissance because I have to manually list them in my _strings.php file, but for me it's better than having to edit strings in several files.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...