When using system settings, locale setting just works perfectly. But if you have a multi context site, and you define locale setting per context, it is not overridden, even if general system setting is empty.
On a template, use the createdon date with an output modifier in order to output data with text, like 20th September 2019:
[[+publishedon:date=`%e %b %Y`]]
Then go to system settings and change locale to different language and you will see it applied properly.
If you have other contexts and edit/add locale setting into the contexts, and clear the general locale system setting, it won鈥檛 work. It just outputs in English.
tested with 3 different contexts:
If I set locale on the general settings, it is assumed and works, but never on the contexts directly.
Not cache related as cache was cleared every single time when changed context.
Tried to create a snippet to analyse the system behaviour:
$modx->log(modX::LOG_LEVEL_ERROR, '[phpstrftime - Custom Output modifier] current locale: ' . $modx->getOption('locale'),'','phpstrftime');
// it seems that Modx cannot get the current locale setting. Let's force it
//setlocale(LC_ALL, $modx->getOption('locale'));
$output = strftime($options, $input);
return $output;
When calling the output modifier like this:
[[+publishedon:phpstrftime=`%e %b %Y`:uppercase]]
there is a line registered on the system log that shows this:
[2019-09-29 11:11:53] (ERROR in phpstrftime @ D:\xampp\htdocs\core\cache\includes\elements\modsnippet\73.include.cache.php : 20) [strftime - Custom Output modifier] current locale: pt_PT.UTF8
So the locale setting is set on the system properly and coming from the Context, as the general system setting was left blank on purpose.
But the date is still delivering the string in English:
8 SEP 2019
As soon as I uncomment the setlocale on the snippet, everything works just fine.
Same behaviour is seen with date_timezone
As explained by @JoshuaLuckers on Modx community:
The function _initCulture is responsible for initialising the culture settings.
_initCulture is only called when MODX is initialised (link to source). By default it initialises using the web context.
When switching to another context using switchContext the _initCulture method isn鈥檛 being called.
Locale setting not taken into account when switching contexts having locale defined.
Context defined locale should be processes just like any other system setting when defined on the context.
MODX 2.7.2-pl
Apache 2.4
php 7.2 or 7.3
Xrouting
Babel
LangRouter and xRouting both call _initCulture for that reason.
If you use an own plugin for this, you have to add that code.
Hi @Jako,
As mentioned, I only use Xrouting. I created a small plugin just for test purposes as locale context setting was not working as expected.
Do you think this is Xrouting issue and not modx issue? I could notice your PR on xRouting related to _initCulture.
Maybe wrong event is set or missing event for xRouting?
Looking into the code above: you don't test, which locale is currently set. You only check the system/context setting. To log it right, you have to use:
$modx->log(modX::LOG_LEVEL_ERROR, '[phpstrftime - Custom Output modifier] current locale: ' . setlocale(LC_ALL, 0),'','phpstrftime');
Please check also, if setlocale(LC_ALL, $modx->getOption('locale')); is false. Then the locale system setting does not contain a valid locale.
I think that I am not being able to properly explain myself (sorry, English is not my native language).
What I am trying to say is that if you setup modx with xRouting and Babel (please ignore the snippet I shared above) the locale setting will not be taken into account if defined on the context when switching over the different contexts.
Specifically about the snippet, it was just an attempt to force to set the locale defined in the context (that worked, even though not properly written as you explained).
The locale switching in LangRouter/xRouting works fine in my installations. Maybe it is the cached placeholder that causes the issue (after changing the context setting it is possible, that the resource cache is not cleared - especially with the Cross Contexts Settings Extra).
Switching between contexts should also reinitialise the culture settings without using a plugin.
Switching between contexts should also reinitialise the culture settings without using a plugin.
If thats done without an additional database query and if it could be disabled, I am fine with a change there.
With "culture settings" you also mean date_timezone? See https://community.modx.com/t/context-setting-date-timezone-is-ignored-for-resources-un-publish-date/1618
Hi all,
Are you planning to include the fix for this in 2.7.3-pl?
Thanks in advance
I鈥檒l check and see what鈥檚 possible in the upcoming week. I鈥檒l come back to it!
Most helpful comment
Switching between contexts should also reinitialise the culture settings without using a plugin.