Jump to content
teppo

Translations in site config

Recommended Posts

I've got a feeling that this is either something that by design shouldn't work, or it's something that has a really simple solution and soon I'll have a major facepalm moment: can we define translatable text in /site/config.php in PW3 – and if so, how? 😄

So far I'm not having much luck. Calling __('Translatable') produces fatal error, and so does $config->__('Translatable'). (Config extends Wire, so figured latter could've worked.)

Based on the errors it seems that this might be an impossible situation: in order to get to the translation functions I'd first have to have a ProcessWire instance running, and I don't have one running before config has been parsed.

Any ideas, or am I missing something important here?

Here's that fatal error, just for reference:

Fatal error: Uncaught Error: Call to a member function wire() on null in /wire/core/Functions.php:25

Stack trace:

#0 /wire/core/LanguageFunctions.php(30): ProcessWire\wire('languages')
#1 /wire/core/Wire.php(1516): ProcessWire\__('Translatable', Object(ProcessWire\Config)) 
#2 /site/config.php(90): ProcessWire\Wire->_('Translatable')
#3 /wire/core/ProcessWire.php(923): require('/var/www/sites/...')
#4 /index.php(33): ProcessWire\ProcessWire::buildConfig('/var/www/sites/...')
#5 {main} thrown in /wire/core/Functions.php on line 25

 

Share this post


Link to post
Share on other sites

For the time being I've resolved this by moving config settings with translatable strings to a separate file, which I'm then including later in the process after ProcessWire has been properly set up. If anyone has a better idea, please let me know 🙂

Share this post


Link to post
Share on other sites

Perhaps using something like $str = function { return if (function_exists('__')) { return __('key'); } ), or something along these lines? Just a quick idea from mobile 🙂

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, teppo said:

For the time being I've resolved this by moving config settings with translatable strings to a separate file, which I'm then including later in the process after ProcessWire has been properly set up. If anyone has a better idea, please let me know 🙂

Without testing anything in this regard, in my understanding, when config.php is called is to early. Never thought this could / should work. In init.php or ready.php I assume it will work.

Share this post


Link to post
Share on other sites
4 hours ago, teppo said:

For the time being I've resolved this by moving config settings with translatable strings to a separate file, which I'm then including later in the process after ProcessWire has been properly set up. If anyone has a better idea, please let me know 🙂

Don't see anything bad with that. What strings do you want to be translatable? Never needed translatable strings in config 🙂 Would ready.php be too late? Or a separate module's init()? You could also set $config->... = $this->_(...); there

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, bernhard said:

What strings do you want to be translatable?

These were for MarkupMenu, an aria-label within template strings that I wanted to be translatable 😅

Not sure if I've ever tried this before either; didn't occur to me that it wouldn't be possible, but makes sense now. Anyway, the separate config file idea actually works quite nicely for me in this case. Ready.php would've been a good solution as well 👌

  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...