Thanks Bob, I would up doing a couple different things… took a lesson from Laravel and implemented a “languages as strings” approach to deal with things like pagetitle, longtitle, etc. It can also handle items hard coded in the database.
The drawback here is that if someone edits a pagetitle… etc, I’ll have to create a plugin to handle updating the json file, also @inline chunks for getResource, wayfinder etc don’t work, tpl files need to be created.
<?php
/**
* If the cultureKey is not 'fr' ... we don't need to do anything
* Just echo the string back
* [[!getLanguageByString? &translate=`[[*pagetitle]]`]]
* [[!getLanguageByString? &translate=`[[*longtitle:default=`[[*pagetitle]]`]]`]]
*
*/
/*
/XYZ/core/components/courses/lexicon/language_strings.json
{
"Logout": "Déconnexion",
"Dashboard": "Tableau de bord.",
"Profile": "Profil",
"Change Password": "Changer le mot de passe.",
"Register": "S'inscrire.",
}
*/
if($modx->getOption('cultureKey') == 'fr')
{
$json = file_get_contents(MODX_BASE_PATH . '/XYZ/core/components/courses/lexicon/language_strings.json');
$strings = json_decode($json, false);
foreach($strings as $key => $value)
{
// we should look at making this not case sensitive ....
if(trim($key) == trim($scriptProperties['translate']))
{
echo trim($value);
return;
}
}
echo $scriptProperties['translate'];
}else{
echo $scriptProperties['translate'];
}
return;
I can also load the lexicons for different modules on a per page basis, splitting them up into smaller files is definitely the way to go! It would probably be handy to make the &lexicon attribute able to handle an array (of lexicons) but this works for now.
<?php
// [[!loadModuleLexicon? &lexicon=`moduleOne`]]
$lexicon = isset($scriptProperties['lexicon']) ? $scriptProperties['lexicon'] : FALSE;
if(!$lexicon)
{
return;
}
$modx->lexicon->load('courses:'.$lexicon);
return;
And the locale picker … I didn’t do anything with session, because “why” the cultureKey needs to be set on every request anyway, and if the browser isn’t accepting cookies … well, they can’t log in soooooo…
<?php
$redirectTo = $modx->resource->get('id');
$newLocale = isset($_GET['setLocale']) ? $_GET['setLocale'] : FALSE;
/**
* Does the browser accecpt cookies?
*/
if (isset($_GET['cookieCheck'])) {
if (!isset($_COOKIE['myLocale'])) {
/**
* throw an error if not....
*/
$modx->log(modX::LOG_LEVEL_ERROR, 'Browser does not use cookies');
}
}
/**
* was setLocale part of the querystring?
* is it in the acceptable languages?
*/
if (isset($newLocale) && in_array($newLocale, ['en', 'fr'])) {
// $modx->log(modX::LOG_LEVEL_ERROR, 'Setting cookie locale');
/**
* set the cookie
*/
setcookie('myLocale', $newLocale, time() + (86400 * 365), "/"); // 86400 = 1 day
/**
* redirect back to the same page with a cookie check
* we have to redirect or else the cookie won't be read
*/
$url = $modx->makeUrl($redirectTo, '', array('cookieCheck' => 'cookie'), 'full');
$modx->sendRedirect($url,array('type' => 'REDIRECT_REFRESH'));
}
echo $modx->getChunk('localePickerTpl', ['currentLocale' => $modx->getOption('cultureKey')]);
return;
/*
<div class="locale-picker">
[[!+currentLocale:is=`en`:then=`
<a style="color:#999999; cursor: not-allowed;" >EN</a> | <a href="[[~[[*id]]]]&setLocale=fr">FR</a>
`]]
[[!+currentLocale:is=`fr`:then=`
<a href="[[~[[*id]]]]&setLocale=en">EN</a> | <a style="color:#999999;cursor: not-allowed;">FR</a>
`]]
</div>
*/