Jump to content

Translations in site config


teppo
 Share

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

 

Link to comment
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 ?

Link to comment
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
Link to comment
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.

Link to comment
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
Link to comment
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
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...